使用Selenium+Java+Juint实现移动web端自动化的代码实现

浏览器: 

  Chrome

首先通过developer模式查看Chrome浏览器支持哪些手机,如图:

在代码中使用ChromeOptions对象的addArguments方法来设置参数,如下代码所示:

package test;

import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Test {

  @Before
  public void setUp() throws Exception {

  }

  @After
  public void tearDown() throws Exception {
  }

  @org.junit.Test
  public void test() {
    // 声明ChromeOptions,主要是给Chrome设置参数.
    final ChromeOptions chromeOptions = new ChromeOptions();
    // 设置user agent的参数为iPhone 6
    chromeOptions.addArguments("--user-agent=iPhone 6");
    final WebDriver driver = new ChromeDriver(chromeOptions);
    // 设置打开的浏览器的屏幕大小,模拟手机屏幕.
    driver.manage().window().setSize(new Dimension(600, 900));
    driver.get("https://meitaichina.com");
  }
}

 

posted @ 2017-06-20 10:51  留白*  阅读(1716)  评论(0编辑  收藏  举报