2017-09-10 26 views
-1

使用下面的代码,我试图打开一个链接页面,然后转到移动部分并根据名称顺序对项目进行排序。现在我想检查移动设备是否按名称按字母顺序排序。 我想我的列表转换下面的ArrayList但不能够检查是否印刷元素按升序排列,好心帮如何检查webelements是否有序?

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 the webPage is " + title); 
     expectedTitle.equalsIgnoreCase(title); 
     System.out.println("Title is 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 is 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 are " + str); 
     } 
     HashSet<WebElement> value = new 
     List<WebElement> list = new ArrayList<WebElement>(element); 
     list.addAll(element); 
     System.out.println("arrangement" + list); 
    } 
} 
+0

能否请你添加一些例子,展示你想达到什么样的?列表目前的状况如何?为了检查列表是否已排序,您可以遍历它并检查一个元素是否正确地大于或等于(关于给定顺序)前一个元素。或者你可以对它进行排序,并检查结果列表是否与输入列表相同(只有在没有相同的元素时才会起作用,因为它们可能会改变顺序)。 – Zabuza

+0

在最后一行中,有一些代码丢失:'HashSet value = new'。 – Zabuza

+0

我试图做哈希集,但无法实现,根据我的代码 –

回答

1

要做到这一点是只抓产品列表中最简单的方式,依次通过他们,并使用String#compareToIgnoreCase()查看当前产品名称(a String)是否比上一个产品名称“大”。

我会为您可能为本页重复的常见任务编写一些函数。

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")); 
    for (WebElement product : products) 
    { 
     names.add(product.getText()); 
    } 

    return names; 
} 

public static boolean isListSorted(List<String> list) 
{ 
    String last = list.get(0); 
    for (int i = 1; i < list.size(); i++) 
    { 
     String current = list.get(i); 
     if (last.compareToIgnoreCase(current) > 0) 
     { 
      return false; 
     } 

     last = current; 
    } 

    return true; 
} 

注意:您要使用JUnit或TestNG的为您的断言,而不是写自己的,因为它使得它非常非常容易(你不必编写和调试自己节省时间)。我在下面写的代码是使用TestNG。您可以看到使用类似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())); 

getProductNames()回报

[IPHONE, SAMSUNG GALAXY, SONY XPERIA] 
+0

@JainishKapadia'getProductNames()'的代码在上面。我不明白你在问什么。 – JeffC