简体   繁体   English

Selenium + Java = FindElements (list) 并检查其中一些是否包含特定文本

[英]Selenium + Java = FindElements (list) and check if some of them contains a specific text

My question:我的问题:

Let suppose I have a page with the products I bought.假设我有一个页面显示我购买的产品。 In this page I need to verify if one of these products added contains the same name that I got in a previous step and put into a var ( String firstProductName ).在此页面中,我需要验证添加的这些产品之一是否包含与我在上一步中获得并放入 var ( String firstProductName ) 中的名称相同的名称。

So I notice the cssLocator .name is the locator for all these products' names.所以我注意到 cssLocator .name是所有这些产品名称的定位器。 If I had only one product bought, I would just find it by this locator and use getText() to verify if contains the same name that I have stored in the var firstProductName .如果我只购买了一个产品,我会通过这个定位器找到它并使用getText()来验证是否包含与我存储在 var firstProductName中的名称相同的名称。

The problem is: Sometimes I have only one product, sometimes I have more than one.问题是:有时我只有一种产品,有时我有不止一种。

I need to:我需要:

  1. Access this page, find all elements with the .name locator.访问此页面,使用.name定位器查找所有元素。
  2. Then I need to check one by one seeing if through the getText() method the text found contains my string firstProductName然后我需要一一检查通过getText()方法找到的文本是否包含我的字符串firstProductName
  3. If at least one have the name equals to this String, my test is ok.如果至少有一个名称等于这个字符串,我的测试就可以了。 If not, the test fails.如果不是,则测试失败。

How do I do that?我怎么做?

Something like:就像是:

List<WebElement> allProducts = select.findElements(By.cssSelector("{not quite clear what your selector is, but it includes "name"}"));
for (WebElement product: allProducts) {
    if(product.getText().equals(firstProductName)) 
        return; // or break or whatever
}

You could wrap this all up in a function as below.您可以将所有这些都包装在一个函数中,如下所示。

/**
 * Determines if the supplied product name is found on the page
 * 
 * @param productName
 *            the product name to be searched for
 * @return true if the product name is found, false otherwise
 */
public boolean foundProduct(String productName)
{
    List<WebElement> products = driver.findElements(By.className("name"));
    for (WebElement product : products)
    {
        if (product.getText().trim().equals(productName))
            return true;
    }

    return false;
}

Then in your test code you would do something like然后在你的测试代码中你会做类似的事情

assertTrue(foundProduct("some product name"));

but I don't know if you are using TestNG, etc. Basically the test passes if true is returned or fails if false is returned.但我不知道你是否在使用 TestNG 等。基本上,如果返回true则测试通过,如果返回false则测试失败。

For Java 8 and above try this solution对于 Java 8 及更高版本,请尝试此解决方案

public boolean productsContainsProvidedProduct(String product) {

 List<WebElement> products = driver.findElements(By.xpath("your_xpath_to_list_of_elements"));  
 wait.until(ExpectedConditions.visibilityOfAllElements(products));
 return products.stream().anyMatch(e -> e.getText().trim().contains(product));
}

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

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