Testng + Webdriver 入门篇
- - 研发管理 - ITeye博客转自:http://www.cnblogs.com/GGHHLL/archive/2013/06/07/3124121.html. 断断续续摸索了几个星期终于将这个入门篇成文了. 从Testng 官网 http://testng.org/ 都能找到这些资料. 首先安装Eclipse 的Testng 插件:这里 .
断断续续摸索了几个星期终于将这个入门篇成文了。从Testng 官网 http://testng.org/ 都能找到这些资料。
mvn archetype:generate -DgroupId=com.testchina -DartifactId=my_testng_test -DinteractiveMode=false
b). eclipse 导入现有的以上生产的 mvn工程
c). 修改pom.xml 添加以下selenium 和 testng 依赖
<dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>2.25.0</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.7</version> <scope>test</scope> </dependency>
3. 添加类HomePage, 与 前一篇的HomePage 一样
右击HomePage, 新建 TestNG 类 ,记得在xml suit file 中输入testng.xml . 至此文件结构为:
src/main/java -------HomePage.java src/test/java -------HomePageTest.java -------testng.xml pom.xml
4. 修改 HomePageTest 测试类
public class HomePageTest { protected static WebDriver driver; @BeforeClass(groups = { "ff", "ie" }) public void beforeClass() { driver.get("http://www.baidu.com"); } @AfterClass public void afterClass() { } @BeforeSuite(groups = { "ie" }) public void beforeSuite_runOnIE() { driver = new InternetExplorerDriver(); } @BeforeSuite(groups = { "ff" }) public void beforeSuite_runOnFF() { File pathToFirefoxBinary = new File( "C:\\Program Files (x86)\\FF10\\firefox.exe"); FirefoxBinary firefoxbin = new FirefoxBinary(pathToFirefoxBinary); driver = new FirefoxDriver(firefoxbin, null); } @AfterSuite(groups = { "ff", "ie" }) public void afterSuite() { driver.quit(); } @Parameters({ "searchTXT" }) @Test(groups = { "ie" }) public void HomePage(String searchTxt) { HomePage homepage = new HomePage(driver); homepage.enterSerchTxt(searchTxt); homepage.clickSerchButton(); // maybe the net will delay, so wait for while try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } homepage.checkResult(); } @Parameters({ "searchTXT2" }) @Test(groups = { "ff" }) public void HomePage2(String searchTxt) { HomePage homepage = new HomePage(driver); homepage.enterSerchTxt(searchTxt); homepage.clickSerchButton(); // maybe the net will delay, so wait for while try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } homepage.checkResult(); } }
以上用到了Group 和 Parameters。 参数在xml中进行定义, testng.xml如下:
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite" parallel="false"> <test name="Test"> <parameter name="searchTXT" value="ie" /> <parameter name="searchTXT2" value="ff" /> <groups> <run> <include name="ff" /> </run> </groups> <classes> <class name="com.testchina.HomePageTest" /> </classes> </test> <!-- Test --> </suite> <!-- Suite -->
最后eclipse里配置运行 选择Suite,运行该xml 即可。