POM=Page Object Model,DDT=Date Driver Test
第一个设计点,就是支持同一个测试用例可以在不同浏览器下测试。我们这里只考虑winodws平台的浏览器,主要是ie和chrome,那到底要怎么做呢,才能支持一个自动化用例能够在不同浏览器上运行?通过读取配置文件的值,来切换不同浏览器运行测试脚本。
设计思路:
1. 写一个配置文件,例如properties文件,里面写好浏览器的类型和测试服务器的地址,方便切换
2 .编写一个浏览器引擎类,通过读取配置文件加上if语句去控制启动不同浏览器。
3. 测试脚本调用浏览器引擎类实例,得到driver,开始测试自动化脚本
4.利用TestNG编写一个测试类文件,测试切换不同浏览器是否脚本运行成功。
IDEA中maven的目录如下:
1、配置config.properties文件如上图,通过注释和去注释改变程序的判断条件;
2、浏览器引擎类(BrowserEngine)代码如下:
package framework;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class BrowserEngine {
public String browserName;
public String serverURL;
public WebDriver driver;
public void initConfigData() throws IOException {
/*Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,在Java中,其配置文件常为.properties文件,格式为文本文件,文件的内容的格式是“键=值”的格式,
文本注释信息可以用"#"来注释。主要方法如下:
getProperty ( String key) 通过参数 key ,得到 key 所对应的 value
load ( InputStream inStream) 从输入流中读取属性列表(键和元素对),以供 getProperty ( String key) 来搜索
setProperty ( String key, String value) 调用 Hashtable 的方法 put 。他通过调用基类的put方法来设置 键 - 值对
store ( OutputStream out, String comments) 与 load 方法相反,该方法将键 - 值对写入到指定的文件中去。
clear (),清除所有装载的 键 - 值对
*/
Properties p=new Properties();
//加载配置文件
InputStream ips=new FileInputStream("E:\\Java_project\\XTPTest_selenium\\suites\\config.properties");
//InputStream是抽象类,FileInputStream是InputStream的子类
p.load(ips);
//日志
browserName=p.getProperty("browserName");
//
serverURL=p.getProperty("URL");
//
ips.close();
}
public WebDriver getBrowser(){
if(browserName.equalsIgnoreCase("chrome")){
System.setProperty("webdriver.chrome.driver","E:\\Java_project\\XTPTest_selenium\\src\\main\\resources\\chromedriver.exe");
driver=new ChromeDriver();
//
}
else if(browserName.equalsIgnoreCase("IE")){
System.setProperty("webdriver.ie.driver","E:\\Java_project\\XTPTest_selenium\\src\\main\\resources\\IEDriverServer.exe");
driver=new InternetExplorerDriver();
//
}
driver.get(serverURL);
//
driver.manage().window().maximize();
//
callWait(5);
return driver;
}
public void tearDown() throws InterruptedException{
//
driver.quit();
Thread.sleep(3000);
}
//隐式等待
public void callWait(int time){
driver.manage().timeouts().implicitlyWait(time,TimeUnit.SECONDS);
//
}
}
3、新建一个测试类,测试上面写的代码:
package testsuite;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import framework.BrowserEngine;
import org.testng.annotations.Test;
import java.io.IOException;
public class TestLaunchBrowser {
public WebDriver driver;
@BeforeClass
public void setUp() throws IOException{
BrowserEngine browserEngine=new BrowserEngine();
browserEngine.initConfigData();
driver=browserEngine.getBrowser();
}
@Test
public void clickNews(){
driver.findElement(By.id("key")).sendKeys("iPhone 7");
driver.findElement(By.xpath("//*[@id='search']/div/div[2]/button")).click();
}
@AfterClass
public void tearDown(){
driver.quit();
}
}
这里要强调的就是setUp方法中,如何获取浏览器driver这个实例对象并赋值给当前测试脚本中定义的driver,这个一定要理解好。一句话解释就是,你在浏览器引擎类定义了一个driver对象,在测试脚本中又定义了一个driver对象,你需要保持整个测试过程,只有一个唯一的driver,否则会报错,测试脚本不会执行查找元素和点击事件。这个建议要好好去理解一下这个为什么要保持唯一性,为什么赋值。这个是设计自动化测试框架的核心的一点思想和必须要面临和解决的问题,参考一个程序只能有一个main方法一样的道理。
参考博文:https://blog.csdn.net/u011541946/article/details/76598441