I am developing a framework using selenium webdriver in Java. I have two maven projects. One is for the framework and other is for the test project.
In order to launch launch ChromeDriver, I need to set the system property with the path of the chromedriver.exe file. I am doing this in DriverFactory.java which is in /src/main/java of the Framework Project. Now, if I place the exe files in src/main/resources/drivers, Java complains that the file is not found. The code is :
private static String chromeDriverLocation = "drivers/chromedriver.exe";
File cDriver = new File(DriverFactory.class.getResource(chromeDriverLocation).getFile());
// Is it executable
if (!cDriver.canExecute()) {
cDriver.setExecutable(true);
}
System.setProperty("webdriver.chrome.driver", DriverFactory.class.getResource(chromeDriverLocation).getFile());
I tried placing this file in src/main/resources/drivers in the Test Project and modified the code like :
private static String chromeDriverLocation = System.getProperty("user.dir")+ "\\classes\\drivers\\chromedriver.exe";
System.setProperty("webdriver.chrome.driver", chromeDriverLocation);
Though the above piece of code works in local when executed through Jenkins(Maven), I am not sure if this is the best way. Can someone suggest other good ways of achieving this ?
As an option, you may place the chromedriver.exe file into a directory added to PATH. In this case running the ChromeDriver will be as simple as creating an instance of new ChromeDriver() without dealing with system properties.
If you still would like to store chromedriver.exe under Resources, you may get it as steam, save to a file and set the "webdriver.chrome.driver" property:
InputStream in = getClass().getResourceAsStream("chromedriver.exe");
OutputStream out = new FileOutputStream("chromedriver.exe");
IOUtils.copy(in, out);
Related
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.
I am using Selenium Webdriver in Java for creating accounts from a excel file for a mediawiki software. I am working with eclipse.
When i try to export my project to a runnable jar file, there are some problems.
I have a file named "geckodriver.exe" im my resources folder (projectname/src/resources/geckodriver.exe)
All i want to do is to set a system property for selenium, so the user not have to manually choose the gecko driver file for working correctly with selenium.
This is my actual code, working inside eclipse
String filename = "geckodriver.exe";
File geckodriverFile = new File(this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath());
geckodriverPath = geckodriverFile.getAbsolutePath() + "\\resources\\" + filename;
And finally i want to set the system property
System.setProperty("webdriver.gecko.driver", geckodriverPath);
How can i achieve this to work inside a jar file?
Below is my code which works perfect in Eclipse Java Project
String IEPath = "src/IEDriverServer.exe";
File file = new File(IEPath);
System.setProperty("webdriver.ie.driver",file.getPath());
WebDriver driver = new InternetExplorerDriver();
If I export the same code to a runnable JAR file and double click it or if I run it from command prompt gives below exception
The driver ececutable does not exist C:\Backup\New folder\src\IEDriverServer.exe
I have copied IE exe inside my Java Project and have exported the Java Project including the IE exe. When I run the JAR, it is failing to pick the IE exe path.
Please help! TIA!
You have two options here:
Read the resource inside the jar. See more info here.
Use WebDriverManagerto automate the management of IEDriverServer.exe.
For alternative 2, simply import the WebDriverManager library in your project and change your code:
String IEPath = "src/IEDriverServer.exe";
File file = new File(IEPath);
System.setProperty("webdriver.ie.driver",file.getPath());
... by:
InternetExplorerDriverManager.getInstance().setup();
I'm attempting to create an executable jar for a selenium test. Part of the things the code needs to do is set a system property to tell Selenium where the driver executable can be found (I'm using the chromedriver). File structure is as follows:
src
com
mycompany
SeleniumTest.java
chromeDriver
windows
chromedriver.exe
And the code is as follows:
private static String WINDOWS_DRIVER = "/chromeDriver/windows/chromedriver.exe";
System.setProperty("webdriver.chrome.driver",
SeleniumTest.class.getResource(WINDOWS_DRIVER).getFile());
When executed in eclipse, this code works fine. However, when I export to a runnable jar file (from eclipse) I get the following error:
Exception in thread "main" java.lang.IllegalStateException: The driver executable
does not exist: F:\temp\file:\F:\temp\seleniumTest.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:75)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:139)
And yet seleniumTest.jar exists at F:\temp as does the path within the jar which the error message specifies.
Any ideas on what is wrong or suggestions to try? I've tried changing the slahses to backslashes and also (just as a test) hard coding the path (e.g. setting the system property to F:\temp\seleniumTest.jar!\chromeDriver\windows\chromedriver.exe), but neither has worked.
The system property is supposed to contain the path to the file, on the file system, where the driver can be found and executed.
The driver is not a file. It's an entry of your jar file. Executables bundled in a jar file can't be executed.
If you really want to bundle the driver into your jar file and execute it, then you'll have to read the bytes from this classpath resource, write them to a temporary executable file, and then tell selenium where this temporary executable file is located.
Try something like:
// locate chromedriver in the jar resources
URL res = getClass().getResource("/chromeDriver/windows/chromedriver.exe");
// locate chromedriver in the jar filesystem
File f = new File(res.getFile());
// copy chromedriver out into the real filesystem
File target = new File(System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + f.getName());
java.nio.file.Files.copy(f.toPath(), target.toPath(), StandardCopyOption.REPLACE_EXISTING);
if (!target.canExecute())
throw new FileNotFoundException("chrome.exe copy did not work!");
System.setProperty("webdriver.chrome.driver", target.getCanonicalPath());
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.