java - 如何远程运行 TestNG Selenium 测试?

标签 java selenium intellij-idea command-line testng

我有带有 TestNG 测试和 Selenium WebDriver 的 Java 项目。目前,我只能从 IDE 本地运行它们。

如何实现一个允许我在服务器上使用 post/get 请求运行测试的系统?

例如,带有按钮的页面:单击按钮时,测试应该开始在服务器上执行,并进行所有 WebDriver 操作,完成后,我会收到答案。

首先,我尝试从命令行调用我的testng.xml。它有效,但看起来命令行不是,我可以通过简单的 FirefoxDriver 远程使用,所以我开始研究“RemoteWebDriver”功能。

然后我下载了“selenium-server-standalone-3.4.0”并启动了服务器和节点,但每次脚本结束时都会出现异常:

org.openqa.selenium.WebDriverException: The path to the driver executable must be set by the webdriver.gecko.driver system property

我添加了必需的属性,但它仍然不起作用(同样的问题)。这是一段导致异常的代码:

@Test
public static void main() throws Exception {
    URL server = new URL("http://127.0.0.1:4444/wd/hub");
    DesiredCapabilities capabilities = DesiredCapabilities.firefox();
    System.out.println("Connecting to " + server);
    WebDriver driver = new RemoteWebDriver(server, capabilities);
    System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
    driver.get("http://www.google.com");
    driver.quit();
}

如您所见,系统属性已设置(geckodriver.exe 存在于目录中,并且可以与 FirefoxDriver 配合使用,但不能与 RemoteWebDriver 配合使用)。

我不明白,我做错了什么,甚至不确定我的工作方向是否正确。你能帮我一下吗?

尝试添加@ekostadinov 提议的参数。以下是我启动集线器和节点的方法:

java -Dwebdriver.gecko.driver="geckodriver.exe" -jar lib\selenium-server-standalone-3.4.0.jar -role hub

java -jar lib\selenium-server-standalone-3.4.0.jar -role node  -hub http://localhost:4444/grid/register

仍然不起作用,但异常不同:

org.openqa.selenium.SessionNotCreatedException: Unable to find a matching set of capabilities

更新

改变了这个

DesiredCapabilities capabilities = DesiredCapabilities.firefox();

至此

DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setBrowserName("firefox");

而且它成功了。谢谢大家的帮助!

最佳答案

没有必要

implement a system that allows me to run a test with a post/get request on server

除非您确实想要这样做。 Continuous_integration服务器开箱即用地为您完成所有这些工作以及更多其他工作。例如Jenkins (非常适合基于JAVA的解决方案)提供非常方便Selenium plugin ,这可以帮助您管理网格。

这里需要注意一件事 - 在服务器上启动 Hub 时,您应该传递其他参数,如下所示:

 java -Dwebdriver.chrome.driver="/full/path/to/chromedriver" -Dwebdriver.gecko.driver="/full/path/to/geckodriver" -jar selenium-server-standalone-3.4.0.jar -role hub

关于java - 如何远程运行 TestNG Selenium 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45147477/

相关文章:

java - 将 int 值分配给 JCheckBox

java - 查找网页上最常见的单词(使用 Jsoup)?

c# - 数据重新发布后无法处理模态警报窗口(使用 Selenium webdriver C# 和 IE)

scala - 尝试在 IntelliJ 中添加 Spark 依赖项时出现 OpenJDK Server VM 和 Unresolved 依赖项警告

java - 使用 new 语句将值传递给构造函数

java - 如何确定 Java 连接池是否与 URLConnection 一起工作?

python - 更改 Geckodriver for Python Selenium 的日志级别

ruby-on-rails - 如何使用Webkit或Selenium驱动程序填充来自 capybara 的ckeditor

java - 在 Intellij 中将 Spock 测试添加到 Java 项目

javascript - 从 Karma/Jasmine 测试加载外部文件