Application of TESTNG test framework in Web automation test based on selenium

Source: Internet
Author: User
Tags testng

This test framework can execute the written test case in a custom order, taking the selenium webdriver automated test case as an example:

1. Create a new Java project named Fortestng, and then make a Libs folder to import all and selenium related jar packages:

2. Install testng, click Help->install New Software in Eclipse, click Add,location Fill in "Http://beust.com/eclipse", then click OK:

Then tick testng and click Next to install:

Then click Next:

Click Accept and Finish:

To install:

Click OK:

Click Yes to restart Eclipse when the installation is complete:

3. After eclipse restarts, you can find one more testng jar package in the Libs folder:

Select all the jar packages under the Libs folder and right-click->build path->add to Build Path:

4. Create a new package named fortestng:

Press "CTRL + N" To select the TestNG class and click Next:

Fill in the following information (Select source folder, select Package name, give your testng test Class A class name, tick two annotations, give the XML file a name), click Finish:

The Newtest class code is as follows (@BeforeMethod method executes before each @test method executes, @AfterMethod method executes after each @test method executes):

 Packagefortestng;Importorg.testng.annotations.Test;ImportUserinfo.userinfo;ImportOrg.testng.annotations.BeforeMethod;ImportOrg.testng.annotations.AfterMethod;ImportJava.util.logging.Level;ImportJava.util.logging.Logger;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver; Public classNewtest { PublicWebdriver Webdriver; UserInfo Usrinfo=NewUserInfo (); @Test Public voidTestA ()throwsinterruptedexception {waitforlinktext (Webdriver,Login); Webdriver.findelement (By.linktext (Login) . Click (); Waitforid (Webdriver,"INPUT1"); Webdriver.findelement (By.id ("INPUT1") . SendKeys (Usrinfo.usrname); Webdriver.findelement (By.id ("Input2") . SendKeys (USRINFO.PWD); Webdriver.findelement (By.id ("Signin") . Click (); System.out.println ("Login successfully."); } @Test Public voidTESTB ()throwsinterruptedexception {waitforlinktext (Webdriver,"çâ" ¢â»ã¥â½â€¢ "); Webdriver.findelement (By.linktext ("çâ" ¢â»ã¥â½â€¢ ") . Click (); Waitforid (Webdriver,"INPUT1"); Webdriver.findelement (By.id ("INPUT1") . SendKeys (Usrinfo.usrname); Webdriver.findelement (By.id ("Input2") . SendKeys (USRINFO.PWD); Webdriver.findelement (By.id ("Signin") . Click (); System.out.println ("Login successfully."); } @BeforeMethod Public voidBeforemethod () {System.out.println ("Before method."); Webdriver=NewFirefoxdriver (); String URL= "Http://cnblogs.com";        Webdriver.get (URL); System.out.println ("Navigate successfully."); } @AfterMethod Public voidAftermethod () {System.out.println ("After method.");        Webdriver.quit (); System.out.println ("Quit successfully."); }    Private Static voidWaitforid (Webdriver webdriver, String ID)throwsinterruptedexception {Try {
Thread.Sleep (+); Webdriver.findelement (by.id (id)); } Catch(Exception e) {Logger log= Logger.getlogger ("Lavasoft"); Log.setlevel (Level.info); Log.info (E.tostring ()); } } Private Static voidWaitforlinktext (Webdriver webdriver, String linkText)throwsinterruptedexception {Try {
Thread.Sleep (+); Webdriver.findelement (By.linktext (LinkText)); } Catch(Exception e) {Logger log= Logger.getlogger ("Lavasoft"); Log.setlevel (Level.info); Log.info (E.tostring ()); } }}

5. Create a new UserInfo package and a UserInfo class to store the user information:

The code is as follows:

 Package UserInfo;  Public class UserInfo {    public  String usrname;      Public String pwd;      Public UserInfo () {        = "user name";         = "password";    }}

6. Modify the Xmltest.xml file as follows (specifies the order of execution of the test method):

<?XML version= "1.0" encoding= "UTF-8"?><Suitename= "Suite"Parallel= "false">    <Testname= "Test"Preserve-order= "true">        <Classes>            <classname= "Fortestng.newtest" />            <Methods>                <includename= "Testb" />                <includename= "TestA" />            </Methods>        </Classes>    </Test> <!--Test -</Suite> <!--Suite -

The above code specifies that the method Testb is executed first, and then the method Testa is executed;

7. In the XML file, right-click the run as->testng Suite running test:

The result of the operation is as follows: we can see the test results of the corresponding method in the console.

As you can see from the summary information, a total of two test methods have been run, one of which failed, and we can see that there is an exception that locate the link element, and under the Results of running Suite column, you can see whether the test results pass the message:

Method Testb Test Failed, this is due to input in the method Testb garbled linktext caused by:

  @Test  public  void  testb ()  Interruptedexception {Waitforlinktext (Webdriver,  "çâ" ¢â»ã¥â½â€¢ " "çâ" ¢â»ã¥â½â€¢ ""). Click ();    Waitforid (Webdriver,  "input1" );    Webdriver.findelement (By.id ( "input1"  "Input2"  "signin" )). Click (); System.out.println ( "Login successfully.")  

*, if you run an error, you are prompted not to find classpath--create "testng_home" environment variable, the value is "D:\TestNG" (Here I put "Testng-6.9.9.jar" this jar package on the D-disk path):

Create a CLASSPATH environment variable with a value of "%classpath%;%testng_home%\testng-6.9.9.jar;" :

Then restart Eclipse and run the test.

8, further to see the test results, first find the folder where the project, found that there is a Test-output folder:

Under the Suite folder under it, open "test.html":

You can then see the test results (red for failure, green for pass):

More usage of the TESTNG test framework can be seen here.

Application of TESTNG test framework in Web automation test based on selenium

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.