Have WebDriverManager Download to Specific Directory - java

I understand that the following line of code places the latest chromedriver file in ~/.cache/selenium...
WebDriverManager.chromedriver().setup();
My question: Is there a way to configure this to download the file to a specific directory?
As an example, say I have a directory named "MyChromeDriver". I would like WebDriverManager to place the chromedriver file inside "MyChromeDriver" without all the extra sub-directories.
Thank you in advance!

Invoke WebDriverManager as follows:
WebDriverManager.chromedriver().cachePath("MyChromeDriver").avoidOutputTree().setup();

Related

Where to find WebDriverManager "~/.cache/selenium" absolute path?

I'm using WebDriverManager via io.github.bonigarcia maven package. Based on documentation the following line of code places the latest chromedriver file in ~/.cache/selenium...
WebDriverManager.chromedriver().setup();
My question: What should be the absolute path for that relative path "~/.cache/selenium"?
Thank you in advance!
~ here is your user working folder.
For example on Windows based system the file will be located in:
C:\Users\yourName\.cache\selenium
folder.
So, if my user folder on my computer is called Prophet it will be
C:\Users\Prophet\.cache\selenium

The type org.openqa.selenium.firefox.FirefoxDriver is not accessible

I need help in setting up selenium in MAC OS.
I have added all the required selenium jar files and java JRE to build path.
I have added java path in .profile
But still I am still getting error for Firefox driver.
The type org.openqa.selenium.firefox.FirefoxDriver is not accessible
Can you please help.
This error message...
The type org.openqa.selenium.firefox.FirefoxDriver is not accessible
...implies that the GeckoDriver was not accessible by your program/script,
Your main issue is the presence of numerous unwanted JAR files containing the same class which are:
selenium-server-standalone-3.14.0.jar
Selenium-Java client JARs.
selenium-firefox-driver-2.35.0.jar
Solution
Remove all the JARs and add back only selenium-server-standalone-3.14.0.jar
Download the required format of GeckoDriver executable from mozilla/geckodriver, extract the binary and then initialize the FirefoxDriveras follows:
//imports
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
//other lines of code
System.setProperty("webdriver.gecko.driver","C:\\path\\to\\geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://www.google.com/");
To resolve this issue,
create a new Java project with JRE syetm libraray (JavaSE 1.8)
copy jar file and build path
copy driver executables
Create new class and move on with your work!!!
From what I can see in your 'Referenced Libraries' folder, you seem to have added both the selenium-standalone-3.x.x.jar and the individual client-combined-3.x.x versions of the jar files (selenium client jars for java). Is it possible that your eclipse does not know which libraries to finally use? Request you to remove all the libraries in your build path and add ONLY the selenium-server-standalone-3.x.x.jar file. Once done, refresh and this should resolve.
Also, once the right jars are in place, the import should resolve even without the System.setProperty instruction in your class file.
If the issue persists, could you please paste a screen capture without the right-click context menu on the error?
Hope this helps.
The pretty simple solution is " You might have created module while creating the package. So simply delete the module it will resolve the issue. It worked for me!!

Creating and using a jar with non-text-resources

I'm trying to create a jar that enables people to launch web browser drivers through this library.
So the idea is to have one project "ToBecomeJar" have chromedriver (does not have a file extension) as an item in a "drivers" folder and using it in its code as:
private WebDriver getDriver(){
System.setProperty(CHROME_DRIVER_PROPERTY, "drivers/chromedriver");
driver =new ChromeDriver();
return driver;
}
The issue here is that when I turn this project into a library, of course the "PathToDriver" will be taken as an absolute path when used, leading to my new project needing path structure exactly the same as the library with the driver there.
Is there any way to make this relative?
I've tried working with a resource folder and calling the resource with .getResource but I really can't manage to make it work. When looking into this people mention that it should become .getResourceStream as it becomes something other then a file, but that doesn't work for me as it's not a text file I'm trying to use.
Try to use 'user.dir' which is an environment var of the JVM.
It can be used to construct the path string of your driver executable:
private WebDriver getDriver(){
System.setProperty(CHROME_DRIVER_PROPERTY, System.getProperty("user.dir") + "/drivers/chromedriver");
driver = new ChromeDriver();
return driver;
}
You can extract the needed resources from jar to a temporary folder during the startup of your app and then point to that folder for the proper drivers.

Selenium chromedriver.exe

As part of my project setup, I have two projects one as libraries and other as Selenium.
I have all the browser setup in libraries project with /src/main/resource having chromedriver.exe
Selenium project has all the pagefactory classes and this project has dependency on Library.
However when i am running project on slave I am getting following error message:
java.lang.IllegalStateException: The driver executable does not exist: c:\jenkins_slave10\workspace\TEST-Demos\file:\C:\Users\svc-hudson\.m2\repository\com\bskyb\automation\crm\libraries\1.1-SNAPSHOT\libraries-1.1-SNAPSHOT.jar!\chromedriver\windows\chromedriver.exe
at com.google.common.base.Preconditions.checkState(Preconditions.java:177)
at org.openqa.selenium.remote.service.DriverService.checkExecutable(DriverService.java:117)
at org.openqa.selenium.remote.service.DriverService.findExecutable(DriverService.java:112)
at org.openqa.selenium.chrome.ChromeDriverService.createDefaultService(ChromeDriverService.java:89)
at org.openqa.selenium.chrome.ChromeDriver.(ChromeDriver.java:149)
at com.abc.automation.crm.actions.BrowserSetup.openBrowserChrome(BrowserSetup.java:38)
at com.abc.automation.crm.actions.Search.setup(Search.java:111)
at com.abc.automation.crm.actions.Search.directorynumber(Search.java:35)
at com.abc.automation.crm.stepdefs.Demo.i_search_for_directory_number(Demo.java:34)
Did you try setting the system property to specify the chromedriver.exe location?
Either start the selenium server with
-Dwebdriver.chrome.driver=c:\path\to\your\chromedriver.exe
or
set the system property in your code:
System.setProperty("webdriver.chrome.driver", "C:/path/to/your/chromedriver.exe");
You should not pack chromedirver on jar.
Try to add prebuild step in jenkins that will download chromdriver to your project folder in workspace and set relative path to it.
Error is saying that the path you have specified is not correct path.
System.setProperty("webdriver.chrome.driver", "/home/path/Downloads/chromedriver"); // specify complete path.
WebDriver webDriver = new ChromeDriver();
Might be possible that chrome driver is not compatible with OS. You have to check this. You can download driver from http://chromedriver.storage.googleapis.com/index.html?path=2.16/
and extract .zip file.
Error is saying that the path you have specified is not correct.
System.setProperty("webdriver.chrome.driver", "c://jars//imp//IEDriverServer.exe"); // complete path.
WebDriver webDriver = new ChromeDriver();
I think you are missing specify your path with double '//' like below
c://jars//imp//IEDriverServer.exe

Configuration of Selenium 2 (WebDriver), using IE and upload file with WebDriver

How do I configure Selenium WebDriver? I have automated test cases using Selenium with Java. Now I need to automate upload and download of a file using WebDriver. I had added webdriver-common-0.9.7376.jar. I like to use Internet Explorer. How can I do that?
I'm just declaring variable and using driver
private static WebDriver driver;
driver.findElement(By.id(upload)).sendKeys("file to be upload");
Is this correct?
Ques. 1: How to configure WebDriver?
Ans: There are 2 ways: 1) Adding "selenium-server-standalone-2.29.0.jar" only
OR,
2) Adding "selenium-java-2.29.0.jar" and all the jars located on "selenium-java-2.29.0\selenium-2.29.0\libs" folder
You can download "selenium-server-2.29.0.zip" and "selenium-java-2.29.0.zip" from http://code.google.com/p/selenium/downloads/detail?name=selenium-server-2.29.0.zip and http://code.google.com/p/selenium/downloads/detail?name=selenium-java-2.29.0.zip respectively.
Extract them and you could get corresponding jar files to add.
Ques. 2: How to instantiate IE and how to upload file?
Ans: The java code as below:
File file = new File("C:\\Program Files\\Internet Explorer\\iexplore.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
WebDriver driver = new InternetExplorerDriver();
driver.findElement(By.id("upload")).sendKeys("file to be upload");
If "File file = new File("C:\Program Files\Internet Explorer\iexplore.exe");" doesn't work download "IEDriverServer" and replace that line with below:
File file = new File("E:\\Ripon\\IEDriverServer_Win32_2.29.1\\IEDriverServer.exe");
[Note: You can download "IEDriverServer" from http://code.google.com/p/selenium/downloads/list ]
You need to add all jar after downloading selenium-java 2.25 0r any version. First add all jar then all all lib folder jar.
selenium-java-2.25.0.jar
selenium-java-2.25.0-srcs.jar and then all lib jar (Don't forget to add all lib folder jar)
Without instantiate driver for your browser, it won't open a browser window to do the upload/download operation. If you're using IE you've to write driver = new InternetExplorerDriver();
Instead of the old and outdated webdriver-common package, you probably need the newest selenium-java from http://code.google.com/p/selenium/downloads/list.
If you'll ever also need running Selenium RC locally, or Remote WebDriver ot Selenium Grid, you'll need the selenium-server package there (if you don't yet know what these are, just take selenium-java).
In both cases, for running InternetExplorerDriver, you'll also need the IEDriverServer from the page mentioned above. It's up to you whether to use the 32 or 64 bit version.
You can find an example of setting it up here in the documentation. If you dig around a bit, you'll find many more useful information in that documentation.
For example, for Internet explorer, you'll do:
System.setProperty("webdriver.ie.driver", "C:\\path\\to\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
// your testing code
driver.quit();
Your method of uploading a file is correct.
And as of now (Selenium v2.29.0), you can't download files via any WebDriver. If you really want to do so, you'll have to find another way.

Categories

Resources