【问题标题】:junit5 with selenium testcontainer not working带有硒测试容器的junit5无法正常工作
【发布时间】:2021-11-20 15:36:53
【问题描述】:

目标 - 我希望能够使用 Junit 5testcontainers 编写 selenium 测试。我正在编写一个非常简单的代码,以便能够检查谷歌主页搜索栏的属性。

问题 - chrome.getWebDriver();返回空值。我错过了什么吗?

异常 - java.lang.NullPointerException:无法调用“org.openqa.selenium.WebDriver.manage()”,因为“驱动程序”为空。

这是因为我在初始化 WebDriver 后尝试设置隐式等待。


我的 pom.xml -

<properties>
    <maven.compiler.source>16</maven.compiler.source>
    <maven.compiler.target>16</maven.compiler.target>
    <test-containers.version>1.16.0</test-containers.version>
    <selenium.version>3.141.59</selenium.version>
    <junit.version>5.8.1</junit.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>${selenium.version}</version>
    </dependency>

    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>testcontainers</artifactId>
        <version>${test-containers.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>selenium</artifactId>
        <version>${test-containers.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.testcontainers</groupId>
        <artifactId>junit-jupiter</artifactId>
        <version>${test-containers.version}</version>
        <scope>test</scope>
    </dependency>

</dependencies>

我的测试课-

@TestInstance(Lifecycle.PER_CLASS)
@Testcontainers
public class GoogleTest {

    @Container
    public BrowserWebDriverContainer<?> chrome = new BrowserWebDriverContainer<>()
            .withCapabilities(Config.getChromeOptions());

    private GooglePage googlePage;

    @BeforeAll
    public void setup() throws Exception {
        // ISSUE IS WITH THE LINE BELOW
        WebDriver driver = chrome.getWebDriver();
        driver.manage().timeouts().implicitlyWait(7, TimeUnit.SECONDS);
        // trying to use page object model, just navigates to the google homepage
        googlePage = new GooglePage(driver);
    }

    @Test
    public void testTitle() throws Exception {
        WebElement element = googlePage.getSearchBar();
        Thread.sleep(5000);
        assertEquals(element.getAttribute("role"), "combobox");
    }

}

In case you need it, the google drive link for the code

【问题讨论】:

  • 缺少.withCapabilities(new ChromeOptions())
  • 不,就像我在那里设置 headless=true 一样,所以为了简洁起见,我将其从 sn-p 中删除。不过谢谢

标签: selenium junit5 testcontainers


【解决方案1】:

弄错了,如果使用 junit5,fields should be private static final 我想...

我还在用public...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-10
    • 2021-09-05
    • 2021-03-07
    • 2017-05-12
    • 1970-01-01
    • 2017-01-24
    • 2014-11-27
    • 2015-10-15
    相关资源
    最近更新 更多