Hello. There is such a code. The snag in the place where I left XXXXXXXX. How do I go there each time in a cycle to substitute input data in turn (from 0th to last, one cycle).

public void MenuText (String ExpectedMenuItem1, String ExpectedMenuItem2, String ExpectedMenuItem3, String ExpectedMenuItem4, String ExpectedMenuItem5){ List<WebElement> allLi = driver.findElements(By.xpath("//ul[@class = 'navbar_item']/li")); int sizeAllLi = allLi.size(); for(int i = 0; i < sizeAllLi; i++){ Assert.assertEquals(XXXXXXXX, allLi.get(i).getText()); } 

Thank you in advance.

UPD: Thanks for the tips and for the idea of ​​using a variable number of arguments. And how do I then shove the data into this method? So I understand, it should be an array. This is code under Selenium and I want to use TestNG. Input data is specified in the xml file. While it turns out this way (taking into account your advice):

 @Test @Parameters({"ExpectedMenuItem1", "ExpectedMenuItem2", "ExpectedMenuItem3", "ExpectedMenuItem4", "ExpectedMenuItem5"}) public void MenuText (String ... expectedMenuItems){ driver.get("https://www.medimpact.com/"); List<WebElement> allLi = driver.findElements(By.xpath("//ul[@class = 'navbar_item']/li")); int sizeAllLi = allLi.size(); for(int i = 0; i < sizeAllLi; i++){ // Assert.assertEquals(ExpectedMenuItem, allLi.get(i).getText()); Assert.assertTrue(expectedMenuItems.length > i); Assert.assertEquals(expectedMenuItems[i], allLi.get(i).getText()); } 
  • 2
    it may make sense to transfer to the method all the same an array (or list) of menu items, and not separately. And if their number suddenly grows to 20? Accordingly, it will suffice also to take from the array by index ....... - Alexey Shimansky
  • one
    I will add that there is to use varargs, then you do not have to change the external code. public void MenuText(String... ExpectedMenuItems) this: public void MenuText(String... ExpectedMenuItems) - diraria

2 answers 2

In the method you need to use varargs . MenuText(String... yourVars) this: MenuText(String... yourVars) . Inside the method, you get an array of variable length, depending on how many variables were passed. Well, and then you pass it in a cycle and substitute values.

     public void MenuText (String ... expectedMenuItems) { List<WebElement> allLi = driver.findElements(By.xpath("//ul[@class = 'navbar_item']/li")); int sizeAllLi = allLi.size(); for (int i = 0; i < sizeAllLi; i++) { Assert.isTrue(expectedMenuItems.length > i); Assert.assertEquals(expectedMenuItems[i], allLi.get(i).getText()); } }