• [原创]Testng + Webdriver 入门篇


    断断续续摸索了几个星期终于将这个入门篇成文了。从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 即可。

  • 相关阅读:
    Java模拟ATM运行过程(第一版)
    双色Hanoi塔问题及判断指令
    一点编程风格
    游戏程序员养成计划
    OGRE编译
    Python学习收藏
    case 语句注意
    [c、c++]宏中"#"和"##"的用法(zz)
    网络代码平台tortoise svn+google code
    使用delegate进行解耦
  • 原文地址:https://www.cnblogs.com/GGHHLL/p/3124121.html
Copyright © 2020-2023  润新知