Selenium AndroidDriver使用(一)

Selenium AndroidDriver使用(一) - andych008的专栏 - 博客频道 - CSDN.NET

 

Selenium AndroidDriver使用(一)

分类: android auto_test paper 272人阅读 评论(1) 收藏 举报

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

http://code.google.com/p/selenium/downloads/listhttp://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。

大概是这样的:

  1. package com.atest;  
  2.   
  3. import java.util.regex.Pattern;  
  4. import java.util.concurrent.TimeUnit;  
  5. import org.junit.*;  
  6. import static org.junit.Assert.*;  
  7. import static org.hamcrest.CoreMatchers.*;  
  8. import org.openqa.selenium.*;  
  9. import org.openqa.selenium.firefox.FirefoxDriver;  
  10. import org.openqa.selenium.support.ui.Select;  
  11.   
  12. public class BaiduKitty {  
  13.     private WebDriver driver;  
  14.     private String baseUrl;  
  15.     private StringBuffer verificationErrors = new StringBuffer();  
  16.     @Before  
  17.     public void setUp() throws Exception {  
  18.         driver = new FirefoxDriver();  
  19.         baseUrl = "http://www.baidu.com/";  
  20.         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);  
  21.     }  
  22.   
  23.     @Test  
  24.     public void testBaiduKitty() throws Exception {  
  25.         // open | /index.html |   
  26.         driver.get(baseUrl + "/index.html");  
  27.         // type | id=kw | Hello kitty  
  28.         driver.findElement(By.id("kw")).clear();  
  29.         driver.findElement(By.id("kw")).sendKeys("Hello kitty");  
  30.         // click | id=su |   
  31.         driver.findElement(By.id("su")).click();  
  32.         // assertText | css=a > em | hello kitty  
  33.         assertEquals("hello kitty", driver.findElement(By.cssSelector("a > em")).getText());  
  34.     }  
  35.   
  36.     @After  
  37.     public void tearDown() throws Exception {  
  38.         driver.quit();  
  39.         String verificationErrorString = verificationErrors.toString();  
  40.         if (!"".equals(verificationErrorString)) {  
  41.             fail(verificationErrorString);  
  42.         }  
  43.     }  
  44.   
  45.     private boolean isElementPresent(By by) {  
  46.         try {  
  47.             driver.findElement(by);  
  48.             return true;  
  49.         } catch (NoSuchElementException e) {  
  50.             return false;  
  51.         }  
  52.     }  
  53. }  


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

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

在电脑上cmd。

  1. >adb devices  
  2. * daemon not running. starting it now on port 5037 *  
  3. * daemon started successfully *  
  4. List of devices attached  
  5. 0163D4701901D01E        device  
  6.   
  7. >adb -s 0163D4701901D01E forward tcp:8080 tcp:8080  


DemoDroid.java上Run as JUnit Test

 OK  OK  OK

ps:

如果是FireFox  for Win,

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


如果是Chrome for Win,

下载ChromeDriver

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


s

s

原文地址:https://www.cnblogs.com/lexus/p/2828916.html