【问题标题】:Running Selenium Tests on Jenkins在 Jenkins 上运行 Selenium 测试
【发布时间】:2017-11-02 21:56:25
【问题描述】:

我正在尝试通过在 Red-Hat Linux 服务器上设置的 Jenkins Jobs 运行 Selenium (FluentLenium) 测试。它在项目目录中运行一个简单的 JUnit/WebDriver 测试和一个使用“mvn test”命令的 maven 构建。我面临的问题是找到一个驱动程序来运行它。这里的 Jenkins 工程师在我指定的路径上为我放了一个 geckodriver.exe。虽然它似乎并没有太大帮助。

我的代码失败的地方:

 @Override
    public WebDriver newWebDriver(){
        System.setProperty("webdriver.gecko.driver", "/var/lib/jenkins/tools/gecko/geckodriver.exe");

        return new FirefoxDriver();
    }


org.openqa.selenium.WebDriverException: 
Browser failed to start, test [ searchForGod(ok.TGoogle) ] execution interrupted.
Caused by: [ java.lang.IllegalStateException: The driver executable does not exist: /var/lib/jenkins/tools/gecko/geckodriver.exe]


+ ls /var/lib/jenkins/tools/gecko/
geckodriver
geckodriver-v0.16.1-linux64.tar.gz

我知道 geckodriver 需要 Firefox 应用程序才能运行。有人告诉我,由于 linux 服务器是非 GUI 的,a display port is setup to run Firefox。此显示端口在 linux 服务器上的环境变量上设置为 DISPLAY = 99。尽管我再次迷失了如何让我的 pom.xml 或我的 java 代码指向它。目前我正在尝试:

<plugins>
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.20.1</version>
    <configuration>
        <includes>
            <include>PASS.class</include>
            <include>TGoogle.class</include>
        </includes>
        <environmentVariables>
            <DISPLAY>99</DISPLAY>
        </environmentVariables>
    </configuration>
</plugin>
</plugins>

这些都不起作用,我也没有取得任何进展。我现在唯一的选择是在 linux 启动时设置我自己的 Jenkins 服务器,并尝试让它在本地工作,然后让我们的 Jenkins 工程师使用我的设置。尽管他声称我应该能够运行它,因为其他人已经能够通过 Selenium 插件提交 .html 文件。虽然,我没有运气并且已经用尽了所有的选择。

版本:
本地: Selenium WebDriver 3.5.3、Junit 4.9、Fluentlenium 3.4、Maven
Linux 服务器:他说他使用的是 selenium-standalone-server.jar 2.44(我知道这肯定会与我的 FluentLenium 版本冲突,但没有它我会遇到同样的异常......),Red Hat Enterprise Linux Server 7.2 版,壁虎驱动 0.19.1

更新:
以下代码用于创建与服务器的连接。 Firefox 版本现在是 52.4,geckodriver 0.16.1,Selenium 3.7.0,FluentLenium 3.4。此版本配置在本地工作。是的 geckodriver 不是 .exe。它只是没有文件扩展名的 geckodriver。

  @Override
public WebDriver newWebDriver(){
    GeckoDriverService ds = new GeckoDriverService.Builder()
            .usingDriverExecutable(new File("/var/lib/jenkins/tools/gecko/0.16.1/geckodriver"))
            .usingFirefoxBinary(new FirefoxBinary(new File("/usr/bin/firefox")))
            .withEnvironment(ImmutableMap.of("DISPLAY",":99")).build();
    return new FirefoxDriver(ds);
}

不幸的是,詹金斯现在正在投掷:

[INFO] Running TGoogle

Nov 03, 2017 3:46:48 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C

[ERROR] Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 4.703 s <<< FAILURE! - in TGoogle
[ERROR] searchForGod(TGoogle)  Time elapsed: 4.636 s  <<< FAILURE!
java.lang.AssertionError
    at TGoogle.searchForGod(TGoogle.java:29)

至少它现在正在“连接”。虽然它在本地运行良好。

更新 2: 现在终于可以工作了。 newWebDriver() 方法代码中显示的配置是正确的。我有一个包含在页面对象中的断言,而不是直接在 JUnit @Test 中。不知道为什么这会有所作为(因为它不在本地的 IntelliJ 中)。一旦我移动了断言,它就起作用了!!万岁,希望这对某人有所帮助。

【问题讨论】:

  • 我很困惑:你说它是linux,但你试图调用geckodriver.exe?还是问题描述有误?如果它在 linux 上,请删除 .exe 并告诉我们接下来会发生什么
  • 不幸的是,我无法直接访问 linux 机器。这就是为什么我建议我可能只是设置自己的服务器。在黑暗中工作非常困难(Jenkins 工程师也不知道解决方案)。
  • 我的意思是你不能在 Linux 上运行 .exe,所以 /var/lib/jenkins/tools/gecko/geckodriver.exe 没有任何意义(这是一个 linux 路径,但你正在尝试调用 windows 可执行文件)
  • 是的,这可能是我搞砸的众多细节之一。我现在怀疑那是一个.exe。我看到他从 tar.gz 中提取它,我很确定他知道自己在做什么。如果我解决了这个问题,我仍然认为我需要指向某个地方的 Firefox 二进制文件并设置我的 EnvVariable DISPLAY 端口。
  • 我发布了更新

标签: java linux selenium jenkins


【解决方案1】:

解压 geckodriver tarball 并指定解压后的 geckodriver 文件的路径。使用您在 pom.xml 中提到的相同版本的 selenium 服务器。 在主机 linux 机器上安装 XVFB

仔细检查 geckodriver 可执行版本是否兼容 在主机上使用 Firefox 浏览器版本。

【讨论】:

    猜你喜欢
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2017-10-08
    • 2014-11-22
    • 1970-01-01
    • 1970-01-01
    • 2017-04-05
    • 2017-01-04
    相关资源
    最近更新 更多