使用Java声明List <>中的第一项

时间:2018-07-16 18:24:38

标签: java list selenium

为了练习,我使用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)

希望有人可以在此处帮助识别我的错误。非常感谢您提供的所有帮助!

1 个答案:

答案 0 :(得分:2)

此:

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

}

PS:

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