package firstSeleniumTesting;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class testingAutomation {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.drive", "C:\\SeleniumDriver\\chromedriver_win32");
WebDriver driver = new ChromeDriver(); //Launches Firefox
driver.close();
}
}
I get the following error
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from https://chromedriver.storage.googleapis.com/index.html
at org.openqa.selenium.internal.Require$StateChecker.nonNull(Require.java:280)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:132)
at org.openqa.selenium.chrome.ChromeDriverService.access$000(ChromeDriverService.java:38)
at org.openqa.selenium.chrome.ChromeDriverService$Builder.findDefaultExecutable(ChromeDriverService.java:231)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:434)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:127)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:46)
at firstSeleniumTesting.testingAutomation.main(testingAutomation.java:13)
Add the .exe in the chromedriver
Use like this
System.setProperty("webdriver.chrome.driver", "C:\\SeleniumDriver\\chromedriver_win32.exe");
WebDriver driver = new ChromeDriver();
driver.close();
I have created a java/selenium/cucumber automation framework with 4 feature files each testing different functional area's.
I can run each of these within Eclipse as feature files and they all run fine.
However I am trying to figure out how I can run them all as a JUnit test so that I can generate the extent reports.
I run the mainRunner class as a Junit test like ..
The first feature runs fine but then the next three do not. A chrome browser is opened for each of these but
the site is not navigated to. The error
'org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?'
is shown in the console logs.
So is this issue to do with how I have set my feature files up to use the startBrowser, initBrowser and closeBrowser methods. Which are
defined in a seperate class 'BrowserInitTearDownStepDefinitions'?
All my feature files look like ..
Feature: Update a Computer in Database
-- left out details
#StartBrowser
Scenario: Start browser session
When I initialize driver
Then open browser
#MyTest4
Scenario Outline: Update a computer
-- left our details
Examples:
-- left out details
#CloseBrowser
Scenario: Close the browser
Then close browser
I have had a look at the other posts related to this message that reference the same error, but can't see a clear cut solution.
As the error would indicate I seem to be quitting the driver but then not reinitialising it for the next test. Although Im not sure why this
would be the case given that each feature file has the #StartBrowser and #CloseBrowser tags which should execute these methods. Below is my BrowserInitTearDownStepDefinitions class.
public class BrowserInitTearDownStepDefinitions {
//Initialises Chrome browser
#When("^I initialize driver$")
public void initializeDriver() {
System.out.println("initializeDriver runs");
System.setProperty("webdriver.chrome.driver","C:\\xxxx\\CucumberAutomationFramework\\src\\test\\java\\cucumberAutomationFramework\\resources\\chromedriver.exe");
WebDriverUtils.setDriver(new ChromeDriver());
}
//Opens Chrome browser and clicks in search input box
#Then("^open browser$")
public void openBrowser() {
System.out.println("Open Browser runs");
WebDriver driver = WebDriverUtils.getDriver();
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(60, TimeUnit.SECONDS);
driver.get("https://computer-database.gatling.io/computers");
}
//Closes browser after all scenarios run and deletes cookies
#Then("^close browser$")
public void closeBrowser() {
System.out.println("Close Browser runs");
WebDriver driver = WebDriverUtils.getDriver();
driver.manage().deleteAllCookies();
driver.quit();
driver =null;
}
}
I create a new instance of the webdriver within my stepDefs file with ..
WebDriver driver = WebDriverUtils.getDriver()
The webdriver utils class code is ..
package cucumberAutomationFramework.utilityClasses;
import org.openqa.selenium.WebDriver;
public class WebDriverUtils {
private static WebDriver driver;
public static void setDriver(WebDriver webDdriver) {
if (driver == null) {
driver = webDdriver;
}
}
public static WebDriver getDriver() {
if (driver == null) {
throw new AssertionError("Driver is null. Initialize driver before calling this method.");
}
return driver;
}
}
Any advice would be much appreciated. Thanks.
selenium grid connection with auto it not working ?
#daluudaluu/PartialSeleniumGridIntegrationWithAutoItExample.java
Last active a year ago
Embed
Download ZIP
Code Revisions 2 Forks 1
Partial Selenium Grid integration support with tools like AutoIt, Sikuli, etc.
Raw
PartialSeleniumGridIntegrationWithAutoItExample.java
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.*;
import java.net.URL;
public class DemoTest {
public WebDriver driver;
public DesiredCapabilities capabilities;
#Before
public void setUp() throws Exception {
capabilities = DesiredCapabilities.firefox();
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub" ), capabilities);
}
#After
public void tearDown() throws Exception {
driver.quit();
}
#Test
public void test() throws Exception {
// Use RemoteWebDriver, grab actual node host info
driver.get("http://www.google.com");
String sessionId = ((RemoteWebDriver) driver).getSessionId().toString();
//grid info extractor from: https://gist.github.com/krmahadevan/1766772
String nodeHost = GridInfoExtracter.getHostNameAndPort("localhost", 4444, sessionId)[0];
System.out.println("Extracted hostname: "+nodeHost);
// Now use node host info to handle running AutoIt on that specific node, assuming all needed files deployed to all nodes
// Case 1 - PSExec.exe from Windows host (that is executing this Selenium code) to Selenium node that is Windows host
//String psexecCmd = "C:\\LocalMachinePathTo\\psexec.exe \\\\%s -u %s -p %s -i C:\\GridNodeMachinePathTo\\autoitCompiledScript.exe";
//Process p = Runtime.getRuntime().exec(String.format(psexecCmd,nodeHost,"jdoe","hisPassword"));
//p.waitFor();
// Case 2 - winexe from *nix host (that is executing this Selenium code) to Selenium node that is Windows host
// Example reference: http://secpod.org/blog/?p=661
//String winexeCmd = "/LocalMachinePathTo/winexe -U domainName/jdoe%hisPassword //"+nodeHost+" C:\\GridNodeMachinePathTo\\autoitCompiledScript.exe";
//Process p = Runtime.getRuntime().exec(winexeCmd);
//p.waitFor();
// Case 3 - have SSH installed on Windows-based Selenium nodes, now just connect to node host via SSH to run a command, i.e. execute AutoIt script binary
// no sample code given for Java, but maybe this gives you ideas on Java SSH connection:
// http://stackoverflow.com/questions/995944/ssh-library-for-java
// Case 4 - if you have implemented a custom web service (XML-RPC/SOAP/REST) for AutoIt that listens for requests/commands
// just connect to it via (Java) HTTP library to "http://"+nodeHost+"/someWebServicePath/someCommand" (via GET or POST)
// details not covered, this is just an example. Most people won't be taking this route due to customization & complexity in
// building the web service first
// Case 5 - using AutoItDriverServer that is also running on Selenium nodes
//DesiredCapabilities autoitCapabilities = new DesiredCapabilities();
//autoitCapabilities.setCapability("browserName", "AutoIt");
//WebDriver autoitDriver = new RemoteWebDriver(new URL("http://"+nodeHost+":4723/wd/hub"), autoitCapabilities);
//autoitDriver.findElement(By.id("133")).click();
//and whatever other AutoItX commands to call that you normally have in the AutoIt script that you compile into binary
//for more ideas on AutoIt commands issued as WebDriver commands with respect to Selenium integration, see:
//https://github.com/daluu/AutoItDriverServer/blob/master/sample-code/SeleniumIntegrationWithAutoItDriver.py
//or if you are old school, and want to do that same approach even with AutoItDriverServer,
// assuming you have first set AutoItScriptExecuteScriptAsCompiledBinary to True in autoit_options.cfg file before starting AutoItDriverServer:
//((JavascriptExecutor) autoitDriver).executeScript("C:\\GridNodeMachinePathTo\\autoitCompiledScript.exe");
// Case for Sikuli integration, using https://github.com/enix12enix/sikuli-remote-control
//RemoteScreen rs = new RemoteScreen(nodeHost);
//rs.setMinSimilarity(0.9);
//rs.click("D://test.png");
}
}
I am trying to run my first webdriver script in eclipse. using jre1.8.0_1111.
I used the following code but it shows error.please help me with the code.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Trial {
static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String baseUrl = "google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Naik\\Downloads\\geckodriver-v0.11.1-win64\\geckodriver.exe");
driver.get(baseUrl);
}
Error stack
Exception in thread "main" java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.gecko.driver system property; for more information, see github.com/mozilla/geckodriver. The latest version can be downloaded from github.com/mozilla/geckodriver/releases
Download the geckodriver from the below URL and save it on your local machine.
https://github.com/mozilla/geckodriver/releases
Then set the right path where the geckodriver.exe is saved.Moreover the set property must be used before declaring the driver!
public class Trial {
public static void main(String[] args) {
String baseUrl = "google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Users\\Naik\\Downloads\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
}
You need to first download GeckoDriver. After that, you can either add it to the PATH variable in environment variables sections, or you can set the path using "webdriver.gecko.driver" property. Check the below article for the steps -
http://www.automationtestinghub.com/selenium-3-0-launch-firefox-with-geckodriver/
Also, please make sure that you are using the latest versions of Selenium, GeckoDriver and Firefox.
If you don't want to download geodriver, the other way is
Downgrade the Firefox browser version to 44 or more lesser and run your test.
https://ftp.mozilla.org/pub/firefox/releases/
Then you don't have to use gecko driver.
To downgrade firefox to lower version, first uninstall Firefox and the download and install from the link above mentioned
I tried this
WebDriver driver = new ChromeDriver();
But I'm getting the error as
Failed tests: setUp(com.TEST): The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see code here . The latest version can be downloaded from this link
How can I make Chrome test the Selenium WebDriver test cases?
You need to download the executable driver from:
ChromeDriver Download
Then use the following before creating the driver object (already shown in the correct order):
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
This was extracted from the most useful guide from the ChromeDriver Documentation.
Download the updated version of the Google Chrome driver from Chrome Driver.
Please read the release note as well here.
If the Chrome browser is updated, then you need to download the new Chrome driver from the above link, because it would be compatible with the new browser version.
public class chrome
{
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
}
}
You should download the chromeDriver in a folder, and add this folder in your PATH environment variable.
You'll have to restart your console to make it work.
If you're using Homebrew on a macOS machine, you can use the command:
brew tap homebrew/cask && brew cask install chromedriver
It should work fine after that with no other configuration.
You need to install the Chrome driver. You can install this package using NuGet as shown below:
You can use the below code to run test cases in Chrome using Selenium WebDriver:
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class ChromeTest {
/**
* #param args
* #throws InterruptedException
* #throws IOException
*/
public static void main(String[] args) throws InterruptedException, IOException {
// Telling the system where to find the Chrome driver
System.setProperty(
"webdriver.chrome.driver",
"E:/chromedriver_win32/chromedriver.exe");
WebDriver webDriver = new ChromeDriver();
// Open google.com
webDriver.navigate().to("http://www.google.com");
String html = webDriver.getPageSource();
// Printing result here.
System.out.println(html);
webDriver.close();
webDriver.quit();
}
}
Find the latest version of chromedriver here.
Once downloaded, unzip it at the root of your Python installation, e.g., C:/Program Files/Python-3.5, and that's it.
You don't even need to specify the path anywhere and/or add chromedriver to your path or the like.
I just did it on a clean Python installation and that works.
Download the latest version of the Chrome driver and use this code:
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(10000);
driver.get("http://stackoverflow.com");
On Ubuntu, you can simply install the chromium-chromedriver package:
apt install chromium-chromedriver
Be aware that this also installs an outdated Selenium version. To install the latest Selenium:
pip install selenium
All the previous answers are correct. Following is the little deep dive into the problem and solution.
The driver constructor in Selenium for example
WebDriver driver = new ChromeDriver();
searches for the driver executable, in this case the Google Chrome driver searches for a Chrome driver executable. In case the service is unable to find the executable, the exception is thrown.
This is where the exception comes from (note the check state method)
/**
*
* #param exeName Name of the executable file to look for in PATH
* #param exeProperty Name of a system property that specifies the path to the executable file
* #param exeDocs The link to the driver documentation page
* #param exeDownload The link to the driver download page
*
* #return The driver executable as a {#link File} object
* #throws IllegalStateException If the executable not found or cannot be executed
*/
protected static File findExecutable(
String exeName,
String exeProperty,
String exeDocs,
String exeDownload) {
String defaultPath = new ExecutableFinder().find(exeName);
String exePath = System.getProperty(exeProperty, defaultPath);
checkState(exePath != null,
"The path to the driver executable must be set by the %s system property;"
+ " for more information, see %s. "
+ "The latest version can be downloaded from %s",
exeProperty, exeDocs, exeDownload);
File exe = new File(exePath);
checkExecutable(exe);
return exe;
}
The following is the check state method which throws the exception:
/**
* Ensures the truth of an expression involving the state of the calling instance, but not
* involving any parameters to the calling method.
*
* <p>See {#link #checkState(boolean, String, Object...)} for details.
*/
public static void checkState(
boolean b,
#Nullable String errorMessageTemplate,
#Nullable Object p1,
#Nullable Object p2,
#Nullable Object p3) {
if (!b) {
throw new IllegalStateException(format(errorMessageTemplate, p1, p2, p3));
}
}
SOLUTION: set the system property before creating driver object as follows.
System.setProperty("webdriver.gecko.driver", "path/to/chromedriver.exe");
WebDriver driver = new ChromeDriver();
The following is the code snippet (for Chrome and Firefox) where the driver service searches for the driver executable:
Chrome:
#Override
protected File findDefaultExecutable() {
return findExecutable("chromedriver", CHROME_DRIVER_EXE_PROPERTY,
"https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver",
"http://chromedriver.storage.googleapis.com/index.html");
}
Firefox:
#Override
protected File findDefaultExecutable() {
return findExecutable(
"geckodriver", GECKO_DRIVER_EXE_PROPERTY,
"https://github.com/mozilla/geckodriver",
"https://github.com/mozilla/geckodriver/releases");
}
where CHROME_DRIVER_EXE_PROPERTY = "webdriver.chrome.driver"
and GECKO_DRIVER_EXE_PROPERTY = "webdriver.gecko.driver"
Similar is the case for other browsers, and the following is the snapshot of the list of the available browser implementation:
To run Selenium WebDriver test cases in Chrome, follow these steps:
First of all, set the property and Chrome driver path:
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
Initialize the Chrome Driver's object:
WebDriver driver = new ChromeDriver();
Pass the URL into the get method of WebDriver:
driver.get("http://www.google.com");
I included the binary into my projects resources directory like so:
src\main\resources\chrome\chromedriver_win32.zip
src\main\resources\chrome\chromedriver_mac64.zip
src\main\resources\chrome\chromedriver_linux64.zip
Code:
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.*;
import java.nio.file.Files;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
public WebDriver getWebDriver() throws IOException {
File tempDir = Files.createTempDirectory("chromedriver").toFile();
tempDir.deleteOnExit();
File chromeDriverExecutable;
final String zipResource;
if (SystemUtils.IS_OS_WINDOWS) {
zipResource = "chromedriver_win32.zip";
} else if (SystemUtils.IS_OS_LINUX) {
zipResource = "chromedriver_linux64.zip";
} else if (SystemUtils.IS_OS_MAC) {
zipResource = "chrome/chromedriver_mac64.zip";
} else {
throw new RuntimeException("Unsuppoerted OS");
}
try (InputStream is = getClass().getResourceAsStream("/chrome/" + zipResource)) {
try (ZipInputStream zis = new ZipInputStream(is)) {
ZipEntry entry;
entry = zis.getNextEntry();
chromeDriverExecutable = new File(tempDir, entry.getName());
chromeDriverExecutable.deleteOnExit();
try (OutputStream out = new FileOutputStream(chromeDriverExecutable)) {
IOUtils.copy(zis, out);
}
}
}
System.setProperty("webdriver.chrome.driver", chromeDriverExecutable.getAbsolutePath());
return new ChromeDriver();
}
Download the EXE file of chromedriver and extract it in the current project location.
Here is the link, where we can download the latest version of chromedriver:
https://sites.google.com/a/chromium.org/chromedriver/
Here is the simple code for the launch browser and navigate to a URL.
System.setProperty("webdriver.chrome.driver", "path of chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://any_url.com");