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

问题描述 投票:1回答:1

我有以下代码

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

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

java selenium-webdriver arraylist
1个回答
0
投票

我认为你需要的是将预期的字符串放入一个列表(如果你期望重复)或一个集合(如果你没有重复)。

EG

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);
© www.soinside.com 2019 - 2024. All rights reserved.