使用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");
  }
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM