Question

I want to automate android web apps using Selenium WebDriver and i've run a simple program to open the google page and search a term . While finding the element with the name and id it runs perfectly. But while trying to find the element with the Xpath and Css seems difficult. Does anybody know how to use xpath and css path in selenium android webdriver? Here is the sample code i've used:

public class TestDriver extends TestCase 
{

    public static void testGoogle() throws Exception 
    {
    AndroidDriver driver = new AndroidDriver();


    driver.get("http://google.com");
    WebElement element = driver.findElement(By.name("q"));
       //WebElement element = driver.findElement(By.xpath("//*[@id='gbqfq']"));
       //WebElement element = driver.findElement(By.cssSelector("#gbqfq"));
    element.sendKeys("android driver");
    element.submit();
    System.out.println("Page title is: " + driver.getTitle());
    //driver.quit();
  }

}
Was it helpful?

Solution

I have managed to prove that By.xpath and By.cssSelector are able to find elements which can then use in our code to interact with the web page. For a couple of practical reasons I created a shadow, simplified test page which then submits a query to obtain search results from Google's search engine.

Here is the simplified test page, enough to demonstrate the basics. Copy this file to the sdcard of your android device or AVD e.g. using the following command adb push form.html /sdcard/form.html

<html>
<head>
  <title>WebDriver form test page</title>
</head>
<body>
<form name="testform" action="http://www.google.co.uk/search" method="get">
  <input id="gbqfq" class="gbqfif" type="text" value="" autocomplete="off" name="q">
  <input type="submit" value="search Google">
</form>
</body>
</html>

And here's the modified version of the code in your question:

@Test
public void testByClauses() throws Exception {
     AndroidDriver driver = new AndroidDriver();
     // driver.get("http://www.google.co.uk/");
     driver.get("file:///sdcard/form.html");
     System.out.println(driver.getPageSource());

     WebElement nameElement = driver.findElement(By.name("q"));
     WebElement xpathElement = driver.findElement(By.xpath("//*[@id='gbqfq']"));
     WebElement cssElement = driver.findElement(By.cssSelector("#gbqfq"));

     nameElement.sendKeys("android ");
     xpathElement.sendKeys("driver ");
     cssElement.sendKeys("webdriver");
     nameElement.submit();
     System.out.println("Page title is: " + driver.getTitle());
     driver.quit();
}

I happen to be using JUnit 4 as the test runner (hence the @Test annotation) however this code should also work in JUnit 3.

Points to note:

  • I have deliberately used 3 separate WebElements which refer to the same underlying HTML element
  • Each sendKeys(...) call appends text to the search query
  • Technically we don't need to submit the search query to demonstrate the By(...) clauses work, however it's good to see the code execute and get some matching search results.
  • I commented out the request to load Google search's homepage, but left it in my answer so you can easily see how to use the Google site instead of the static local HTML page.
  • I ran the test with a locally connected Nexus 4 phone, over USB, and used a locally built instance of the AndroidDriver().

OTHER TIPS

Works good for me.

//Author: Oleksandr Knyga
function xPathToCss(xpath) {
    return xpath
        .replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
        .replace(/\/{2}/g, '')
        .replace(/\/+/g, ' > ')
        .replace(/@/g, '')
        .replace(/\[(\d+)\]/g, ':eq($1)')
        .replace(/^\s+/, '');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top