在Windows 7上的jenkins中执行selenium(testng)测试时找不到Chrome二进制文件

我有selenium和testng的基本登录测试.从eclipse执行时,它按预期工作并调用Google Chrome.如果从TESTNG命令行执行,它也可以正常工作.

这是我的@Before:

@BeforeMethod
public void setUp() throws Exception { 
 File file = new File("C:/Selenium-driver/chromedriver.exe"); 
 System.setProperty("webdriver.chrome.driver", file.getAbsolutePath()); 
 driver = new ChromeDriver(); 
 driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
 }

但是,如果我通过Jenkins执行相同的测试,它说:

Building in workspace C:\EclipsePortable\Data\workspace\GS_TESTNG 
[G_TESTNG] $cmd /c call C:\Windows\TEMP\hudson9049518275115936054.bat 

C:\EclipsePortable\Data\workspace\G_TESTNG>_test_login_cmd.bat 

C:\EclipsePortable\Data\workspace\G_TESTNG>cd C:\EclipsePortable\Data\workspace\G_TESTNG 

C:\EclipsePortable\Data\workspace\G_TESTNG>java -cp C:\EclipsePortable\Data\workspace\G_TESTNG\lib\*;C:\EclipsePortable\Data\workspace\G_TESTNG\bin org.testng.TestNG testng.xml 
... 
... TestNG 6.9.9 by Cédric Beust (cedric@beust.com) 
... 

[TestNG] Running: 
  C:\EclipsePortable\Data\workspace\G_TESTNG\testng.xml 

Starting ChromeDriver 2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30) on port 36257 
Only local connections are allowed. 
FAILED CONFIGURATION: @BeforeMethod setUp 
org.openqa.selenium.WebDriverException: unknown error: cannot find Chrome binary 
  (Driver info: chromedriver=2.26.436362 (5476ec6bf7ccbada1734a0cdec7d570bb042aa30),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information) 
Command duration or timeout: 664 milliseconds 
Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:15:17' 
System info: host: 'NMTLA12810', ip: '192.168.0.179', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_102' 
Driver info: org.openqa.selenium.chrome.ChromeDriver 
        at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
        at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) 
        at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) 
        at java.lang.reflect.Constructor.newInstance(Unknown Source) 
        at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 
        at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 
        at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 
        at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:249) 
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131) 
        at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:144) 
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:170) 
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:159) 
        at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:116) 
        at com.test.TestA.setUp(TestA.java:33) 
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
        at java.lang.reflect.Method.invoke(Unknown Source) 
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85) 
        at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:510) 
        at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:211) 
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:585) 
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816) 
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124) 
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125) 
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108) 
        at org.testng.TestRunner.privateRun(TestRunner.java:774) 
        at org.testng.TestRunner.run(TestRunner.java:624) 
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:359) 
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354) 
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312) 
        at org.testng.SuiteRunner.run(SuiteRunner.java:261) 
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) 
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) 
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215) 
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1140) 
        at org.testng.TestNG.run(TestNG.java:1048) 
        at org.testng.TestNG.privateMain(TestNG.java:1355) 
        at org.testng.TestNG.main(TestNG.java:1324) 

SKIPPED CONFIGURATION: @AfterClass tearDown 
SKIPPED: testGooglePageTitleInChrome 

=============================================== 
    TestNG Test Group 
    Tests run: 1, Failures: 0, Skips: 1 
    Configuration Failures: 1, Skips: 1 
=============================================== 


=============================================== 
Main Test Suite 
Total tests run: 1, Failures: 0, Skips: 1 
Configuration Failures: 1, Skips: 1 
=============================================== 

[TestNG] Time taken by org.testng.reporters.JUnitReportReporter@7fbe847c: 16 ms 
[TestNG] Time taken by org.testng.reporters.jq.Main@759ebb3d: 78 ms 
[TestNG] Time taken by org.testng.reporters.SuiteHTMLReporter@ba8a1dc: 59 ms 
[TestNG] Time taken by org.testng.reporters.XMLReporter@1c655221: 5 ms 
[TestNG] Time taken by org.testng.reporters.EmailableReporter2@1edf1c96: 16 ms 
[TestNG] Time taken by [FailedReporter passed=0 failed=0 skipped=0]: 0 ms 
Build step 'Exécuter une ligne de commande batch Windows' marked build as failure 
Finished: FAILURE
最佳答案
您的AppData上可能安装了chrome(特别是如果您使用的是Windows 7).我猜Jenkins无法从您的用户目录访问二进制文件.确保Jenkins可以访问Chrome上的目录,或者尝试将chrome重新安装到程序文件目录中.

另一方面,您可以简单地指示Chromedriver在哪里寻找Chrome二进制文件,

ChromeOptions chromeOptions= new ChromeOptions();
chromeOptions.setBinary("C:\\ThePAthtoChrome.exe");

ChromeDriver driver = new ChromeDriver(chromeOptions);

转载注明原文:在Windows 7上的jenkins中执行selenium(testng)测试时找不到Chrome二进制文件 - 代码日志