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
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());
}
ChromeOptions options = new ChromeOptions();
if(System.getProperty("os.name").equals("Windows Server 2012 R2")){
File ChromeExePath =new File("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe");
if (ChromeExePath.exists()){
options.setBinary(ChromeExePath);
}
}
ChromeDriver driver = new ChromeDriver(options);
Please check your binary path if still not works then
you should have installed chrome in your machine
and your chromedriver.exe version should accordingly (like if you have chrome 87.0 ver then download chromedriver.exe for ver 87.0)
download form it from https://chromedriver.chromium.org/downloads
and given new chromedriver.exe path to your script.
This works fine for me
It worked for me. I have coded as below
ChromeOptions chromeOptions= new ChromeOptions();
chromeOptions.setBinary("C:\\Users\\kannanu\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");
System.setProperty("webdriver.chrome.driver",ChromeExePath);
ChromeDriver driver = new ChromeDriver(chromeOptions);
driver.get("http://newtours.demoaut.com/");
I was having similar issue in JS, it got fixed after installing npm i chromedriver
ChromeOptions chromeOptions= new ChromeOptions();
chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome Beta\\Application\\chrome.exe");
System.setProperty("webdriver.chrome.driver","C:\\STUDY\\Selenium\\CHROMEDRIVERS\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver(chromeOptions);
driver.get("http://newtours.demoaut.com/");