使用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