比较元素列表和字符串数组

时间:2017-12-21 08:43:35

标签: java selenium-webdriver arraylist

我有以下代码

public void main() throws InterruptedException {

//expected messages to be displayed in tool tip are as below
   String[] expected_tootltip_Msgs = {"A", "B", "C",
        "D","E","F","G"};

//declaring integer to know the total count
        Integer counter=0;
        Thread.sleep(20000);
        List<WebElement> listImages=driver.findElements(By.tagName("img"));
        System.out.println("No. of Images: "+listImages.size());
        for(WebElement image:listImages)
        {
            if(image.isDisplayed())
            {
                counter++;
               System.out.println(image.getAttribute("alt"));
            }
        }
        System.out.println("No. of total displable images: "+counter);        
  } 

如何比较字符串expected_tooltip_msgs和列表元素显示的输出?如果两者都相同,我的测试用例就会通过。有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为您需要的是将预期的字符串放入列表(如果您希望重复)或Set(如果您没有重复)。

e.g。

List<String> expectedTooltips = Lists.newArrayList("A", "B", "C",
        "D","E","F","G"); // this uses the Guava library helper method, you could use List.of if you are using Java 9
...

List<String> actualTooltips = new ArrayList<>();
for(WebElement image:listImages)
{
    if(image.isDisplayed())
    {
       actualTooltips.add(image.getAttribute("alt"));
    }
}

boolean areTooltipsAsExpected = expectedTooltips.equals(actualTooltips);