java - Java 项目中的 Selenium Chromedriver 超时问题

标签 java linux eclipse selenium selenium-chromedriver

我不知道如何正确配置 selenium 测试以在 Ubuntu VM 中运行。 在我有 eclipse IDE 的 Windows 上,虽然可以正常工作。 以下是 testNG 类的部分 java 代码:

@BeforeClass(alwaysRun = true)
public void setUp() throws Exception {   

    if (SystemUtils.IS_OS_LINUX)      
        System.setProperty("webdriver.chrome.driver", "/usr/bin/selenium/chromedriver/chromedriver");

    if(SystemUtils.IS_OS_WINDOWS)
        System.setProperty("webdriver.chrome.driver", ""+System.getProperty("user.dir").toString()+"/lib/chromedriver.exe");

    driver = new ChromeDriver();          

    System.out.println(System.getProperty("portletToTest"));  
    baseUrl = "http://www.google.it";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void test() throws Exception {
    driver.get(baseUrl + "/");

我不断得到的错误是这个,来自 testNG 报告(除了 localhost: 的端口,每次运行它时都会有所不同)

org.openqa.selenium.WebDriverException: Timed out waiting for driver server to start.
Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700'
System info: host: 'esvn', ip: '192.168.5.182', os.name: 'Linux', os.arch: 'amd64', os.version: '3.5.0-54-generic', java.version: '1.8.0_121'
Driver info: driver.version: ChromeDriver
    at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:178)
    at org.openqa.selenium.remote.service.DriverService.start(DriverService.java:166)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:78)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:241)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:128)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:141)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:174)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:163)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:120)
    at Automation.profileuserportlet.setUp(Unknown Source)
    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.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:515)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:217)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:144)
    at org.testng.internal.TestMethodWorker.invokeBeforeClassMethods(TestMethodWorker.java:169)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:756)
    at org.testng.TestRunner.run(TestRunner.java:610)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.TestNG.privateMain(TestNG.java:1434)
    at org.testng.TestNG.main(TestNG.java:1403)
Caused by: org.openqa.selenium.net.UrlChecker$TimeoutException: Timed out waiting for [http://localhost:17352/status] to be available after 20008 ms
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:107)
    at org.openqa.selenium.remote.service.DriverService.waitUntilAvailable(DriverService.java:175)
    ... 34 more
Caused by: com.google.common.util.concurrent.UncheckedTimeoutException: java.util.concurrent.TimeoutException
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:143)
    at org.openqa.selenium.net.UrlChecker.waitUntilAvailable(UrlChecker.java:80)
    ... 35 more
Caused by: java.util.concurrent.TimeoutException
    at java.util.concurrent.FutureTask.get(FutureTask.java:205)
    at com.google.common.util.concurrent.SimpleTimeLimiter.callWithTimeout(SimpleTimeLimiter.java:130)
    ... 36 more

我已经仔细检查了linux上的chromedriver文件路径,没问题,文件有可执行权限。 我还必须在 linux 机器上启动 chromedriver 可执行文件吗?除了运行已编译的 java 项目,我还需要做其他事情吗?

最佳答案

您的 chrome 驱动程序应该具有与 chrome 浏览器相匹配的正确版本。

关于java - Java 项目中的 Selenium Chromedriver 超时问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42007934/

相关文章:

java - 如何使用扫描仪 Java 检查一行是否为空,如果是,则跳过它

linux - 将数组名称传递给 bash 中的函数

c++ - 如何在 Linux 中获取 C/C++ 中的用户名?

eclipse-搜索一次只能打开一个文件

Android:未找到兼容的目标,但 AVD 可用

java - 我的 java GUI 代码出了什么问题,Eclipse 没有响应?

java - 如何使用 Guava Multimap 的 replaceValues 方法?

JAVA - WELD 装饰器错误

java - 使用cameltestsupport进行Camel单元测试,模板始终为空

linux - 转换 d.m.Y h :m:s to Y-d-m h:m:s for a large file in linux