java - how to check if webelements are in order? -


using code below, trying open link page , go mobile section , sort items on basis of name order. want check if mobile devices sorted name means alphabetically. tried convert list below arraylist not able check if elements printed in ascending order, kindly help

package selflearning;  import java.util.arraylist; import java.util.collection; import java.util.collections; import java.util.hashset; import java.util.list;  import org.openqa.selenium.by; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.firefox.firefoxdriver; import org.openqa.selenium.support.ui.select;  public class guru99ecommerce1 {     public static void main(string[] args) throws exception {         system.setproperty("webdriver.gecko.driver","c:\\geckodriver\\geckodriver.exe");         webdriver driver = new firefoxdriver();         driver.get("http://live.guru99.com/index.php/");          string title=driver.gettitle();         string expectedtitle = "home page";         system.out.println("the title of webpage " + title);         expectedtitle.equalsignorecase(title);         system.out.println("title verified");         driver.findelement(by.xpath("//a[text()='mobile']")).click();          string nexttitle = driver.gettitle();         system.out.println("the title of next page" + nexttitle);          string nextexpectedtitle = "pagemobile";         nextexpectedtitle.equalsignorecase(nexttitle);         system.out.println("the next title verified");          select s = new select(driver.findelement(by.xpath("//div[@class='category-products']//div/div[@class='sorter']/div/select[@title='sort by']")));         s.selectbyvisibletext("name");          list<webelement> element = driver.findelements(by.xpath("//div[@class='product-info']/h2/a"));          for(webelement e: element)         {             string str = e.gettext();             system.out.println("the items  " + str);         }         hashset<webelement> value = new          list<webelement> list = new arraylist<webelement>(element);         list.addall(element);         system.out.println("arrangement" + list);     } } 

the easiest way grab list of products, loop through them, , see if current product name (a string) "greater" last product name using string#comparetoignorecase().

i write functions common tasks repeat page.

public static void sortby(string sortvalue) {     new select(driver.findelement(by.cssselector("select[title='sort by']"))).selectbyvisibletext(sortvalue); }  public static list<string> getproductnames() {     list<string> names = new arraylist<>();     list<webelement> products = driver.findelements(by.cssselector("ul.products-grid h2.product-name"));     (webelement product : products)     {         names.add(product.gettext());     }      return names; }  public static boolean islistsorted(list<string> list) {     string last = list.get(0);     (int = 1; < list.size(); i++)     {         string current = list.get(i);         if (last.comparetoignorecase(current) > 0)         {             return false;         }          last = current;     }      return true; } 

note: should using junit or testng assertions instead of writing own because makes much, easier (and don't have write , debug own saves time). code wrote below using testng. can see how shorter (and simpler) code below when using library testng.

string url = "http://live.guru99.com/index.php"; driver.navigate().to(url);  assert.assertequals(driver.gettitle(), "home page"); driver.findelement(by.xpath("//nav[@id='nav']//a[.='mobile']")).click(); assert.assertequals(driver.gettitle(), "mobile"); sortby("name"); system.out.println(getproductnames()); system.out.println(islistsorted(getproductnames())); 

where getproductnames() returns

[iphone, samsung galaxy, sony xperia] 

Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -