Selenium AndroidDriver使用(一)


参考:http://code.google.com/p/selenium/wiki/AndroidDriver


在http://code.google.com/p/selenium/downloads/list或http://seleniumhq.org/download/

下载Selenium IDE//用于在FireFox上录制Selenium脚本(html),并且可以将Selenium脚本(html)Export为JUnit 4 /WebDriver或其它语言的代码。

下载Test Suite Batch Converter//用于扩展Selenium IDE的Export功能。也是FireFox的plug

下载selenium-server-standalone-2.25.0.jar//JUnit工程需要的libs。用于向WebDriver发送命令。

下载android-server-2.21.0.apk//安装在android手机上,用于接收、处理selenium客户端传来的各种命令。


打开FireFox,通过Selenium IDE录制测试脚本,保存为DemoDroid.html,并运行一下。OK。

然后在Selenium IDE->File->Batch convert test cases->Java/ JUnit 4/ WebDriver,保存为DemoDroid.java。

大概是这样的:

package com.atest;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class BaiduKitty {
	private WebDriver driver;
	private String baseUrl;
	private StringBuffer verificationErrors = new StringBuffer();
	@Before
	public void setUp() throws Exception {
		driver = new FirefoxDriver();
		baseUrl = "http://www.baidu.com/";
		driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	}

	@Test
	public void testBaiduKitty() throws Exception {
		// open | /index.html | 
		driver.get(baseUrl + "/index.html");
		// type | id=kw | Hello kitty
		driver.findElement(By.id("kw")).clear();
		driver.findElement(By.id("kw")).sendKeys("Hello kitty");
		// click | id=su | 
		driver.findElement(By.id("su")).click();
		// assertText | css=a > em | hello kitty
		assertEquals("hello kitty", driver.findElement(By.cssSelector("a > em")).getText());
	}

	@After
	public void tearDown() throws Exception {
		driver.quit();
		String verificationErrorString = verificationErrors.toString();
		if (!"".equals(verificationErrorString)) {
			fail(verificationErrorString);
		}
	}

	private boolean isElementPresent(By by) {
		try {
			driver.findElement(by);
			return true;
		} catch (NoSuchElementException e) {
			return false;
		}
	}
}




新建一个java工程SeleniumDemo。导入libs(selenium-server-standalone-2.25.0.jar)。将DemoDroid.java 拷进src里。


安装android-server-2.21.0.apk到手机上(2.3.x以上)。并运行

在电脑上cmd。

>adb devices
* daemon not running. starting it now on port 5037 *
* daemon started successfully *
List of devices attached
0163D4701901D01E        device

>adb -s 0163D4701901D01E forward tcp:8080 tcp:8080


DemoDroid.java上Run as JUnit Test

 OK  OK  OK


ps:

如果是FireFox  for Win,

System.setProperty("webdriver.firefox.bin","D:/Program Files/Mozilla Firefox/firefox.exe");
driver = new FirefoxDriver();


如果是Chrome for Win,

下载ChromeDriver

System.setProperty("webdriver.chrome.driver", "E:/write/auto_test/chromedriver.exe");
driver = new ChromeDriver();



s



s

你可能感兴趣的:(auto_test,android,paper)