import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class sasas {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.manage().window().maximize();
driver.get(appUrl);
System.out.println("Test script executed successfully.");
driver.close();
}
}
this is the sample code i am trying. when i run i get the error message as "The driver executable does not exist: D:\Firefox\geckodriver.exe" please help me to proceed. i added the location in environmental variable then too i get this error . PATH i added as D:\Samplecode.
kindly help me
(1) To use gecko driver, make sure you are using Firefox version 55 and above to get better gecko Web-Driver feature support, find out more here
(2) Perhaps, you should downgrade Selenium to a lower version i.e. version 2.53.1. Selenium version 2.53.1 runs perfectly on Firefox 47.0.1 and lower, does not require using web driver API. I have ran your code against this and it worked fine.
public class Sasas {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
driver.manage().window().maximize();
driver.get(appUrl);
System.out.println("Test script executed successfully.");
driver.close();
}
}
Use the relative path:
JAVA
1.- In your project, create the Drivers/Win/Firefox/geckodriver.exe folder and add your .exe
2.- Replace:
System.setProperty("webdriver.gecko.driver","D:\\Firefox\\geckodriver.exe");
For:
String path = System.getProperty("user.dir") + "/Drivers/Win/Firefox/Geckodriver.exe";
System.setProperty("webdriver.gecko.driver", path);
Note: using the relative path is the most optimal
Related
I'm currently learning selenium. I have followed a step by step walkthrough on how to start selenium-chromedriver. However I am experiencing this error here and I am have no idea how to solve it.
The following are the source code the walkthrough has given me.
package driverUtilities;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class DriverUtilities {
private static DriverUtilities instanceOfDriverUtilities;
private WebDriver driver;
public static DriverUtilities getInstanceOfDriverUtilities() {
if (instanceOfDriverUtilities == null) {
instanceOfDriverUtilities = new DriverUtilities();
}
return instanceOfDriverUtilities;
}
public WebDriver getDriver() {
if (driver == null) {
CreateDriver();
}
return driver;
}
private String GetDriverName() {
Properties config = new Properties();
String driverName = "";
try {
config.load(new FileInputStream("config.properties"));
} catch (FileNotFoundException e) {
System.out.println("Config file is not present");
e.printStackTrace();
} catch (IOException e) {
System.out.println("Error when loading config file");
e.printStackTrace();
}
for (String key : config.stringPropertyNames()) {
if (key.equals("browser")) {
driverName = config.getProperty(key);
}
}
return driverName;
}
private void CreateDriver() {
String driverName = GetDriverName();
switch (driverName) {
case "Google Chrome":
System.setProperty("webdriver.chrome.driver", "chromedriver");
this.driver = new ChromeDriver();
break;
case "Firefox":
System.setProperty("webdriver.gecko.driver", "geckodriver.exe");
this.driver = new FirefoxDriver();
break;
default:
break;
}
}
}
This is the Driver Utilities class
package test;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import driverUtilities.DriverUtilities;
public class Mod08_Slide_16_Navigation_Commands {
#Test
public void navigationCommands() {
DriverUtilities myDriverUtilities = new DriverUtilities();
WebDriver driver = myDriverUtilities.getDriver();
System.out.println("Start the Test Case");
// Load the website - http://www.bbc.co.uk
driver.get("http://www.bbc.co.uk");
System.out.println("\nLoad the website - http://www.bbc.co.uk");
// Refresh the page
driver.navigate().refresh();
System.out.println("\nRefresh the page");
// Load the website - http://www.google.co.uk
driver.get("http://www.google.co.uk");
System.out.println("\nLoad the website - http://www.google.co.uk");
// Go back to the website - http://www.bbc.co.uk
driver.navigate().back();
System.out.println("\nGo back to the website - http://www.bbc.co.uk");
// Go forward to the website - http://www.google.co.uk
driver.navigate().forward();
System.out.println("\nGo forward to the website - http://www.google.co.uk");
// Close the browser window
driver.close();
System.out.println("\nClose the browser window");
System.out.println("\nEnd of Test Case \"Navigation Commands\"");
}
}
This is the java test class I am trying to make work of
# Change the browser to use for testing purposes, accepted values are Google Chrome and Firefox
browser=Google Chrome
I also have the pom.xml file and a config.properties file and will share it if its required.
Note: My assumptions is that the setproperty function is not locating the correct path to chromedriver. However, I'm a arch linux user thus I'm not sure if the value inside the setproperty function is set to "chromedriver" or "/usr/bin/chromedriver"
Note 1: I have navigated to the .metadata of the Eclipse IDE and deleted it after reading some stackoverflow posts. However, this did not work*
Note 2: I am completely new to Selenium however, I have some experiences with Java and understand that nullpointerexception occurs when I declare a variable but did not create an object and assign that variable to it.
Edit 1: I have included the config.properties as specified
One suggestion I have is to hard-code the creation of the driver (without config.properties) and once you have the code working, you can work on optimizations such as dynamically creating the web driver based on a key value like you are doing here. To do this, simply replace this line
WebDriver driver = myDriverUtilities.getDriver();
with
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\WebDrivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Obviously, make sure the path to the chromedriver.exe is resolved correctly for your system. By the way, unless your web drivers are in the root folder of your code project, that path is not correct. I will double check that is the case.
I ran your code with this recommendation and it worked. That tells me either your web driver path is not correct, or there is something funky with your config.properties.
Lastly, this loop isn't correct either
for (String key : config.stringPropertyNames()) {
if (key.equals("browser")) {
driverName = config.getProperty(key);
}
}
You don't want to loop through all your browser keys. If you have more than one "key", it will iterate through them and return the last one found. Instead, you need to call getProperty() once your properties file is loaded:
String propValue = props.getProperty("browser");
Then, once you have the correct propValue, you should pass it to your switch to properly instantiate the web driver.
switch(propValue) {
case "chrome":
this.driver = new ChromeDriver();
break;
case "firefox":
this.driver = new GeckoDriver();
break;
default:
// throw some exception here
throw new IllegalArgumentException(propValue + " is not a supported browser.");
}
My code:
package pak0310;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class class0310
{
public static void main(String[] args)
{
// objects and variables instantiation
WebDriver driver = new FirefoxDriver();
String appUrl = "https://accounts.google.com";
// launch the firefox browser and open the application url
driver.get(appUrl);
// maximize the browser window
driver.manage().window().maximize();
// declare and initialize the variable to store the expected title of the webpage.
String expectedTitle = " Sign in - Google Accounts ";
// fetch the title of the web page and save it into a string variable
String actualTitle = driver.getTitle();
// compare the expected title of the page with the actual title of the page and print the result
if (expectedTitle.equals(actualTitle))
{
System.out.println("Verification Successful - The correct title is displayed on the web page.");
}
else
{
System.out.println("Verification Failed - An incorrect title is displayed on the web page.");
}
// enter a valid username in the email textbox
WebElement username = driver.findElement(By.id("Email"));
username.clear();
username.sendKeys("TestSelenium");
// enter a valid password in the password textbox
WebElement password = driver.findElement(By.id("Passwd"));
password.clear();
password.sendKeys("password123");
WebElement SignInButton = driver.findElement(By.id("signIn"));
SignInButton.click();
// close the web browser
driver.close();
System.out.println("Test script executed successfully.");
// terminate the program
System.exit(0);
}
}
Error:
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 https://github.com/mozilla/geckodriver. The latest version can be downloaded from https://github.com/mozilla/geckodriver/releases
at com.google.common.base.Preconditions.checkState(Preconditions.java:754)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:124)
at org.openqa.selenium.firefox.GeckoDriverService.access$100(GeckoDriverService.java:41)
at org.openqa.selenium.firefox.GeckoDriverService$Builder.findDefaultExecutable(GeckoDriverService.java:115)
at org.openqa.selenium.remote.service.DriverService$Builder.build(DriverService.java:329)
at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:207)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:103)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:99)
at pak0310.class0310.main(class0310.java:22)
...
In this similar question (https://stackoverflow.com/a/38752315/8195985) user Paras answered:
You are using latest version of Selenium WebDriver i.e. Selenium 3.x, this version of webdriver doesn't support direct firefox launch. You have to set the SystemProperty for webdriver.gecko.driver.
Replace the Code:-
WebDriver driver; =new FirefoxDriver();
With This code:-
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "<Path to your WebDriver>");
driver =new FirefoxDriver();
Maybe use that will help you.
Download geckodriver from https://github.com/mozilla/geckodriver/releases
unzip folder you will get like this geckodriver.exe
Copy that paste in some where and copy the complete path with driver.
use below code in <path> place paste your path
System.setProperty("webdriver.gecko.driver", "your path");
WebDriver driver = new FirefoxDriver();
This error suggesting to use of geckodriver (post Selenium 3), to launch Firefox. You can refer
How to use the gecko executable with Selenium
When I execute the jenkins job, the selenium test always fail with this error
java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property
or when I set the driver path
not found /var/jenkins/.../C:/selenium/drivers/chromedrive
I have the follow environment
1 jenkins server and selenium hub running on linux
1 selenium node running on Windows.
The selenium node is running with the follow line
java -Dwebdriver.chrome.driver=C:/selenium/drivers/chromedriver.exe -jar selenium-server-standalone-2.53.1.jar -port 5556 -role node -hub http://192.168.15.99:4444/grid/register -browser "browserName=chrome, version=ANY, maxInstances=10, platform=WINDOWS"
Selenium hub and node can see each other.
Why I can't execute the tests? Look's like selenium are trying to execute on the hub, not on the node. How can I configure to do not ask for Chrome driver location?
My test
public class TesteSelenium{
private static final String APLICATION_CONTEXT = "/SYSA";
WebDriver driver;
HomePage home;
#Before
public void setUp() {
Properties p = PropertiesUtil.getProperties();
File file = new File(p.getProperty("webdriver.path"));
System.setProperty(p.getProperty("webdriver.type"), file.getAbsolutePath());
driver = new ChromeDriver();
driver.get(p.getProperty("host.address")+APLICATION_CONTEXT);
LoginPage login = PageFactory.initElements(driver, LoginPage.class);
login.setUsuarioTextField(p.getProperty("usuario.selenium.login"));
login.setSenhaPasswordField(p.getProperty("usuario.selenium.password"));
home = login.submit();
}
#After
public void finish() {
driver.close();
}
I use a properties file
host.address = http://jbossserver:8080
usuario.selenium.login = USER_SELENIUM
usuario.selenium.password = 123123
webdriver.path = C:/selenium/drivers/chromedriver
webdriver.type = webdriver.chrome.driver
You should have your parameters inside quotation marks in command line. Like this:
java -Dwebdriver.chrome.driver="C:/selenium/drivers/chromedriver.exe"
The code to run Selenium tests remotely is slightly different.
public void setUp() throws MalformedURLException {
Properties p = PropertiesUtil.getProperties();
//File file = new File(p.getProperty("webdriver.path"));
//System.setProperty(p.getProperty("webdriver.type"), file.getAbsolutePath());
DesiredCapabilities capability = DesiredCapabilities.chrome();
//driver = new ChromeDriver();
WebDriver driver = new RemoteWebDriver(new java.net.URL("http://seleniumHubIp:4444/wd/hub"), capability);
driver.get(p.getProperty("host.address")+APLICATION_CONTEXT);
LoginPage login = PageFactory.initElements(driver, LoginPage.class);
login.setUsuarioTextField(p.getProperty("usuario.selenium.login"));
login.setSenhaPasswordField(p.getProperty("usuario.selenium.password"));
home = login.submit();
}
My mistake was writing the code to run local tests on a remote selenium node.
I am trying to run Selenium Webdriver script in Chrome, have added following lines in my existing script
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\chromedriver.exe");
private WebDriver driver = new ChromeDriver();
I am building my script in Intellij using Java. Not sure why i am getting "can not resolve symbol setProperty". I tried changing JRE and JDK files but nothing has worked. Any help would be appreciated.
Adding code
public class StartCaseJava extends TestCase {
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
// Getting Date and Timestamp for Last Name
Date d = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MMddyyHHmmss");
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
// private WebDriver driver = new ChromeDriver();
// driver = new FirefoxDriver();
// driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
private WebDriver driver = new ChromeDriver();
public void testStartCaseJava() throws Exception {
// System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
It's a long shot but should this
C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
maybe be renamed to C:\Users\Garimaari\IdeaProjects\Webdriver testing\Chromedriver\chromedriver.exe")
or are the double back slashes actually a necessary feature?
This might be because of one missing '\' before chromedriver.exe in your setProperty string.
Try using :
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Garimaari\\IdeaProjects\\Webdriver testing\\Chromedriver\\chromedriver.exe");
I am able to run my script using Chrome now. Here is the small sample declaration I used:
class StartCaseJAva {
static WebDriver driver;
public void testcasejava()) {
System.setProperty(path);
driver = new ChromeDriver();
}
}
I am working on frame based website. I am using selenium 2.47.1 & PhantomJS 1.9.2. I have written Automation Script & run it using firefox driver, it works perfectly. I am trying to execute the code with help PhanthomJS driver. But whenever I am trying to execute the it gives NoSuchFrameFoundException. My script is given below...
public class iFrame {
public String baseUrl = "https://test5.icoreemr.com/interface/login/login.php";
public WebDriver cd;
String scenarioName = "Sheet1";
ExcelLibrary ex = new ExcelLibrary();
#BeforeTest
public void SetBaseUrl() {
Capabilities caps = new DesiredCapabilities();
((DesiredCapabilities) caps).setJavascriptEnabled(true);
((DesiredCapabilities) caps).setCapability(
PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"C:\\Users\\Probe7\\Downloads\\phantomjs-1.9.2-windows\\phantomjs.exe"
);
this.cd = new PhantomJSDriver(caps);
cd.get(baseUrl);
cd.manage().window().maximize();
}
#Test(priority = 0)
/* Login into the Application */
public void Login() {
cd.manage().timeouts().implicitlyWait(25, TimeUnit.SECONDS);
cd.switchTo().frame("Login");
System.out.println("Control Moved to Login Frame");
String UserName = ex.getExcelValue(scenarioName, 2, 4);
cd.findElement(By.xpath("//body/center/form/table/tbody/tr/td/div/div[2]/table/tbody/tr[1]/td[2]/input")).sendKeys(UserName);
}
Above code gives following exception..
org.openqa.selenium.NoSuchFrameException: No frame element found by name or id Login
Eventhough same script works perfectly in firefox driver, but not in PhantomJS.
Please advice me what went wrong?? I am looking into this nearly a day, but didn't find any solution.. Please help me on this?
Thanks in Advance...