Cannot find Chrome binary when executing a selenium (testng) test in jenkins on windows 7

82,395

Solution 1

I think you need to install chrome in your instance. for that you follow this basic commands

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
sudo yum install ./google-chrome-stable_current_*.rpm

google-chrome &

Solution 2

It is possible that chrome is installed on your AppData (especially if your are on Windows 7). I am guessing Jenkins could not access the binary from your user directory. Make sure the directory you have Chrome on, is accessible to Jenkins, or try reinstalling chrome again to program files directory.

On the other hand, you can simply instruct Chromedriver where to look for Chrome Binary,

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

ChromeDriver driver = new ChromeDriver(chromeOptions);

Solution 3

The problem is these lines in your code:

File file = new File("C:/Selenium-driver/chromedriver.exe"); 
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath()); 

This is a massive anti-pattern that I regularly see. The webdriver.chrome.driver environmental variable was designed so that you could install your chromedriver binaries on multiple systems in different places, and then set the environmental variable on each system so that if a selenium test is ever run on that system it will automatically pick up the location of the binary.

By hard coding this environmental variable via code you are ignoring any preconfigured environmental variable that was set up when the build agent was built, you are not using it in the way it was intended.

If you insist on doing it this way you should always check to see if the env variable is already set before you set it, this way you will not overwrite the existing env variable. You will also need to make sure that the file path you have hard coded exists on your build agent (the secondary cause of your error is that it doesn't) so that if the env variable isn't set, you are actually setting a valid path to the chromedriver binary.

Solution 4

I just check my chrome version (chrome://settings/help(85.0.4183.83 )) and then downloaded chromedriver.exe accordingly (https://chromedriver.chromium.org/downloads)and given new chrome driver.exe path...-This solves my same issue

Solution 5

I had the same problem, I was able to execute selenium scripts through local but when tried executing on Jenkins got error "unknown error: cannot find Chrome binary".

Here is my solution which worked on Jenkins server: 1. Download and enable Xvfb before start of selenium script on jenkins. For more info read here https://wiki.jenkins.io/display/JENKINS/Xvfb+Plugin 2. I am having ubuntu 16.xx os on local and jenkins server. 3. Try to keep chromedriver in "usr/bin" folder only otherwise you may get error.

try {

            if(service == null){
                service = new ChromeDriverService.Builder()
                        .usingDriverExecutable(new File("/usr/bin/chromedriver"))// set the chromedriver path
                       .usingAnyFreePort()
                        .withEnvironment(ImmutableMap.of("DISPLAY", ":15"))
                        .withSilent(true)
                        .build();
                service.start();
            }

            System.out.println("Reading chrome driver");
            System.setProperty("webdriver.chrome.driver","/usr/bin/chromedriver");
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--headless");
            WebDriver driver = new ChromeDriver(chromeOptions);
            driver.get("https://google.com");
            driver.quit();
 }
        catch(Exception ex){
        System.out.println(ex.getMessage());
        }

Share:
82,395

Related videos on Youtube

Templog Log
Author by

Templog Log

Updated on December 17, 2021

Comments

  • Templog Log
    Templog Log over 2 years

    I have a basic login test with selenium and testng. When executed from eclipse, it works as expected and invoke Google Chrome. If executed from TESTNG command line, it works fine too.

    This is my @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); 
     }
    

    However, if I execute the same test thru Jenkins it says:

    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 ([email protected]) 
    ... 
    
    [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
    
  • Templog Log
    Templog Log over 7 years
    Hi Prageeth, i added otpions for chrome as you mentioned and i still get the same error. and i do not find the folder AppData.
  • Templog Log
    Templog Log over 7 years
    How can i know where jenkins is looking for chrome.exe ?
  • Prageeth Saravanan
    Prageeth Saravanan over 7 years
    I am guessing you probably configured Jenkins as a WIndows service running under Local System account. You can launch Your AppData folder by simply entering %APPDATA% in run. The AppData for Local System user account is at C:\Windows\System32\Config\systemprofile\AppData
  • Prageeth Saravanan
    Prageeth Saravanan over 7 years
    Also, make sure the path you gave under chromeOptions.setBinary("C:\\ThePAthtoChrome.exe"); is valid. It's highly unlikely that selenium would say Binary not found if you give a Valid Chrome exe path
  • Templog Log
    Templog Log over 7 years
    i don't understand, should i give the path to chrome.exe or chromedriver.exe ?
  • Templog Log
    Templog Log over 7 years
    i found solution with this: ChromeOptions options = new ChromeOptions(); options.setBinary("C:/Program Files/Google/Chrome/Application/chrome.exe"); System.setProperty("webdriver.chrome.driver", "C:/chromedriver.exe"); driver = new ChromeDriver(options);
  • Prageeth Saravanan
    Prageeth Saravanan over 7 years
    Binary is for chrome not the driver.
  • Prageeth Saravanan
    Prageeth Saravanan over 7 years
    Right, that's what I've said in the answer. Glad it helped !!
  • Davide Cannizzo
    Davide Cannizzo almost 6 years
    Explain what you're doing, @Aastha Chopra.
  • Bibimission
    Bibimission almost 5 years
    Yup, that's it! I had the same error until I installed Google Chrome
  • Joe Taras
    Joe Taras over 4 years
    Please add further inofrmation about your answer. The question is very old, so an answer after some years must to give an added value
  • puerile
    puerile over 3 years
    Haha! This was also my problem. I forgot I didn't have Chrome installed. LOL!
  • Sugandha Mishra
    Sugandha Mishra over 2 years
    I was able to download the chrome but could not find the location where it is installed. That path is needed to set the CHROME_BIN variable. Any inputs?