使用下面的代码,我试图打开一个链接页面,然后转到移动部分并根据名称顺序对项目进行排序。现在我想检查移动设备是否按名称按字母顺序排序。 我想我的列表转换下面的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);
}
}
能否请你添加一些例子,展示你想达到什么样的?列表目前的状况如何?为了检查列表是否已排序,您可以遍历它并检查一个元素是否正确地大于或等于(关于给定顺序)前一个元素。或者你可以对它进行排序,并检查结果列表是否与输入列表相同(只有在没有相同的元素时才会起作用,因为它们可能会改变顺序)。 – Zabuza
在最后一行中,有一些代码丢失:'HashSet value = new'。 –
Zabuza
我试图做哈希集,但无法实现,根据我的代码 –