TestNG, testing next Generation, the next generation of test technology, is a test framework built on JUnit and nunit ideas that uses annotations to enhance testing capabilities, which can be used for unit testing or integration testing.
Installation: Help-->install New software
Click Add to enter the dialog box that pops up:
Click OK to install all the way
TestNG combined with Selenium
- New Java Project Selenium_testng_test
- Import Selenium and TestNG class libraries: Project Right--->build Path-->add libraries Add selenium and testng class libraries respectively
- Create a new package name under the SRC directory: com.testng.test
- Create a new testng test class, name Newtest
Implementation code:
Packagecom.testng.test;Importorg.testng.annotations.Test;Importorg.testng.annotations.BeforeTest;Importorg.testng.annotations.AfterTest;ImportJava.util.concurrent.TimeUnit;Importorg.openqa.selenium.By;Importorg.openqa.selenium.TimeoutException;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;Importorg.openqa.selenium.support.ui.ExpectedCondition;Importorg.openqa.selenium.support.ui.WebDriverWait;ImportOrg.testng.Assert; Public classNewtest {//declares a global variable driver PrivateWebdriver Driver; @Test Public voidF () {by InputBox= By.id ("kw"); by SearchButton= By.id ("su"); Intelligenwait (Driver,10, InputBox); Intelligenwait (Driver,10, SearchButton); Driver.findelement (InputBox). SendKeys ("JAVA"); Driver.findelement (SearchButton). Click (); Try{Thread.Sleep (2000); } Catch(interruptedexception e) {e.printstacktrace (); }} @BeforeTest Public voidbeforetest () {driver=NewFirefoxdriver (); Driver.manage (). Timeouts (). Pageloadtimeout (10, Timeunit.seconds); Driver.manage (). window (). Maximize (); Driver.get ("Http://www.baidu.com"); } @AfterTest Public voidaftertest () {driver.quit (); } /**This is how smart waits for an element to load **/ Public voidIntelligenwait (Webdriver driver,intTimeOut,Finalby by ) { Try { (NewWebdriverwait (Driver, TimeOut)). Until (NewExpectedcondition<boolean>(){ PublicBoolean Apply (webdriver driver) {webelement element=driver.findelement (by); returnelement.isdisplayed (); } }); } Catch(TimeoutException e) {assert.fail (Timeout "+ TimeOut +" seconds after the element has not been found ["+ by +"] ", E); } }}
Use of Selenium2 (Java) testng Seven