Use of Selenium2 (Java) testng Seven

Source: Internet
Author: User
Tags testng

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

    1. New Java Project Selenium_testng_test
    2. Import Selenium and TestNG class libraries: Project Right--->build Path-->add libraries Add selenium and testng class libraries respectively
    3. Create a new package name under the SRC directory: com.testng.test
    4. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.