如何检查Webelement是否为秩序



使用以下代码,我正在尝试打开一个链接页面,然后转到移动部分,然后根据名称顺序对项目进行排序。现在,我想检查移动设备是否按名称为字母顺序排序。我试图将下面的列表转换为arraylist,但无法检查打印的元素是否按顺序进行,请帮助

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);
    }
}

最简单的方法就是抓住产品列表,循环浏览它们,看看当前产品名称(a String)是否比上一个产品名称"更大"使用String#compareToIgnoreCase()

我会为您可能重复此页的常见任务编写一些功能。

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]

最新更新