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();
}
}
Works good for me.
I have managed to prove that
By.xpath
andBy.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
And here's the modified version of the code in your question:
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:
sendKeys(...)
call appends text to the search queryBy(...)
clauses work, however it's good to see the code execute and get some matching search results.AndroidDriver()
.