前置步骤,创建工程(可以testng或普通工程),这里默认是创建的testng工程,前面文章中有讲如何配置好testng工程
当前很多微信小程序都是H5实现,这章主要是讲如何测试H5自动化。
直接上代码
首先:编写了一个公共在网页端打开H5页面的类
package Public;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;
import data.PubValue;
import page.Login_PO;
public class PublicFuction {
public WebDriver my_dr;
public WebDriverWait wait;
PubValue pv = new PubValue();//可以注释掉, PubValue类中主要放了预制参数,chromDriver路径等等
// H5自动化启动
public void mobile(WebDriver my_dr, WebDriverWait wait, String URL) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", pv.chromeDriver);//--pv.chromeDriver 的启动路径,这里可以写死
Map<String, String> mobileEmulation = new HashMap<String, String>();
// 设置设备,例如:Google Nexus 7/Apple iPhone 6
// mobileEmulation.put("deviceName", "Google Nexus 7");
// mobileEmulation.put("deviceName", "iPhone 6 Plus");
// //这里是要使用的模拟器名称,就是浏览器中模拟器中的顶部型号
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~~~");
my_dr = new ChromeDriver(capabilities);
wait = new WebDriverWait(my_dr, 10);
my_dr.get(URL);
System.out.println("启动driver成功~~~");
} catch (Exception e) {
System.out.println("启动driver失败~~~");
System.out.println(e.getMessage());
}
this.my_dr = my_dr;
this.wait = wait;
}
}
编写用例,调用写好的公共类
package testCase.H5;
import org.testng.annotations.Test;
import Public.PublicFuction;
import Public.SqlFuction;
import data.PubValue;
import page.H5.AddArchivesSurvey_PO;
import util.ExcelUtil;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
/**
*
*/
public class AddArchiversSurvey {
PubValue pv = new PubValue();
PublicFuction pc = new PublicFuction();
SqlFuction sf = new SqlFuction();
AddArchivesSurvey_PO as = new AddArchivesSurvey_PO();//定位页面元素的类,当然初学者也可以注释掉,如果
@Test(description="启动类")
public void testAddArchiversSurvey() throws Exception {
// 启动H5页面
pc.mobile(pc.my_dr, pc.wait, pv.archivesSurveyURL);//pv.archivesSurveyURL 访问的H5的页面
Thread.sleep(5000);
WebDriver my_dr = pc.my_dr;
WebDriverWait wait = pc.wait;
//打开页面,后续就可以开展H5自动化,H5页面的自动化跟web端一样
Thread.sleep(2000);
my_dr.quit();
}
}
这个是一个简单的教你如果使用java+selenium就能构建到H5页面的自动化,具体操作步骤,跟普通的web前端一样