I’m experiencing a weird problem. I have a mapped element, from which I need to get an array of elements. If tested in chrome/edge dev mode, the xpath of mapped element happily returns number of elements. But FindAll returns only one (first) element. Here is the code I’m using:
function CompareElementsWithRefJSON(createNewRefFiles, setElementFlag, headerInputs, tabInputs, autocompleteMenu, selectPopUpMenu, tabsData, filePath)
{
let headerItems = headerInputs.FindAll('type','*',5000);
headerItems = headerItems.toArray();
...
}
I have a workaround, using FindElements, which works OK, but I don’t like it, because it’s unnecessary complicated:
function CompareElementsWithRefJSON(createNewRefFiles, setElementFlag, headerInputs, tabInputs, autocompleteMenu, selectPopUpMenu, tabsData, filePath)
{
let formInputsXpath = GetXpathFromAlias(headerInputs); // This returns xpath of mapped alias object
let headerParent = headerInputs.Parent; // Get parent element
let headerItems = headerParent.FindElements(formInputsXpath); // Returns array of elements. Works OK.
...
}
So, any idea why FindAll returns just one element? BTW, it does not matter what PropName I use as the first parameter. The result is the same. Always just one element.
Here’s a simple example, of using FindAll to search for all objects that have the specified value of ‘ObjectType’ and of the specified property ‘Button’.
The search is performed in the object hierarchy displayed in the Object Browser panel starting from ‘Panel(“main”)’ object and continuing down the hierarchy to the specified depth of 1.
If you were to do
let headerItems = headerInputs.FindAll('type','*',5000);
Log.Message("Number of items: " & UBound(headerItems))
What does “Number of items:” show? And what does the Object Browser hierarchy show?
This is my 4th attempt to answer you. From some reasons, my answers disappear few minutes after posting My guess is that it happens because of attached images? I will try to attach the pictures in separate answers.
The code returns “1” and the found element is just the first element from the expected array of elements.
As for the Object Browser, no matter what I try, it always shows an ancestor element, instead of the one I’m trying to show.
Yes, there definitely is “Type” property in the found element. In fact, each of the elements, if inspected with Spy, have Type property. And I also tried other properties, unfortunately, with he same result.
Could you create a new project and select JavaScript as the language (or convert the code and use VBScript). Using Chrome, navigate to https://www.w3schools.com/css/css3_buttons.asp and run the following code,
function GetAllButtons()
{
var browser = Sys.Browser("chrome");
var page = browser.Page("https://www.w3schools.com/css/css3_buttons.asp").Panel(2).Panel(0).Panel("belowtopnav").Panel(0).Panel("main");
var buttons = page.FindAll("ObjectType", "Button", 1);
Log.Message(`Number of buttons on page ${buttons.length}`);
}
Does it work, and does it return 56 buttons? I just want to eliminate and ensure that FindAll method is working as expected, before proceeding to the next step.
Name mapping and aliases. Note, the that hierarchy structure is kept the same as shown in the Object Browser.
I can use the mapped object like so,
function GetAllButtonsAliases()
{
var buttons = NameMapping.Sys.browser.pageCssButtons.panelCss.panelCss.panelBelowtopnav.panelCss.panelMain.FindAll("ObjectType", "Button", 1);
Log.Message(`Number of buttons on page ${buttons.length}`);
}
Or via the the aliases name,
function GetAllButtonsAliases()
{
var buttons = Aliases.browser.pageCssButtons.panelMain.FindAll("ObjectType", "Button", 1);
Log.Message(`Number of buttons on page ${buttons.length}`);
}
The difference, is that I’m looking for the criteria where ObjectType equals Button, in panelMain. Whereas, you are looking in button and this will return 1 object. You need to ensure you are finding the appropriate “children” within the “parent” node.
OK, I see. Thank you for your help! Unfortunately, this is not what I’m looking for. It’s similar to FindElements solution from my initial post Also in this case, I need to use “parent” element, to find “child” elements.
What I want to do, is to directly use a mapped element, pointing to multiple elements. I’m quite surprised there is no simple way to obtain the list of elements from a mapped element, whose xpath is pointing to multiple elements. Basically, I don’t want to use parent/child relationship and define additional property names/values in code, to find certain child elements.
Just for fun here is the xpath I’m using:
//div[contains(@class, ‘editFormHeader’)]//*[contains(local-name(),‘mat-checkbox’) or contains(local-name(),‘mat-form’) or contains(local-name(),‘mat-select’) or contains(local-name(),‘mat-button-toggle’) or contains(local-name(),‘mat-radio-button’)]//*[(name()=‘input’ and not (contains(@class,‘fake’))) or name()=‘textarea’ or name()=‘mat-select’ or (name()=‘button’ and not (@aria-label=‘Clear’) and not (@id=‘globe’) and not (@type=‘submit’))]
As you can see, it’s quite complicated xpath, but it works nicely. It returns only expected form elements. I’m using this approach also in Ranorex, where I can quickly get the list of elements from a mapped object, using simple iList and CreateAdapters method. No need or defining and referring parent element. As it seems, there is no similarly simple way to achieve this in TC?
The examples that I have provided, that should be enough to help you out in order to achieve what you require. You just need to ensure you are referring to the correct parent, to find all the children’s that matches your criteria up to the depth of 5000 - since you mentioned “FindAll returns just one element”, the passed parameter for headerInputs, may not be the correct parent.
Are you able to provide a screenshot of your Object Browser and the parent object, followed by the child objects you are searching for?
I’m not a great fan of XPath, and the XPath you have provided, it’s not easy to understand!
The problem is, that defining matching criteria for FindAll, would be a pain. It’s just easier to obtain all required elements via single, yet complicated, xpath. I personally prefer to define all my elements via xpaths than to define additional search parameters in code. But yeah, I understand your dislike
What I’m working with, is the Angular-based UI, which alone is pain to automate. But It’s quite manageable with some clever xpaths, combining IDs and classes and other properties. This is why I prefer to use xpaths. They may not be easy to construct, but once done, they work.
As for the Object browser, there is unfortunately not much to show, because as I mentioned before, no matter what I try, instead of mapped element, pointing to multiple items, it shows the ancestor Main_Page mapped element. I’m sorry to say this, but TC ObjectBrowser is rather inferior to other element browsers (particularly Ranorex Spy).
Thank you anyway for your help. I will stick with my original FindElements solution. I don’t like it either, but it’s probably easier than defining all matching criteria for FindAll.
Nope. headerInputs refers to the alias element, containing the above mentioned xpath. I now understand, that FindAll will not work as I expected it to work.
Sadly, there seems to be no simple method, which can return the array of elements directly from the alias, whose xpath returns multiple elements. The only way seems to be using Parent/Ancestor element, from which can be obtained array of elements, using EvaluateXPath or FindElements and above xpath passed as a parameter. There is not even an option to pass the Alias object directly to those methods. They only accept xpath. So one either needs to extract the xpath from the Alias element (using custom code) or using hardcoded xpath. I really like TC, but some things are surprisingly complicated.
What is the alias element? I assumed it was Aliases.browser.Main_PAGE that you were passing for headInputs as highlighted.
I’m just trying to understand what parameter is passed to the function CompareElementsWithRefJSON for headerInputs - as I don’t think it was mentioned in the post.
function CompareElementsWithRefJSON(createNewRefFiles, setElementFlag, headerInputs, tabInputs, autocompleteMenu, selectPopUpMenu, tabsData, filePath)
{
let headerItems = headerInputs.FindAll('type','*',5000);
headerItems = headerItems.toArray();
...
}
This alias contains the xptah, poiting to multiple form inputs. But whenever I try to show this Alias in Object Browser, it simply jumps to Main_PAGE element.
Another reason for not liking XPath, it would never located the correct object in the Object Browser!
If you were to Highlight, is TestComplete able to locate your mapped object i.e. Aliases.browser.Main_PAGE.DescartesRoutePlanner_SECTION.Asset_Header_FRM.Asset_Form_Header_IPTs on the UI?
If yes, are you able to try the following snippets of code in a separate function,
// Also try with a different object/property
// Test 1
var obj = Aliases.browser.Main_PAGE.DescartesRoutePlanner_SECTION.Asset_Header_FRM.Asset_Form_Header_IPTs.FindAll("type", "*", 100);
Sys.HighlightObject(obj);
Log.Message(`Number of objects ${obj.length}`);
// Test 2
var obj = Aliases.browser.Main_PAGE.DescartesRoutePlanner_SECTION.Asset_Header_FRM.FindAll("type", "*", 100);
Sys.HighlightObject(obj);
Log.Message(`Number of objects ${obj.length}`);
// Test 3
var obj = Aliases.browser.Main_PAGE.DescartesRoutePlanner_SECTION.FindAll("type", "*", 100);
Sys.HighlightObject(obj);
Log.Message(`Number of objects ${obj.length}`);
Does it highlight the correct object in question, and does it return more than one object matching the criteria?
I’m just trying to mimic what you have done. I have a mapped object using XPath, and I’m referring to that object using aliases. FindAll and FindElements return the same number of objects based on the criteria search
Thank you for your investigation and help! I really appreciate it.
The Test 2 is closest to what I want. The thing is it returns all inputs from given form, not just those specified with my xpath. So it’s not usable for my needs. I would have to specify an array of FindAll properties, which is not something I want to do. Anyway, I have a workaround using the Parent function. Not nice, but functional.
BTW, sys.highlight fails with “type error”, because obj is in fact array, even if it returns only one element. It only works if I specify the obj with array index.
You can also include multiple objects and property values e.g. FindAll([“ObjectType”, “Enabled”, “contentText”], [“Button”, “True”, “Default*”], 100) to search for objects matching that criteria in Aliases.browser.Main_PAGE.DescartesRoutePlanner_SECTION.Asset_Header_FRM
My copy and paste of “Sys.HighlightObject(obj);” didn’t go to plan!