Reputation: 69
I wanted to use touch/tap events in Selenium WebDriver using Java code for iOS iPad or Android tablets.
How can I do this?
Upvotes: 3
Views: 13192
Reputation: 3286
For Android TouchEvents are available.
Here is a code snippet that moves a slider on the Google Search Results page for weather in San Francisco. I cannot guarantee you will get the same results for the search query that's what Google decides :) however I hope this example is enough to get you started.
This example uses the Junit 4 test runner, you can tweak the code to run in Junit 3 and other test runners, etc.
package org.example.androidtests;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;
import org.openqa.selenium.interactions.touch.TouchActions;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AndroidDemoTest {
@Test
public void test() throws InterruptedException {
AndroidDriver driver = new AndroidDriver();
Wait<WebDriver> wait = new WebDriverWait(driver, 20);
driver.get("http://www.google.co.uk");
WebElement q = driver.findElement(By.name("q"));
q.sendKeys("weather in san francisco");
q.submit();
WebElement slider = wait.until(visibilityOfElementLocated(By.className("wob_shi")));
Point location = slider.getLocation();
Dimension size = slider.getSize();
// Find the center of this element where we we start the 'touch'
int x = location.getX() + (size.getWidth() / 2);
int y = location.getY() + (size.getHeight() / 2);
new TouchActions(driver).down(x, y).move(150, 0).perform();
}
}
Note: if you want to have tests that work on both iOS and Android platforms, you will need another approach, perhaps using JQuery Mobile as suggested in another answer to your question. The following article also describes how to use JQuery to simulate touch events for iOS http://seleniumgrid.wordpress.com/2012/06/01/how-to-simulate-touch-actions-using-webdriver-in-iphone-or-ipad/
Upvotes: 1
Reputation: 1656
Yes there is AndroidDriver that you can use to test a web application / web site on an Android device.
Link: Android WebDriver
For iOS, there is WebDriver for iPhone
Link: iPhone WebDriver
Also, do check this:
WebDriver for Mobile Browsers
Since, the drivers are for automation testing of web views/pages they don't have out of the box touch/tap events like native app automation frameworks.
You should try to use jQuery Mobile API to perform touch/tap events on web-pages. Use JavascriptExecutor of selenium webdriver and execute jquery using the executor.
Check this: jQuery Mobile Tap
Upvotes: 1