yjlch1016 发表于 2018-1-24 16:06:22

使用testng.xml组织测试用例

使用testng.xml组织测试用例
测试用例类TeseNG.java:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.*;

public class TestNG {
    private WebDriver driver;

    @BeforeClass
    public void beforeClass() throws InterruptedException {
      System.setProperty("webdriver.firefox.marionette",
                "src/main/resourcec/geckodriver.exe");
      String baiduHomePage;
      baiduHomePage = "https://www.baidu.com/";

      driver = new FirefoxDriver();
      driver.manage().window().maximize();
      driver.get(baiduHomePage);
      Thread.sleep(2000);
      Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
    }

    @Test(dataProvider = "word")
    public void testNG(String keyword, String case_1, String searchTitle)
            throws InterruptedException {
      WebElement webElement = driver.findElement(By.xpath(".//*[@id='kw']"));
      webElement.clear();
      webElement.sendKeys(keyword);

      driver.findElement(By.xpath(".//*[@id='su']")).click();
      Thread.sleep(3000);

      Reporter.log(case_1);
      Assert.assertEquals(driver.getTitle(), searchTitle);
      driver.navigate().refresh();
    }

    @AfterClass
    public void afterClass(){
      driver.close();
      driver.quit();
    }

    @DataProvider(name = "word")
    public static Object[][] data(){
      return new Object[][]{{"Selenium", "搜索Selenium的测试用例", "Selenium_百度搜索"},
                {"JMeter", "搜索JMeter的测试用例", "JMeter_百度搜索"},
                {"Appium", "搜索Appium的测试用例", "Appium_百度搜索"}
    };
    }
}

testng.xml文件:<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="百度搜索的测试计划" verbose="2" preserve-order="true">

    <testname="分别搜索Selenium/JMeter/Appium的测试用例">
      <classes>
            <class name="TestNG" />
      </classes>
    </test>

    <listeners>
      <listener class-name="org.uncommons.reportng.HTMLReporter" />
      <listener class-name="org.uncommons.reportng.JUnitXMLReporter" />
    </listeners>
</suite>

https://images2017.cnblogs.com/blog/1321829/201801/1321829-20180120212459912-49821403.png


页: [1]
查看完整版本: 使用testng.xml组织测试用例