Page 1 of 1

count from the user code how many times a xpath is found

Posted: Tue Feb 04, 2020 4:57 pm
by prepelra
I have this situation:

I need to validate, in a web page, after a submit an empty form - that 2 icons are appearing in the mandatory fields.
The icon is the same, and the difference in xpath between the 2 icons is only the position in webpage, a div number[].
Is not a stable solution, this number can be changed after a new version of the website is released and design is changed. Also image validation is not a good solution.

//*[@id="baseComponent"]/mat-sidenav-container/mat-sidenav-content/div/one-new-patient/div/one-patient-form/div/div/div[1]/div[2]/div/div[10]/mat-form-field/div/div[1]/div[2]/mat-icon

//*[@id="baseComponent"]/mat-sidenav-container/mat-sidenav-content/div/one-new-patient/div/one-patient-form/div/div/div[1]/div[2]/div/div[11]/mat-form-field/div/div[1]/div[2]/mat-icon

As you see the difference is only the div[10] , div [11]

So, the solution was to create a new xpath for both elements, a xpath which is founding 2 elements in tracking.

.//tag[#'baseComponent']//tag[@tagname='mat-icon' and @ng-reflect-svg-icon='icon_warning' and @svgicon='icon_warning'] this xpath is found 2 elements, the 2 icons.

I will need to create a user code and count the number of elements found. So test will pass if two elements for the same xpath are found.
Is this possible from the user code? Should I use Find method for the web? I am more a Java automated developer, so #C is not so friendly for me, at this time :roll:



Thanks

Re: count from the user code how many times a xpath is found

Posted: Wed Feb 05, 2020 8:16 am
by odklizec
Hi,

At first, could you please share a Ranorex snapshot of the app, where we can evaluate both icons? Maybe there is a better way than counting elements?

As for the code, you can use something like this, where repoElement parameter should be connected to the repo element that points to both icons:

Code: Select all

private void CountElements(RepoItemInfo repoElement)
{
    // Create a list of adapters using the "Info" object
    IList<Ranorex.WebElement> elementList = repoElement.CreateAdapters<Ranorex.WebElement>();
    int elementListCount = elementList.Count;
    if (elementListCount == 2)
    {
        Report.Success("Validation", "Icon validation succeeds");
    }
    else
    {
        Report.Failure("Validation", "Icon validation succeeds");
    }
}
Hope this helps?