selenium 2 关于移动端wap网页的测试,3种解决思路。

本文纯属虚构:

1.使用第三方浏览器模拟,本文拟用chrome。

chrome 支持 主流系统ios、Android的浏览器模拟。

如图



 
  
java 初始化driver
//userAgent 可以通过上图UA栏获取,如:Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53
String userAgent = "user_agent_string" ; chromeOptions co = new ChromeOptions (); co . addArguments ( "--user-agent=" + userAgent ); DesiredCapabilities cap = DesiredCapabilities . chrome (); cap . setCapability ( ChromeOptions . CAPABILITY , co ); WebDriver driver = new ChromeDriver ( cap );

其他语言参考:http://simply-tutorial.com/blog/2014/07/10/selenium-webdriver-set-browsers-user-agent-and-proxy/

2.使用selenium 的androiddriver,iphonedriver,

需要下载
以Android为例,下载 androiddriver以及Android-server.apk,android模拟器(或使用真机) ,速度较慢

1. Setup Android emulator   a. Download the Android SDK              http://developer.android.com/sdk/index.html               Note that there is an emulator bug on Gingerbread((2.3.x) that might cause WebDriver to crash. My testing is on Ice Cream Sandwich (4.0.x)

  b. Install Android SDK:              http://developer.android.com/sdk/installing.html

  c. Start Android SDK Manager (SDK Manager.exe)   d. Select and install Package online   e. Start AVD Manager.exe   f. Create an emulator

2. Install the AndroidDriver APK by using platform-tools    a. list divce name:               adb devices   b. download AndroidDriver APK:               http://code.google.com/p/selenium/downloads/list   c. install AndroidDriver APK:               adb -s emulator-5554 -e install -r c:\android-server-2.21.0.apk   d. start the Android WebDriver application              adb -s emulator-5554 shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity   e. setup the port forwarding in order to forward traffic from the host machine to the emulator               adb -s emulator-5554 forward tcp:8080 tcp:8080

3. 编写测试脚本运行: import junit.framework.TestCase;    import org.openqa.selenium.By;  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();    }  } 来自:http://blog.csdn.net/gxlujun/article/details/7520558

3.使用appium /或者robotium等,如果单单测试wap页,此方法不推荐。效率不高.有种高射炮打蚊子的意思。不在赘述,自行google







你可能感兴趣的:(java,selenium)