5

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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user93796
  • 18,749
  • 31
  • 94
  • 150
  • 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... ) https://www.selenium.dev/documentation/ – pcalkins Feb 15 '22 at 19:40
  • @pcalkins Can I run my code + the webdriver + and android webview all within my android app? – user93796 Feb 16 '22 at 03: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. – pcalkins Feb 17 '22 at 19:54
  • If I understand what you are trying to do, you might want to take a look at this: https://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.) – pcalkins Feb 17 '22 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. – user93796 Feb 18 '22 at 07:55
  • @user93796 What you need is a headless browser, something which a lot browser engines are capable of, but, alas, not in regards to Android. Also please check this question, see if it helps you - https://stackoverflow.com/questions/17399055/android-web-scraping-with-a-headless-browser – Yevgen Feb 19 '22 at 19:46
  • Please don't change the question based on which you have received well researched answers. Once you receive canonical answers changing the question can make all the existing answers invalid and may not be useful to future readers. If your requirement have changed feel free to raise a new question. StackOverflow contributors will be happy to help you out. For the time being I have reverted back the question to it's initial state. – undetected Selenium Feb 20 '22 at 20:06
  • That is why i added it under EDIT section. Ultimate goal is to solve the original problem, noting wrong in updating question with edit section. – user93796 Feb 21 '22 at 20:52
  • @user93796 Thats exactly not how StackOverflow works. If the premise of your question have changed you should ask a new question as changing the question can make all the existing answers invalid and may not be useful to future readers. – undetected Selenium Feb 21 '22 at 21:23

3 Answers3

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.

Tal Angel
  • 1,301
  • 3
  • 29
  • 63
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.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • 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 Feb 18 '22 at 07:48
  • @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. – undetected Selenium Feb 20 '22 at 20:05
  • 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. – user93796 Feb 21 '22 at 20:47
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.

djmonki
  • 3,020
  • 7
  • 18
  • 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. https://htmlunit.sourceforge.io/ – user93796 Feb 21 '22 at 20:43
  • I have tried code like u mention above. But it needs chrome webdriver binaries. Are they available for android? – user93796 Feb 21 '22 at 20:44