How to get chromedriver working on android? java example? - testing

I would like to be able to do testing using chromedriver on Android device. How is it possible?
I have rooted an Android device and cannot get the Chrome tests to work.
I tried to follow this guide: https://sites.google.com/a/chromium.org/chromedriver/getting-started/getting-started---android
I cannot find which Chromedriver version I should install on Android device.
Does anyone have an example or step by step guide for this?

You don't need to install chromedriver on a phone, only your local machine you want to run tests from. I actually set this up couple weeks ago. This is basic set up you need:
public WebDriver getMobileChromeDriver() {
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.setExperimentalOption("androidPackage", "com.android.chrome");
chromeOptions.setExperimentalOption("androidDeviceSerial", deviceId);
return new ChromeDriver(chromeOptions);
}
deviceId variable contains uuid taken from adb for particular device. If you don't provide it chromedriver will run on first available node.
One more thing you need to do before running tests is start adb server.
On linux based machines it would be something like:
adb start-server
(assuming you have adb in your path)
If you have problem with determining which chromedriver you need for your local machine let me know.
If you are using Windows machine you probably need to add one more line of code to point to your chrome binary:
System.setProperty("webdriver.chrome.driver", "<path_to_your_chrome_binary>");

For me, the previous answer works but with Chrome version 66 and chrome driver 37.
System.setProperty("webdriver.chrome.driver", "<path_to_your_chrome_binary>");
Even if system property is "webdriver.chrome.driver", you have to set the path of chrome on the device like "/data/app/com.android.chrome-1.apk"

Related

Using Selenium on iPad?

I use a selenium application, and I make test on my computers browsers. It opens and manage the browsers.
Would there be a way to simulate an iPad and run this same code on it? Same thing for a cellphone?
So far I found people who wrote they did so by emulation but did not say how...
I execute iPad testing via emulation using ChromeDriver. Does this help? With C#:
IWebDriver driver;
ChromeOptions ipadOptions = new ChromeOptions();
string deviceName = "Apple iPad";
ipadOptions.EnableMobileEmulation(deviceName);
driver = new ChromeDriver(ipadOptions);
My biggest struggle was testing requirements depending on orientation (landscape vs. portrait.) It doesn't support switching orientation which is ridiculous.
To be able to use selenium code with Android device browser, try following:
install Android SDK on your computer to get access to Android Debug Bridge (adb) features
start adb server from cmd/Terminal with "adb start-server" command
(target device should be already connected)
start in the same way chromedriver server with "chromedriver" command (chromedriver executable should be already in your system Path)
you should see something like:
Starting ChromeDriver 2.15.322448 (52179c1b310fec1797c81ea9a20326839860b7d3) on port 9515(in my case it always starts on 9515 port, but you have to check this value to use it in next step)
run code with 'androidPackage':com.android.chrome' chromeOptions and instance of remote webdriver. Python code looks like:
from selenium import webdriver
capabilities = {'chromeOptions': {'androidPackage':com.android.chrome',}}
driver = webdriver.Remote('http://localhost:9515', capabilities) # 9515 is mentioned port number on which chromedriver server started
driver.get('http://google.com')
driver.quit()
P.S. It's not a direct answer on question how to use selenium with iPad, but OP asks for selenium + Android also

Only local connections are allowed Chrome and Selenium webdriver

I am using Chrome webdriver 2.23 & Selenium 2.53.1.
I have tried a lot, but could not get it fixed. Whenever I run my selenium script, it is giving me the following error
Starting ChromeDriver 2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129) on port 12162
Only local connections are allowed.
This is just an informational message. Your issue might be a missmatch between the versions of chromedriver and selenium-server-standalone.
Try with the latest selenium version 3.0, it is working for me.
Please not that for selenium 3.0 you need to specify the driver first and after the selenium server.
With the new selenium, which is 3.0 you should use:
java -Dwebdriver.chrome.driver=path_to_chrome_driver -jar selenium-server-standalone-3.0.0-beta2.jar
If you are using selenium version below 3.0 you need to reverse the order of selenium with the driver, like:
java -Dwebdriver.chrome.driver=path_to_chrome_driver -jar selenium_server.jar
When you are starting the selenium server, open a console in the directory with chromedriver and selenium server and execute the above command.
Here you are a working stack:
Some previous notes:
If you run selenium in a non graphical enviromnent, xvfb is required.
You will need selenium-server-standalone-2.53.1.jar (working version). You can download selenium versions here: http://selenium-release.storage.googleapis.com/index.html
You will also need chromedriver v 2.27. Download link: https://chromedriver.storage.googleapis.com/index.html
1) Run sudo Xvfb :10 -ac &
2) Run export DISPLAY=:10
3) Run java -jar "YOUR_PATH_TO/selenium-server-standalone-2.53.1.jar" -Dwebdriver.chrome.driver="YOUR_PATH_TO/chromedriver.2.27" -Dwebdriver.chrome.whitelistedIps="localhost"
First off, What you are seeing is not an error. It is an informational message.
When you run this driver, it will enable your scripts to access this and run commands on Google Chrome.
This can be done via scripts running in the local network (Only local connections are allowed.) or via scripts running on outside networks (All remote connections are allowed.). It is always safer to use the Local Connection option. By default your Chromedriver is accessible via port 9515.
See this answer if you wish to allow all connections instead of just local.
If your Chromedriver only shows the above two messages (as per the question), then there is a problem. It has to show a message like this, which says it started successfully.
Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103#{#416}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
To troubleshoot this...
Step 1: Check your Chromedriver version
$ chromedriver --version
ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103#{#416})
My version is 83.0.4103.39.
Step 2: Check your Chrome Browser version
Open Google Chrome.
Options --> Help --> About Google Chrome
Or open a terminal and run the following command (works on Ubuntu).
$ google-chrome --version
Google Chrome 83.0.4103.61
My version is: Version 83.0.4103.61
Step 3: Compare versions of Chromedriver and Google Chrome
Both these versions are starting with 83, which means they are both compatible. Hence, you should see a message like below, when you run the below command.
$ chromedriver
Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103#{#416}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
If your versions mismatch, then you will see the following message. You will not see the line which says, ChromeDriver was started successfully..
$ chromedriver
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987#{#882}) on port 9515
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
Step 4: Download the correct version of Chromedriver
Download the correct version that matches your browser version. Use this page for downloads. After you download, extract the content, and move it to one of the following two folders. Open each of the following two folders and see whether your current Chromedriver is there. If it is on both folders, replace both. And do STEP 3 again.
/usr/bin/chromedriver
/usr/local/bin/chromedriver
Check the version of your installed Chrome browser.
Download the compatible version of ChromeDriver from
https://sites.google.com/a/chromium.org/chromedriver/
Set the location of the compatible ChromeDriver to:
System.setProperty("webdriver.chrome.driver", "C:\\Users\\your_path\\chromedriver.exe");
Run the Test again.
It should be good now.
You need to pass --whitelisted-ips= into chrome driver (not chrome!). If you use ChromeDriver locally/directly (not using RemoteWebDriver) from code, it shouldn't be your problem.
If you use it remotely (eg. selenium hub/grid) you need to set system property when node starts, like in command:
java -Dwebdriver.chrome.whitelistedIps= testClass etc...
or docker by passing JAVA_OPTS env
chrome:
image: selenium/node-chrome:3.141.59
container_name: chrome
depends_on:
- selenium-hub
environment:
- HUB_HOST=selenium-hub
- HUB_PORT=4444
- JAVA_OPTS=-Dwebdriver.chrome.whitelistedIps=
I followed my frnd suggestion and it worked like a gem for me:
Working Code:
1) Downloaded chromedriver.
2) Code is
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Sel {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver_win32\\chromedriver.exe"); // path of chromedriver
WebDriver driver = new ChromeDriver();
driver.get("https://google.ca");
driver.manage().window().maximize();
driver.getTitle();
}
}
For me, updating the chromedriver and selenium version removed this message.
However, this is not an actual error and just an informational message.
If your program is still passing with exit code 0 at the end even when this message is printed, it means the execution went fine.
I was able to resolve the problem by following steps:
a. upgrade to the latest chrome version, clear the cache and close the chrome browser
b. Download latest Selenium 3.0
C#:
ChromeOptions options = new ChromeOptions();
options.AddArgument("C:/Users/username/Documents/Visual Studio 2012/Projects/Interaris.Test/Interaris.Tes/bin/Debug/chromedriver.exe");
ChromeDriver chrome = new ChromeDriver(options);
Worked for me.
Sorry for late post but still for info,I also facing same problem so I Used updated version of chromedriver ie.2.28 for updated chrome browser ie. 55 to 57 which resolved my problem.
I was also getting the same issue. I resolved this issue by updating the chromedriver. So if anyone is facing same issue with chrome browser just update your chromedriver.
I saw this error
Only local connections are allowed
And I updated both the selenium webdriver, and the google-chrome-stable package
webdriver-manager update
zypper install google-chrome-stable
This site reports the latest version of the chrome driver
https://sites.google.com/a/chromium.org/chromedriver/
My working versions are chromedriver 2.41 and google-chrome-stable 68

Firefox is showing "Cannot find firefox binary in PATH." error in MAC OS

Firefox is showing "Cannot find firefox binary in PATH." error in MAC OS.
As per my understanding this issue occurs if the Mozilla is not installed in their default location. I am new in MAC OS
I have also tried with firefox profile and already I have set selenium provided DesiredCapabilities
WebDriver driver = new FirefoxDriver(new FirefoxBinary(new File("/Applications/Firefox.app/Contents/MacOS/firefox-bin")), profile);
I have also tried with different path of mozilla in MAC OS like:-
/Applications/Firefox.app/Contents/MacOS/firefox-bin
/Applications/Firefox.app/Contents/MacOS/firefox
/Applications/Firefox.app
Also please provide a snippet for the same.
Chrome is working fine for me in MAC OS. Only Mozilla is creating problem
Surely, I am missing something. I am attaching the snapshot regarding same.
Also, Is there any method present by which I can locate any application installed location using java code?
According to https://github.com/SeleniumHQ/selenium/wiki/FirefoxDriver there is a property you have to set to set the binary path:
webdriver.firefox.bin
so i assume you have to do following:
System.setProperty("webdriver.firefox.bin","/Applications/Firefox.app/Contents/MacOS/firefox-bin");
WebDriver driver = new FirefoxDriver();
I hope this helps :)
If you have installed firefox, check whether the Firefox application is in the /Applications folder. If not move the application to /Application folder and run the test again.
If above worked, you don't have to add a system property "webdriver.firefox.bin"
Possibly updated for the current situation with this I did this:
Installed geckodriver, available since 2018, to /usr/local/bin (arbitrary choice and made sure it could execute. This placement made it unnecessary to add it to my $PATH.
Installed Firefox to /Applications (I had the developer version)
Added this line to my setUp method: driver = new FirefoxDriver(new FirefoxOptions().setHeadless(true));
All this in macOS 10.11 "Big Sur" (aarch64) using current selenium.

Exception while connecting with Chrome driver

How to use Chrome Driver in selenium project? Here am using latest version of Chrome Driver. My code looks like:
System.setProperty("webdriver.chrome.driver", C:\\Users\\....\\Downloads\\chromedriver_win32_2.3.zip\\chromedriver.exe");
In 64 bit Windows 7, however I am getting an exception.
org.openqa.selenium.WebDriverException: unknown error: unable to
discover open pages
I searched in google they provide the solution to replace the latest version of Chrome driver. I tried but not able to solve the issue.
chromedriver_win32 supports both 32-bit and 64-bit machines.
Use the latest chromedriver version; Download it from,
http://chromedriver.storage.googleapis.com/index.html?path=2.9/
And add the below snippet in your code
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Can you unzip chromedriver_win32_2.3.zip file and then provide location of ChromeDriver inside unzipped folder ... This should help. Modified code should look like the following:
System.setProperty("webdriver.chrome.driver", "C:\Users\....\Downloads\chromedriver_win32_2.3\chromedriver.exe");

how to use different version of firefox using webdriver?

how to set Firefox version in web-driver?
I just want to user different version of Firefox.
like different version 19, 20 , 21....
please provide a generic solution which help for other browser also.
You have to install all the versions on your system. Then you can use the System property webdriver.firefox.bin to define the path for Firefox. Note than since the path is set through a System property, you will not be able to run two different Firefox in the same Java process.
This solution is specific to Firefox. There is no generic solution. You have to configure every WebDriver yourself.
More information about the configuration of Firefox Web Drvier.
Finally i have found solution to run with different browser version
System.setProperty("webdriver.firefox.bin", "/Applications/Firefox-2.app/Contents/MacOS/firefox-bin");
WebDriver driver = new FirefoxDriver();
driver.get(baseUrl);
System.out.println(driver.getTitle());
driver.close();
driver.quit();
I have found how to start a different FireFox browser (actually WaterFox) in Python Selenium, replacing
driver = webdriver.Firefox()
with
driver = webdriver.Firefox(firefox_binary = "/path/to/my/waterfox")
(Ubuntu 20.04, Python 3.95)