4

From within my app I want to hit a website and than perform user action. Currently am using webview but I think webdriver will be easy to use and correct approach.

Current code:

WebView browser = (WebView) view.findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.getSettings().setDomStorageEnabled(true);
browser.getSettings().setUserAgentString(`"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.81 Safari/537.36");`
browser.setWebViewClient(new MyBrowser());
browser.loadUrl("https://myurl.com");

Issues with current code: Its hard to send key stores or use Xpath.

What am looking for? Hit the website using driver and than click buttons etc. Pseudo code as follows:

chat=driver.find_element_by_xpath("/html/somepath")
chat.click()
time.sleep(2)      
search=driver.find_element_by_xpath("/html/body/div[1]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/input")
search.click()

I understand that this is possible using selenium/appium. But what am confused is does selenium/appium also needs a server that runs on a separate machine? I want to run all of the code in my app without external server or any more apps.

Can I just add lib which gives me access to apis like I showed above?

9
  • You can run the webdriver local or remote (if remote you run the webdriver locally, but it communicates with a remote server that hosts a "grid", hub+nodes). So all you need is your code + the webdriver + the browser you are automating. (chromedriver+chrome, geckodriver+firefox, etc... ) selenium.dev/documentation Commented Feb 15, 2022 at 19:40
  • @pcalkins Can I run my code + the webdriver + and android webview all within my android app? Commented Feb 16, 2022 at 3:10
  • I haven't used Appium, but I don't think so. The driver is outside of the app... this is the best way to perform testing since you're going through the user's interface. If your app uses a webview it already has hooks inside that you can use to write your own unit tests from inside the app. Commented Feb 17, 2022 at 19:54
  • If I understand what you are trying to do, you might want to take a look at this: github.com/null-dev/HtmlUnit-Android It's an android port of HTMLUnit. (HTMLUnit is a gui-less browser that takes commands very similar to Selenium's webdriver.) Commented Feb 17, 2022 at 21:12
  • @pcalkins HtmlUnit is is exactly what i wanted. Tried that but looks like this lib is not maintained. I tried to use it but it mimics old browser versions (FireFox 52 max). which the website am tryng to hit doesnot support. The original lib (non android port) has support for latest firefox but doesnot work on andorid. Commented Feb 18, 2022 at 7:55

3 Answers 3

0

You can use the Selenium or Appium without any server. Both are plugins, which means they are basically open code or libraries. You call those objects on your local machine (or phone), you don't call an online remote API.

The Selenium and Appium helps to find elements on a web page or to find elements inside a mobile app. There is absolutely no need for a server here or remote machine.

So, YES, just add the lib which gives you access to api's like you showed above.

Sign up to request clarification or add additional context in comments.

Comments

0

Selenium

Selenium in it's basic form doesn't needs any seperate server to run. Selenium along with it's wide range of tools and libraries that can support the automation of web browsers within the same machine ( i.e. localhost).


WebDriver

At the core of Selenium is WebDriver an interface to write instruction sets that can be run interchangeably in many browsers using each browser's native support for Test Automation. This can be achieved in three simple steps:

Sample code block:

System.setProperty("webdriver.gecko.driver", "/path/to/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.amazon.com/");
driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();

Selenium Grid

However, Selenium also supports a distribution server for scaling browser allocation. If your usecase include steps to run tests in parallel across multiple machines, then Selenium Grid would be your best bet.

Selenium Grid allows the execution of WebDriver scripts on remote machines (virtual or real) by routing commands sent by the client to remote browser instances. It aims to provide an easy way to run tests in parallel on multiple machines.

Selenium Grid would also allow you to run tests in parallel on multiple machines and to manage different browser versions and browser configurations centrally (instead of in each individual test).

Having said that, it does solves a subset of common delegation and distribution problems, but will may not be able to manage your infrastructure, and might not exactly suit to your specific need.


Appium

Similarly, Appium is an open-source tool for automating native, mobile web, and hybrid applications on iOS mobile, Android mobile, and Windows desktop platforms. Hybrid apps which have a wrapper around a webview is a native control that enables interaction with web content. Projects like Apache Cordova make it easier to build apps using web technologies that are then bundled into a native wrapper, creating a hybrid app.

3 Comments

Thanks for detailed answer. I tried to use selenium with chrome driver on android app. But its not able to run find chrome binaries in android. See code in latest edit.
@user93796 Your main question was what am confused is does selenium/appium also needs a server that runs on a separate machine? which I have tried to address in the best possible way. However, not able to run find chrome binaries in android is a valid and a legitimate question, you should ask a new question.
Thanks for the reply. My goal is to get a solution to my problem. I saw u reverted my last edit, request u not do do this.
0

Based on your question and your response comment to the answer provided by @undetectedSelenium, the following assumptions apply:

  1. You are testing a browser within an android phone that is connected to a Windows machine via an adb server running on the Windows machine
  2. The browser under test is Chrome

Install selenium as part of your project:

<dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>4.1.2</version>
    </dependency>
</dependencies>

Sample code block based on your psuedo code and answer provided by @undetectedSelenium

System.setProperty(“webdriver.chrome.driver”, “C:\\path\\to\\chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption(“androidPackage”, “com.android.chrome”);

// By default if the following option is not applied, selenium will take the 1st available
// node provided by the adb server if multiple android devices are attached
options.setExperimentalOption("androidDeviceSerial", deviceId);

WebDriver driver = new ChromeDriver(options);
driver.get("https://www.amazon.com/");
driver.findElement(By.cssSelector("input.nav-input[value='Go']")).click();

The deviceId variable needs to contain the uuid listed as a device from the adb server for the particular device under test, i.e.

options.setExperimentalOption("androidDeviceSerial", 95s572sp0478);

Also you will require the correct Chromedriver for your android device. Check the version of Chrome browser installed on the device and download the correct driver from here for your Windows machine Chromedriver Downloads. Then place into your desired directory and add the directory path into the code.

2 Comments

I am not testing anything. I want to programmatically hit a website from within by app. There is no windows machine or adb. All i have is my andoird app. I want a lib which i can use as headless browser. Eg HtmlUnit which can work on android. htmlunit.sourceforge.io
I have tried code like u mention above. But it needs chrome webdriver binaries. Are they available for android?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.