Loop for checking values between one drop-down list and one menu list

Hi,

I have a menu where the access rights of a user are listed:
image
The html:

<uxlayoutheaderprofilemenucontent>
	<ul styleclass="userContextMenu">
		<li class="ux-u-bg-info-dark nopointer ng-star-inserted">
			<h5><i class="icon ux-u-color-info-lightester-2 ion-ios-people">My Roles:</i></h5>
		</li>
		<li class="ng-star-inserted"><i class="ion-ios-person">FIDES - UnitUser - ESTAT.A.5</i></li>
		<li class="ng-star-inserted"><i class="ion-ios-person">BIFI - UnitUser - ESTAT.B.3</i></li>
		<li class="ng-star-inserted"><i class="ion-ios-person">BIFI - UnitUser - ESTAT.C.1</i></li>
		<li class="ng-star-inserted"><i class="ion-ios-person">BIFI - CSB - ESTAT.A.1</i></li>
		<li class="ng-star-inserted"><i class="ion-ios-person">BIFI - CSB - ESTAT.C.2</i></li>
	</ul>
</uxlayoutheaderprofilemenucontent>

In function of these access rights, the list of units in the forms and in the filters of the table of my application is filled with the same values. For instance there:
image

The html:

<select _ngcontent-igo-c182="" id="Unit" class="ux-form-control ux-form-control__custom-select ng-pristine ng-valid ng-star-inserted ng-touched" tabindex="0">
	<option _ngcontent-igo-c182="" value="7257" class="ng-star-inserted"> ESTAT.A.1 </option>
	<option _ngcontent-igo-c182="" value="230929" class="ng-star-inserted"> ESTAT.A.5 </option>
	<option _ngcontent-igo-c182="" value="7261" class="ng-star-inserted"> ESTAT.B.3 </option>
	<option _ngcontent-igo-c182="" value="7285" class="ng-star-inserted"> ESTAT.C.1 </option>
	<option _ngcontent-igo-c182="" value="7286" class="ng-star-inserted"> ESTAT.C.2 </option>
</select>

I don’t know exactly how to loop in the drop-down list of the forms or in the filters in order to check if the values correspond to the correct access right (of the menu below) and are correctly displayed and available.

Could you please help me with that ?

Thanks in advance for your help.

(I realize that this is listed under the Katalon Recorder category, but you will not be able to accomplish this without some scripting :slight_smile:)

As long as you don’t care about the order in which these items appear, you could simply add both sets of options to lists and compare the lists with each other:

WebDriver driver = DriverFactory.getWebDriver();

// Get a list of the "role" options, and get their text:
List<WebElement> roleElements = driver.findElements(By.xpath("//ul[@class='userContextMenu']//li[@class='ng-star-inserted']"));
List<String> roleOptions = new ArrayList<String>();
for(WebElement roleElement : roleElements) {
    String optionText = roleElement.getText();
    roleOptions.add(optionText.substring(optionText.lastIndexOf("-") + 1));
}

// Get a list of the "unit" options, and get their text:
List<WebElement> unitElements = driver.findElements(By.xpath("//select[@id='Unit']//option"));
List<String> unitOptions = new ArrayList<String>();
for(WebElement unitElement : unitElements) {
    String optionText = unitElement.getText();
    unitOptions.add(optionText);
}

// Check that the lists contain the same elements (ignoring order)
assert roleOptions.containsAll(unitOptions);

If you do care about order, then you could loop through one of the lists and compare to the options in the other list using an index:

for(int i = 0; i < roleOptions.size(); i++) {
    String roleOption = roleOptions.get(i);
    String unitOption = unitOptions.get(i);
    assert roleOption.equals(unitOption);
}

You can use variables in XPaths and loop over the items in that way.

@ThanhTo Thanks for the response. That’s signified for my case (access rights) that I have to do one loop “while” (as you did) for each access right that I have to test (store the value in a variable), isn’t it ?

I need to count the number of elements for doing the “while” condition. How can I count:

  • the number of children under a div with Katalon Recorder ?
  • the total number of options under a select ?

@Brandon_Hein Thanks for your help. I’m new in the “Automate Testing galaxy”. I don’t know how to do that and where. I’m using currently only Katalon Recorder for creating the tests and then exporting in protactor format.
We don’t use Katalon Studio for the moment. I don’t know if I can use Selenium IDE or Katalon Recorder in order to include your script in order to do That I want to test.