因为公司有wap和微信专区渠道,我都是在电脑上模拟的,所以想试一下自动化测试能否模拟,发现可以,代码如下:
package testsuite;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import framework.Base;
public class runtest {
public static WebDriver driver = null;
@SuppressWarnings("deprecation")
@BeforeClass
public void beforeClass(){
System.setProperty("webdriver.chrome.driver", ".\\chromedriver.exe");
Map<String, String> mobileEmulation = new HashMap<String, String>();
//设置设备,例如:Google Nexus 7/Apple iPhone 6
//mobileEmulation.put("deviceName", "Google Nexus 7");
mobileEmulation.put("deviceName", "iPhone X"); //这里是要使用的模拟器名称,就是浏览器中模拟器中的顶部型号
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
try {
System.out.println("开始启动driver~~~");
driver = new ChromeDriver(capabilities);
System.out.println("启动driver成功~~~");
} catch (Exception e) {
System.out.println("启动driver失败~~~");
System.out.println(e.getMessage());
}
driver.get("https://www.baidu.com");
}
@Test
public void login(){
}
}
执行后网页显示如下:
之后就可以对元素进行操作了,模拟了手机网页自动化测试