简体   繁体   English

如何使用Java在Selenium中验证字符串列表是否按字母顺序排列

[英]how to validate a list of strings are in alphabetical order or not in Selenium using java

Is anybody know how to validate a list of strings are in alphabetical order or not using java . 有谁知道如何验证按字母顺序排列的字符串列表或不使用java I tried with compareTo() but I didn't get it. 我尝试了compareTo()但没有得到。

* **It is only for the validation of String . * **仅用于验证String If yes to print "List is in Alphabetical order" and No, to print "Not in order" 如果是,则打印“列表按字母顺序排列”,否,则打印“不按顺序排列”

Here is the code::: 这是代码:::

import java.util.concurrent.TimeUnit;
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 ValidationOfSelectDropDown_4 {

public static void main(String[] args) throws InterruptedException {

    WebDriver driver=new FirefoxDriver();
    driver.get("http://my.naukri.com/manager/createacc2.php?othersrcp=16201&err=1");
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    driver.manage().window().maximize();

    WebElement single=driver.findElement(By.xpath("//select[@id='ugcourse']"));
    Select dd=new Select(single);

    String str=single.getText();
    System.out.println(str);

    }}

O/p of the String str is : 字符串str的O / p为:

Select 选择

Not Pursuing Graduation 不追求毕业

BA BA

B.Arch 阿奇

BCA BCA

BBA 工商管理学士

...Up to end of the option ...直至选项结束

If you already know how to get the list of strings from webdriver (which it looks like you do), you can check the ordering of the list by comparing each element to the previous one: 如果您已经知道如何从webdriver获取字符串列表(看起来像这样做),则可以通过将每个元素与上一个元素进行比较来检查列表的顺序:

if (!strings.isEmpty()) {
  Iterator<String> it = strings.iterator();
  String prev = it.next();
  while (it.hasNext()) {
    String next = it.next();
    if (prev.compareTo(next) > 0) {
      return false;
    }
    prev = next;
  }
}
return true;

If the list is small and memory is no concern, you can alternatively sort the list and compare the lists: 如果列表很小并且不关心内存,则可以对列表进行排序并比较列表:

List<String> ordered = new ArrayList<>(strings);
Collections.sort(ordered);
boolean inOrder = ordered.equals(strings);

What you want to do is put all those values in a ArrayList and check if tha list is sorted. 您要执行的操作是将所有这些值放入ArrayList中,并检查列表是否已排序。 So depending on how those values are separated (I'm assuming they're separated by new lines based on the example you've given), just do: 因此,根据这些值的分隔方式(根据您所提供的示例,我假定它们由新行分隔),只需执行以下操作:

List<String> listOfValues = str.split("\n");

Then check if tha list is ordered through Guava's Ordering methods: 然后检查是否通过番石榴的订购方法订购了列表:

boolean sorted = Ordering.natural().isOrdered(list);

Then output based on sorted's value. 然后根据排序后的值输出。

NB : this post is an aggregation from answers https://stackoverflow.com/a/3481842/2112089 and https://stackoverflow.com/a/3047142/2112089 注意:这篇文章是答案的汇总https://stackoverflow.com/a/3481842/2112089https://stackoverflow.com/a/3047142/2112089

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM