gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 10:50:36 24 4
gpt4 key购买 nike

我有以下代码

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 和列表元素显示的输出?如果两者相同,我的测试用例就会通过。有人可以帮我解决这个问题吗?

最佳答案

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

例如

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

关于java - 比较元素列表和字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47921137/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com