在selenium官网看到一篇讲述的是关于selenium在android手机上运行的文章,于是也就自己试了试。以android模拟器为例(在手机上运行会更顺畅)
配置环境:android SDK4.04+ Android WebDriver
系统环境:winXP
步骤一:安装模拟器 Android Virtual Device (AVD)
该模拟器的安装不细说,网上一大堆,也可以不装直接用自己的手机也可。
我自己采用的是配置了AVD,然后模拟手机设置了一个1G的SD卡装置,将程序安装在模拟的SDK上;
启动AVD:emulator -avd SDK4.04 -sdcard D:\android-sdk-windows\tools\sdcard.img
步骤二:安装webdrive
从官网下载webdrive的apk:android-server-2.21.0.apk(地址:http://code.google.com/p/selenium/downloads/list)
下载后模拟手机安装到模拟器的sdk上,安装好后如图:
页面上的webDrive代表已经装好了。
启动webdrive,映射8080端口:adb forward tcp:8080 tcp:8080
启动后,看到的手机页面是黑色的屏幕,弹出一个webdrive is already的信息后便进入无尽的等待中....
这个时候不要以为是自己没有装好,而是你手机端的webdrive没有收到请求,没有运行而已;
步骤三、编写junit测试用例
首先依赖包:selenium-java-X.zip
建立junit测试单元,测试google的单元,如下代码:
import static org.junit.Assert.*;
import org.junit.Test;
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;
public class OneTest extends TestCase {
public void testGoogle() throws Exception {
WebDriver driver = new AndroidDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the
// element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
}
@Test
public void test() throws Exception {
this.testGoogle();
fail("Not yet implemented");
}
}
测试单元也编写完成了,webdrive也在运行就绪状态,这个时候便是体现是否可行的时机了,
我们运行junit单元,仔细观察手机:
继续运行:
至此完成手机上webdrive的测试验证。
目前webdrive手机上测试只支持的Andriod手机系统为:Gingerbread (2.3.x), Honeycomb (3.x), Ice Cream Sandwich (4.0.x) and later
另外IOS支持至少4.2以上的版本。
参考文档:http://code.google.com/p/selenium/wiki/AndroidDriver
有兴趣的可以继续参阅后续的junit+webdrive的测试框架搭建!