Set firefox and chrome driver for selenium in java? - java

I am facing a problem with selenium web driver in java, it says that "The path to the driver executable must be set by the webdriver.gecko.driver system property" you can see it below, but I have set everything as usual I do.

You just need to change the execution flow.
You see, you get the exception because you create FirefoxDriver first and THEN you set the property. It should be in reverse order.
First, set property and THEN initialize WebDriver:
public class EntryPoint {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "path/to/executable.exe");
WebDriver driver = new FirefoxDriver();
}
}

Assuming you are on Windows:
1. Try replacing "/" with "\" on your path.
2. If the previous step did not work, try running Intellij as an admin. The program might not have the right permissions to execute anything on that folder.

Related

Electron+Selenium+Java. Run electron app with parameters (like in windows shortcut)

I am working on automation for Electron app using Selenium + Java.
Java test class for demo electron app works good (snippet is below).
The problem is, that my real application on Windows should be launched with json configuration file. And the path to this config file set up in the SHORTCUT parameter.
Example how it works for the shortcut:
C://AppExample/Desktop/app.exe --param=config.json
Test class for demo electron app
#Test
public void test() throws InterruptedException {
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.setBinary("C://AppExample/Desktop/app.exe");
options.setCapability("chromeOptions", options);
options.setCapability("setBrowserName", "chrome");
driver = new ChromeDriver(options);
if (driver.findElements(By.id("button-about")).size() > 0)
driver.findElement(By.id("button-about")).click();
driver.findElement(By.id("get-started")).click();
List<WebElement> elements = driver.findElements(By.className("nav-button"));
for (WebElement element : elements) {
element.click();
}
driver.quit();
}
I tried to set custom capability but it doesn't work.
options.setCapability("--param","=config.json");
Is there a way to run electron app with parameters (like parameters in windows shortcut) in selenium?
I found the solution.
First, I need to use WinappDriver+Appium+Selenium Java stack.
The second:
Solution about parameters is here
https://github.com/microsoft/WinAppDriver/issues/1323
So the code will be like that:
dc = {
'app': App,
'platformName': 'Windows',
'platformVersion': '10',
'automationName': 'Windows',
"appArguments": "-- config=file.json",
'appWorkingDir': "path to your file.json"
}

Selenium with Java error :"File path wcxChrome.crx needs to be absolute in key ..."

I'm new to Selenium, and I am doing some SeleniumDriver practices on Java, following the example code on "https://saucelabs.com/resources/articles/getting-started-with-webdriver-selenium-for-java-in-eclipse".
I followed the instructions but I got an error. Here is the code block:
#Test
public void site_header_is_on_home_page() {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
WebDriver browser = new ChromeDriver();
browser.get("https://www.saucelabs.com");
WebElement href = browser.findElement(By.xpath("//a[#href='/beta/login']"));
assertTrue((href.isDisplayed()));
browser.close();
}
The error message is:
[17200:25400:0515/151552.506:ERROR:external_registry_loader_win.cc(152)]
File path wcxChrome.crx needs to be absolute in key
Software\Google\Chrome\Extensions\mjdepfkicdcciagbigfcmdhknnoaaegf
Can someone help me fix this? Many thanks!
It appeared that the problem was because I gave the link to Chrome, not the ChromeDriver. I changed the path to ChromeDriver and it worked fine.

Error :"java.lang.IllegalStateException: The path to the driver executable must be set by the webdriver.chrome.driver system property" is displayed

Following is my code snippet :
public class FindAllElementsFromTable {
WebDriver driver;
#BeforeClass
public void beforeClass(){
System.setProperty("Webdriver.chrome.driver","C:\\Selenium Drivers\\chromedriver.exe");
driver = new ChromeDriver();
}
and some tests after this.
When executed,getting above exception.
I tried with forward slash i.e. "C:/Selenium Drivers/chromedriver.exe" as well, but it makes no difference. I checked other answers, but I haven't made any of those mistakes like initialising the chromedriver twice/wrong place/setting property after WebDriver initialization/wrong driver name etc.
Can you help me understanding the exact issue?
You have a typo in your setProperty line.
It should be webdriver not Webdriver
System.setProperty("webdriver.chrome.driver","C:\\Selenium Drivers\\chromedriver.exe");

Selenium WebDriver can find element in Chrome but not in FireFox

I'm still new to working with FireFox and using the Selenium Driver. I wrote the following driver code in Java as part of a CucumberTest. I have the ChromeDriver section commented out. When I comment out the FirefoxDriver and run this using the ChromeDriver line it works as expected. However, when I try to run the exact same code swapped to the FirefoxDriver, I get to my first when statement and I get a nullPointerException at the first find element by id line. Is there a workaround?
#When("^User enters \"(.*)\" and \"(.*)\"$")
public void user_enters_UserName_and_Password(String realUsername, String realPassword)
throws Throwable {
System.out.println(realUsername + " " + realPassword);
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys(realUsername);
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys(realPassword);
driver.findElement(By.id("loginbutton")).click();
}
EDIT: The issue was in how I was calling the driver. I corrected how I initilzed my driver to this and it worked. Something about how I was calling it before caused it to get discarded so it was null by the time it came to this step.
The issue was in how I was instantiating the Firefox driver in a different file. This is what worked ' String marionetteDriverLocation = "c://Features//geckodriver.exe";
System.setProperty("webdriver.gecko.driver", marionetteDriverLocation);
//create firefox profile
FirefoxProfile myProfile= new FirefoxProfile();
// This will set the true value
myProfile.setAcceptUntrustedCertificates(true);
//setup the driver with profile settings for security issues.
driver = new FirefoxDriver(myProfile);'

How to run ghostdriver with Selenium using java

I want to use phantomJS for some web testing, and I've come across GhostDriver (https://github.com/detro/ghostdriver). I've built it using the instructions in the readme and I can run it on a specified port, but I am not sure how to access the web driver from my java code. To clarify, I've seen this example in ruby:
caps = {
:browserName => "phantomjs",
:platform => "LINUX"
}
urlhub = "http://key:secret#hub.testingbot.com:4444/wd/hub"
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 120
#webdriver = Selenium::WebDriver.for :remote, :url => urlhub, :desired_capabilities => caps, :http_client => client
#webdriver.navigate.to "http://www.google.com/"
puts #webdriver.title
#webdriver.save_screenshot("./screenshot.png")
#webdriver.quit
I'm just not sure how to do the same from java.
Just to clarify for others who might see this, to run it from java:
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
"/Path/to/bin/phantomjs");
driver = new PhantomJSDriver(caps);
Then it can be used like a usual WebDriver.
I believe this link will answer your questions. You will need Selenium 2.28.0, and PhantomJS 1.8. I have tested this, and it works as advertised, although my tests were precursory. Note that you need to download the Selenium zip file to get the jar which contains the bindings. The Maven repo does not yet include it.
http://ivandemarino.me/2012/12/04/Finally-GhostDriver-1-0-0/
First download the exe file of the PhantomJSDriver. Don't need to install, only download this file from http://phantomjs.org/download.html and simply give the path of the exe file in the given code.
public class Browserlaunch {
public static void main(String[] args) {
DesiredCapabilities DesireCaps = new DesiredCapabilities();
DesireCaps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/Drivers/phantomjs/bin/phantomjs.exe");
WebDriver driver=new PhantomJSDriver(DesireCaps);
driver.get("http://google.com");
}
}
Only set system property:
System.setProperty("phantomjs.binary.path", "lib/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

Categories