|
Selenium搭配TestNG
用Maven来构建TestNG依赖:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
</dependency>
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.Reporter;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class TestNG {
private WebDriver driver;
@BeforeMethod
//测试之前的准备工作
public void beforeMethod(){
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);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");
}
@Test
//测试用例
public void testNG(){
driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("Selenium");
driver.findElement(By.xpath(".//*[@id='su']")).click();
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Reporter.log("搜索Selenium的测试用例");
Assert.assertEquals(driver.getTitle(), "Selenium_百度搜索");
}
@AfterMethod
//测试之后的清理工作
public void afterMethod(){
driver.close();
driver.quit();
}
}
|
|