带触摸事件的WEbdriver

时间:2013-04-03 15:47:04

标签: touch webdriver

我想使用适用于iOS iPad或Android平板电脑的Java代码在Selenium WebDriver中使用触摸/点击事件。

我该怎么做?

2 个答案:

答案 0 :(得分:2)

是的,您可以使用AndroidDriver来测试Android设备上的Web应用程序/网站 链接:Android WebDriver
对于iOS,有适用于iPhone的WebDriver 链接:iPhone WebDriver
另外,请检查一下:  WebDriver for Mobile Browsers

因为,驱动程序用于Web视图/页面的自动化测试,它们没有开箱即用的触摸/点击事件,如本机应用程序自动化框架。
您应该尝试使用jQuery Mobile API在网页上执行触摸/点击事件。使用JavascriptExecutor selenium webdriver并使用执行程序执行jquery 检查一下:jQuery Mobile Tap

答案 1 :(得分:1)

对于Android TouchEvents可用。

以下是一个代码段,用于移动旧金山天气的Google搜索结果页面上的滑块。我不能保证你会得到与谷歌决定的搜索查询相同的结果:)但是我希望这个例子足以让你入门。

此示例使用Junit 4测试运行器,您可以调整代码以在Junit 3和其他测试运行程序中运行等。

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();

    }
}

注意:如果您希望测试适用于iOS和Android平台,您将需要另一种方法,可能使用JQuery Mobile,如您对问题的另一个答案所建议的那样。以下文章还介绍了如何使用JQuery模拟iOS http://seleniumgrid.wordpress.com/2012/06/01/how-to-simulate-touch-actions-using-webdriver-in-iphone-or-ipad/

的触摸事件