I’m trying to automate some routine administrative tasks on my Archer AX55 WiFi router by means of Selenium WebDriver, which simulates user interaction through the router’s web-interface (unfortunately, this router doesn’t provide SSH, REST or similar API which I could use for scripting as an alternative). As far as I run the following piece of Java code on my Windows desktop system, everything works fine:
package org.example;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.time.Duration;
public class SeleniumDriver {
public static void main(String[] args) throws InterruptedException {
// compatible with Selenium 4.24.0
FirefoxOptions options = new FirefoxOptions();
options.addArguments("-headless");
WebDriver driver = new FirefoxDriver(options);
// logging in on the router web page
driver.get("http://192.168.0.1/webpages/index.html#/login");
Wait<WebDriver> wait = new WebDriverWait(driver, Duration.ofSeconds(5));
WebElement password = wait.until(webDriver -> driver.findElement(By.xpath("//input[@type='password']")));
WebElement loginBtn = wait.until(webDriver -> driver.findElement(By.xpath("//button[@class='login-btn']")));
password.sendKeys("<MY_LOCAL_PASSWORD>");
loginBtn.click();
wait.until(webDriver -> driver.findElement(By.xpath("//div[@data='advancedTopMenuBtn']")));
// continue interaction
driver.quit();
}
}
However, when embedding the same piece of code in MainActivity class and running it from my Android phone device, I’m getting this kind of error – “java.lang.RuntimeException: Unable to find a free port at org.openqa.selenium.net.PortProber.findFreePort(PortProber.java:61)“. Switching to ChromeDriver doesn’t help.
Failed to find a solution to this problem, I’ve come to conclusion that Selenium just doesn’t provide mobile drivers to run standalone automated scripts packaged as apps on a real Android device. So, the question is the same as in the title of this topic: How can I automate mobile web browser interaction on a real Android device? Could you, please, guide me through possible solutions to this problem (if they do exist).
As an answer to your question “How can I automate mobile web browser interaction on a real Android device? “
Selenium wouldn’t directly work with your web browser on mobile, you would have to make a connection first using Appium (https://appium.io/docs/en/2.1/quickstart/test-java/) which is an open source test automation framework for mobile devices.
Aside from that, firstly you need to enable developer options and USB debugging in your mobile device.
Then install the Selenium Webdriver, Appium Webdriver, Selenium chrome driver and selenium support packages. Also remember that the driver version and your chrome version should be the same for it to work.
I did this in C# so I am attaching my code as reference to what configurations you might have to add in java
For the device name, install the adb debugger in your pc, then connect your mobile device and open a terminal and type adb devices
to list all connected devices and use the device name in the appium options.
Now open appium(let the default port and host stay) and go to Advanced Settings, and add the Chrome driver path as the Chrome Driver binary path
Appium UI
Appium Options:
AppiumOptions options = new AppiumOptions();
options.AddAdditionalCapability("deviceName", "6f476994");
options.AddAdditionalCapability("platformVersion", "14");
options.AddAdditionalCapability("platformName", "Android");
options.AddAdditionalCapability("browserName", "Chrome");
Chrome Options:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.UseSpecCompliantProtocol = false;
options.GetMergeResult(chromeOptions);
Then you can proceed with your code normally
var driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4723/wd/hub"), options);
Thread.Sleep(3000);
driver.Url = "https://adactinhotelapp.com/index.php";
Thread.Sleep(5000);
driver.FindElementById("username").SendKeys("");
driver.FindElementById("password").SendKeys("");
driver.FindElementById("login").Click();
Finally when you run the code, it should perform the relevant actions in your chrome web browser on android.
Wahaj Javed is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3