gpt4 book ai didi

java - 使用 Java 断言 List<> 中的第一项

转载 作者:行者123 更新时间:2023-11-30 02:05:59 27 4
gpt4 key购买 nike

为了练习,我使用 Selenium (java) 创建一个针对术语“Selenium”的测试搜索 google,并断言结果页面顶部的第一个结果是 SeleniumHQ。

自从我使用 Java 以来已经很多年了,我很难记住如何迭代列表并断言列表中的第一个对象。

public class SeleniumTest {

private static String baseURL = "http://google.com";
private static String searchTerm = "Selenium";
private static String seleniumResultString = "Selenium - Web Browser Automation";
private static String googleSearchBarLocatorById = "lst-ib";
private static String googleResultLocatorByClassName = "r";


public void driverProperties() {
System.setProperty("webdriver.chrome.driver", "/Users/<<user>>/Downloads/chromedriver");

}

private void searchGoogleForSelenium() {
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
((ChromeDriver) driver).findElementById(googleSearchBarLocatorById).sendKeys(searchTerm);
((ChromeDriver) driver).findElementById(googleSearchBarLocatorById).sendKeys(Keys.ENTER);
}

private void returnGoogleResults() {
WebDriver driver = new ChromeDriver();
List<WebElement> resultTitles = new ArrayList<>(((ChromeDriver) driver).findElementsByClassName(googleResultLocatorByClassName));
if (resultTitles.get(0).equals(seleniumResultString)) {
System.out.println(seleniumResultString + " is the first result!");
driver.close();
} else {
System.out.println(seleniumResultString + " is not the first result!");
driver.close();
}

}

@Test
public void seleniumResultsTest() {
driverProperties();
searchGoogleForSelenium();
returnGoogleResults();
}
}

输出:

java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at com.slg.selenium.SeleniumTest.returnGoogleResults(SeleniumTest.java:36)
at com.slg.selenium.SeleniumTest.seleniumResultsTest(SeleniumTest.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

希望有人可以帮助找出我的错误。非常感谢您的帮助!!

最佳答案

这个:

List<WebElement> resultTitles = new ArrayList<>(((ChromeDriver) driver).findElementsByClassName(googleResultLocatorByClassName));

可以生成大小为 0 的列表,当您尝试获取空列表的第一个元素时,您将得到 java.lang.IndexOutOfBoundsException。这就是为什么添加:

if (resultTitles.size() > 0)

或者正如 @AndyTurner 已经提到的:

if (!resultTitles.isEmpty())

替换:

private void returnGoogleResults() {
WebDriver driver = new ChromeDriver();
List<WebElement> resultTitles = new ArrayList<>(((ChromeDriver) driver).findElementsByClassName(googleResultLocatorByClassName));
if (resultTitles.get(0).equals(seleniumResultString)) {
System.out.println(seleniumResultString + " is the first result!");
driver.close();
} else {
System.out.println(seleniumResultString + " is not the first result!");
driver.close();
}

}

与:

private void returnGoogleResults() {
WebDriver driver = new ChromeDriver();
List<WebElement> resultTitles = new ArrayList<>(((ChromeDriver) driver).findElementsByClassName(googleResultLocatorByClassName));
if (!resultTitles.isEmpty() && resultTitles.get(0).equals(seleniumResultString)) {
System.out.println(seleniumResultString + " is the first result!");
driver.close();
} else {
System.out.println(seleniumResultString + " is not the first result!");
driver.close();
}

}

附注:

List<WebElement> resultTitles = new ArrayList<>(((ChromeDriver) driver).findElementsByClassName(googleResultLocatorByClassName));

可以这样简化:

List<WebElement> resultTitles = driver.findElementsByClassName(googleResultLocatorByClassName));

还有:

private void searchGoogleForSelenium() {
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
((ChromeDriver) driver).findElementById(googleSearchBarLocatorById).sendKeys(searchTerm);
((ChromeDriver) driver).findElementById(googleSearchBarLocatorById).sendKeys(Keys.ENTER);
}

可以替换为:

private void searchGoogleForSelenium() {
WebDriver driver = new ChromeDriver();
driver.get(baseURL);
driver.findElementById(googleSearchBarLocatorById).sendKeys(searchTerm);
driver.findElementById(googleSearchBarLocatorById).sendKeys(Keys.ENTER);
}

关于java - 使用 Java 断言 List<> 中的第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51367985/

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