博客很久没更新了,最近在研究python PMO, 趁机给java自动化相关的知识点做个总结。
先说Junit,Testng. 这两个都是用来和java配合做自动化单元测试的框架, Testgn介于Junit3和Junit4之间。但是总的来说Testng是优于Junit. 主要表现在以下几个方面:
1.Testng支持更多的标签:
2.在套件测试方面,Testng引入了组的概念,执行套件测试会更灵活。
3.Testng利用dependOnMethods实现依赖测试,Junit目前还不支持。
再介绍下DDT,数据驱动测试,这个思路在自动化测试也是被广泛运用。下面demo是用Testng结合DDT的例子:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.AfterMethod; import org.testng.annotations.BeforeMethod; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import java.util.concurrent.TimeUnit; public class TestNGDDT { private WebDriver driver; private StringBuilder verificationErrors =new StringBuilder(); @DataProvider(name="testData") public Object[][] testData(){ return new Object[][] { new Object[] {"Test","BDD","BDD"}, new Object[] {"Test","DDT","DDT"}, new Object[] {"Test","UTDD","UTDD"} }; } @BeforeMethod public void beforeMethod() { //若无法打开FireFox浏览器,可设定Firefox浏览器的安装路径 System.setProperty("webdriver.gecko.driver", "src/main/resources/Driver/geckodriver.exe"); //打开Firefox浏览器 driver=new FirefoxDriver(); //设定等待时间为5秒 driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); } @Test(dataProvider="testData") public void testSearch(String searchdata1,String searchdata2,String searchResult) { //打开baidu首页 driver.get("http://www.baidu.com/"); //输入搜索条件 driver.findElement(By.id("kw")).sendKeys(searchdata1+" "+searchdata2); //单击搜索按钮 driver.findElement(By.id("su")).click(); //单击搜索按钮后,等待3秒显示搜索结果 try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } //判断搜索的结果是否包含期望的关键字 Assert.assertTrue(driver.getPageSource().contains(searchResult)); } @AfterMethod public void closeMethod() { driver.quit(); } }