I recently started up coding with Selenium and Java. I have a basic test set up and things seem to be working with Firefox. I would like test on Chrome as well. But when I define the Webdriver as ChromeDriver, I get an error saying I need to define it on the system path.
I used Maven to download all the dependencies, but now I don't know how to reference them properly.
My issue:
protected void setUpBeforeTestClass(){
// define path to ChromeDriver
// cause I get the error "The path to the driver executable must be set by the webdriver.chrome.driver system property"
System.setProperty("webdriver.chrome.driver", "/path/to/chromedriver");
// driver = new FirefoxDriver();
driver = new ChromeDriver();
String url = urls[0]; // pull in from array of urls
driver.get(url);
}
Maven downloads dependencies to:
C:\Users\{username}\.m2\repository\org\seleniumhq\selenium\ ...
And ChromeDriver is in that folder.
How can I reference this folder to pull in ChromeDriver without hard-coding the path? (I'm not looking to modify my system environment variables)
My goal is that I can just download my Java classes and Maven dependencies on any machine and run the tests.
You need to download the Chrome Driver Binary and put it somewhere on your computer. Somewhere like "C:/Selenium/chromedriver.exe". You can find it here. You can then access it by using something like:
System.setProperty("webdriver.chrome.driver", "C:/Selenium/chromedriver.exe"));
As per answers, I found that it is the binary that I was missing. Damn.
I found this:
https://github.com/bonigarcia/webdrivermanager
This helps out a lot in terms of managing the webdrivers I want to use. I don't have to download the webdrivers myself, this does it for me.
Download the binary from here:-
http://chromedriver.storage.googleapis.com/index.html?path=2.19/
Use below code:-
WebDriver driver=null;
System.setProperty("webdriver.chrome.driver","./src//lib//chromedriver");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
capabilities.setCapability("chrome.binary","./src//lib//chromedriver");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
Hope it help :)
Get back to me if still facing issue :)
Related
I use WebDriverManager.chromedriver().setup(); for getting chrome property in my selenium tests. It works fine. I am trying to download a file by changing the default download location of chrome browser because I want to download the file to my java project classpath, rather than to my local machine, but I am unsure if WebDriverManager has such an implementation. Currently, I am trying something like this:
WebDriverManager.chromedriver().setup();
String downloadDir = System.getProperty("user.dir");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downloadDir);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
The code block works and downloads the file to the project classpath, as expected, but I think there would be a cleaner and shorter way to do it. I have done some research on the Bonigarcia WebDriverManagerdependency and some of its implementations but can't find anything helpful. Is there a better way to achieve the above?
Use the method targetPath() to change the default location of the driver downloaded by WebDriverManager:
WebDriverManager.chromedriver().targetPath("/my/custom/path").setup();
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'm building an automated test suite using Selenium Web Driver. At a certain point I must test how the page works by having a Chrome extension turn on or off. Think of it as you would want to click on the Adblock extension and then click disable for this site. Then, turn it on again.
I searched all over the Internet and there is no way to implement this using just Selenium. Do you know how could I perform such an action? (from Java ideally)
One possible solution is to go with Chrome options and manage the extensions set to the WebDriver. Quick example:
ChromeOptions options = new ChromeOptions();
options.addExtensions(new File("/path/to/extension.crx"));
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
If you want to turn those On and OFF in a single test, you can spawn two separate drivers and compare the results, since I'm not sure that session reuse will do the job in this case.
Below is the solution is in Python with pyautogui (I believe it's similar to autoit in java - so you can extend the same solution for java also).
Pre-Condition:
save the extension image in the project folder (I saved it under "autogui_ref_snaps" folder in my example with "capture_full_screenshot.png" name
Python:
Imports needed
from selenium import webdriver
from selenium.webdriver import ChromeOptions
import pyautogui #<== need this to click on extension
Script:
options = ChromeOptions()
options.add_argument("--load-extension=" + r"C:\Users\supputuri\AppData\Local\Google\Chrome\User Data\Default\Extensions\fdpohaocaechififmbbbbbknoalclacl\5.1_0") #<== loading unpacked extension
driver = webdriver.Chrome(
executable_path=os.path.join(chrome_options=options)
url = "https://google.com/"
driver.get(url)
# get the extension box
extn = pyautogui.locateOnScreen(os.path.join(GenericMethods.get_full_path_to_folder('autogui_ref_snaps') + "/capture_full_screenshot.png"))
# click on extension
pyautogui.click(x=extn[0],y=extn[1],clicks=1,interval=0.0,button="left")
If you are loading an extension and it's not available in incognito mode then follow my answer in here to enable it.
Can use sikuli(GUI Automation tool) to click on browser addon.
Imports needed:
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;
Script:
Pattern addon=new Pattern("D:\\My Files\\Addon.jpg"); //image of the addon must be given as a pattern for identifying that on the browser/webpage
Screen s=new Screen();
s.hover(addon);
s.click(addon);
If you want to click the extension icon on the right side of the chrome and it is the extension that will be opened during the opening of the page or after clicking action
you can use this
public void openBrowserExtension(){
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.postMessage('clicked_browser_action', '*')");
}
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
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.