`
xinklabi
  • 浏览: 1591204 次
  • 性别: Icon_minigender_1
  • 来自: 吉林
文章分类
社区版块
存档分类
最新评论

Testng + Webdriver 入门篇

 
阅读更多

转自:http://www.cnblogs.com/GGHHLL/archive/2013/06/07/3124121.html

断断续续摸索了几个星期终于将这个入门篇成文了。从Testng 官网http://testng.org/ 都能找到这些资料。

  1. 首先安装Eclipse 的Testng 插件:这里 http://testng.org/doc/download.html 已经很详细了。记得重启eclipse。
  2. 建工程 (mvn 的安装见 http://maven.apache.org/download.html)
  3.      a). 用mvn 先建立工程
    1. 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 即可。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics