Selenium搭配TestNG
用Maven来构建TestNG依赖:
<dependency><br /> <groupId>org.testng</groupId><br /> <artifactId>testng</artifactId><br /> <version>6.11</version><br /></dependency><br /><br />
import org.openqa.selenium.By;<br />import org.openqa.selenium.WebDriver;<br />import org.openqa.selenium.firefox.FirefoxDriver;<br />import org.testng.Assert;<br />import org.testng.Reporter;<br />import org.testng.annotations.AfterMethod;<br />import org.testng.annotations.BeforeMethod;<br />import org.testng.annotations.Test;<br /><br />public class TestNG {<br /> private WebDriver driver;<br /><br /> @BeforeMethod<br /> //测试之前的准备工作<br /> public void beforeMethod(){<br /> System.setProperty("webdriver.firefox.marionette", "src/main/resourcec/geckodriver.exe");<br /> String baiduHomePage;<br /> baiduHomePage = "https://www.baidu.com/";<br /><br /> driver = new FirefoxDriver();<br /> driver.manage().window().maximize();<br /> driver.get(baiduHomePage);<br /> try {<br /> Thread.sleep(2000);<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /><br /> Assert.assertEquals(driver.getTitle(), "百度一下,你就知道");<br /> }<br /><br /> @Test<br /> //测试用例<br /> public void testNG(){<br /> driver.findElement(By.xpath(".//*[@id='kw']")).sendKeys("Selenium");<br /> driver.findElement(By.xpath(".//*[@id='su']")).click();<br /> try {<br /> Thread.sleep(2000);<br /> } catch (InterruptedException e) {<br /> e.printStackTrace();<br /> }<br /><br /> Reporter.log("搜索Selenium的测试用例");<br /> Assert.assertEquals(driver.getTitle(), "Selenium_百度搜索");<br /> }<br /><br /> @AfterMethod<br /> //测试之后的清理工作<br /> public void afterMethod(){<br /> driver.close();<br /> driver.quit();<br /> }<br />}
相关推荐
ITstudied 2020-05-06
cintmain 2020-04-20
wndong 2020-03-01
86427019 2019-12-21
SolitudeSky 2019-12-09
wugang0 2019-12-09
lucialee 2019-11-08
mohanzb 2019-10-22
mrjianghaijun 2015-07-30
chengzhezhijian 2015-06-24
pengzhanxuan 2012-11-14
不忘初心才能始终 2019-07-01
80487608 2015-07-29
石羊 2019-06-30
aduocd 2019-06-30
mrsuddenflash 2013-11-18