英文:
Program start testNG selenium tests freezes
问题
在使用 TestNG 以编程方式启动 Selenium 测试时,进程会被冻结。所有测试都成功完成,但进程不会结束,并且不会返回任何退出代码。
示例
Main.java
package com.example;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
TestNG testNG = new TestNG();
List<XmlSuite> xmlSuites = new ArrayList<>();
XmlSuite xmlSuite = new XmlSuite();
List<XmlTest> xmlTestList = new ArrayList<>();
XmlTest xmlTest = new XmlTest(xmlSuite);
List<XmlClass> xmlClassList = new ArrayList<>();
xmlClassList.add(new XmlClass(TestOne.class));
xmlTest.setXmlClasses(xmlClassList);
xmlTestList.add(xmlTest);
xmlSuite.setTests(xmlTestList);
xmlSuites.add(xmlSuite);
testNG.setXmlSuites(xmlSuites);
testNG.run();
}
}
TestOne.java
package com.example;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class TestOne {
private WebDriver webDriver;
@BeforeSuite
public void prepare() {
WebDriverManager.chromedriver().setup();
this.webDriver = new ChromeDriver();
}
@Test
public void testOne() {
webDriver.get("http://google.com/");
}
@AfterSuite
public void shutdown() {
if (webDriver != null) {
webDriver.close();
}
}
}
在启动时
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 27439
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Apr 09, 2020 10:12:50 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
但进程不会结束
调试和 Streis 并没有太大帮助。发现 AsyncHttpTimer 挂起,该定时器在本地主机上检查端口。
英文:
When programmatically starting selenium tests using testNG, the process freezes. All tests succeed, but the process does not end and does not return any exit code.
Example
Main.java
package com.example;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
TestNG testNG = new TestNG();
List<XmlSuite> xmlSuites = new ArrayList<>();
XmlSuite xmlSuite = new XmlSuite();
List<XmlTest> xmlTestList = new ArrayList<>();
XmlTest xmlTest = new XmlTest(xmlSuite);
List<XmlClass> xmlClassList = new ArrayList<>();
xmlClassList.add(new XmlClass(TestOne.class));
xmlTest.setXmlClasses(xmlClassList);
xmlTestList.add(xmlTest);
xmlSuite.setTests(xmlTestList);
xmlSuites.add(xmlSuite);
testNG.setXmlSuites(xmlSuites);
testNG.run();
}
}
TestOne.java
package com.example;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;
public class TestOne {
private WebDriver webDriver;
@BeforeSuite
public void prepare() {
WebDriverManager.chromedriver().setup();
this.webDriver = new ChromeDriver();
}
@Test
public void testOne() {
webDriver.get("http://google.com/");
}
@AfterSuite
public void shutdown() {
if (webDriver != null) {
webDriver.close();
}
}
}
At startup
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 27439
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Apr 09, 2020 10:12:50 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
But the process does not end
Debug and Streis did not help much. Found out that AsyncHttpTimer hangs, which checks the port on localhost
答案1
得分: 0
问题出在 Selenium 的 alpha 版本上。
在 GitHub 上有一个问题,链接为 https://github.com/SeleniumHQ/selenium/issues/8159。
切换到稳定版的 3 版本解决了这个问题。
英文:
the problem was in the alpha version of selenium
there is issue on github https://github.com/SeleniumHQ/selenium/issues/8159
transition to 3 stable version solved the problem
专注分享java语言的经验与见解,让所有开发者获益!
评论