How to check the order of rusks in selenium

I have a title defined as First / Second / Third as breadcrumbs. I would like to check if the elements are displayed in the correct order using selenium with an optimal coding way.

<ol class = "breadcrumb"
     <li class="break-all">        
          <a href="..." class="break-all">First</a>
          <span class="divider">/</span> 
   </li>
   <li class="break-all">        
          <a href="..." class="break-all">Second</a>
          <span class="divider">/</span> 
   </li>
   <li class="break-all">        
          <a href="..." class="break-all">Third</a>
          <span class="divider">/</span> 
   </li>
</ol>

      

Now that I do

findBy("//ol[@class='breadcrumb']")

      

i get all the items.

+3


source to share


4 answers


findBy("//ol[@class='breadcrumb']").getText().equals("First / Second / Third");

      



it should work.

+1


source


First, you need to find all the elements of the palette, for example with cssSelector()

. Then, for each WebElement in the list, call getText()

to get the actual text:



List<String> expected = Arrays.asList("First", "Second", "Third");

List<WebElement> breadcrumbs = driver.findElements(By.cssSelector("ol.breadcrumb li a"));
for (int i = 0; i < expected.length; i++) {
    String breadcrumb = breadcrumbs.get(i).getText();
    if (breadcrumb.equals(expected[i])) {
        System.out.println("passed on: " + breadcrumb);
    } else {
        System.out.println("failed on: " + breadcrumb);
    }
}

      

+2


source


To solve your problem, you can follow the following process:

1- Create an "ArrayList" and add all the items to be compared with.
2- Get link texts and put them in a new ArrayList.
3- Make sure the two ArrayList arguments match.

Below is the code below:

//Adding all the list items to compare in an ArrayList
ArrayList<String> alist = new ArrayList<String>();
alist.add("First");
alist.add("Second");
alist.add("Third");

//Checking the Arraylist data
System.out.println("The list values are as under: ");
for(String list_item: alist)
    System.out.println(list_item);

//Creating an ArrayList to store the retrieved link texts
ArrayList<String> List_Compare = new ArrayList<String>();

//Retrieving the link texts and putting them into the Arraylist so created
List<WebElement> New_List = driver.findElements(By.xpath("//a[@class='break-all']"));
for(WebElement list_item: New_List){
    List_Compare.add(list_item.getText());
}

//Checking the new Arraylist data
System.out.println("The Retrieved list values are as under: ");
for(String list_item: List_Compare)
    System.out.println(list_item);

//Asserting the original Arraylist matches to the Arraylist with retrieved Link Texts 
try{
    Assert.assertEquals(alist, List_Compare);
    System.out.println("Equal lists");
}catch(Throwable e){
    System.err.println("Lists are not equal. "+e.getMessage());
}

      

NOTE. Import the Assert class using the last piece of code to work. import junit.framework.Assert;

+1


source


You need to create a list of known values ​​to compare. And then use findElements()

to find all items that match your goal. In this, you also need to carefully write the selector so that it captures the list of expected elements. //a[@class='break-all']

can be used to grab a list of desired items

String[] expected = {"First", "Second", "Third"};
List<WebElement> allOptions = select.findElements(By.xpath("//a[@class='break-all']"));

// make sure you found the right number of elements
if (expected.length != allOptions.size()) {
    System.out.println("fail, wrong number of elements found");
}
// make sure that the value of every <option> element equals the expected value
for (int i = 0; i < expected.length; i++) {
    String optionValue = allOptions.get(i).getText();
    if (optionValue.equals(expected[i])) {
        System.out.println("passed on: " + optionValue);
    } else {
        System.out.println("failed on: " + optionValue);
    }
}

      

The implementation is taken from here

0


source







All Articles