------Web Automation testing webdriver+testng--from zero to Proficient (series)
TESTNG is an automated testing framework that uses this test framework to write our automated test cases, which requires a corresponding development environment. In this chapter we will explain the construction of the webdriver+testng development environment.
2.1 Environment Configuration
Installation of 1,ECLIPSE+JDK
This is a relatively basic environment configuration, if you want to use eclipse to develop your code, whether you use Java as a development language, or other like python as a development language, you need to build this environment. So there are a lot of ECLIPSE+JDK environment to build the document, here is not tired of the statement. However, depending on your needs, download the corresponding Eclipse version, and in our application using the TESTNG framework, you can download the pure version of Eclipse.
Installation of 2,testng
Go to the official website download a testng plugin, this work is done within Eclipse, click Help->install New software, followed by the http://beust.com/eclipse,2.1 shown.
Figure 2.1 TestNG plug-in installation
Then choose TestNG, all the way next to finish. After the testng plug-in installation is complete, our test framework is installed successfully.
3. Prepare the required packages for testing
The Testng+webdriver framework requires some corresponding jar package support, before we write the test cases, we prepare the corresponding packages, as follows:
(1) Httpclient-4.3.6.jar
(2) Httpcore-4.3.3.jar
(3) Selenium-java-2.43.0.jar
(4) Selenium-server-standalone-2.42.2.jar
(5) Testng.jar
Please download the above package to spare, note that the version number can be different from the above, try to download the latest package, otherwise it will conflict with the browser version.
2.2 Testing the test environment
After three steps of our preparation, the test environment is set up, but how to build the concrete? We need to check it out.
1. Create a new Java project with a suitable name, such as test. and attach the package that you downloaded above to the project. As shown in 2.2.1:
Figure 2.2.1 Creating a Java Project
2. Create the testng test case in the project. Right-click the src folder of the test project, select "New" à "other ..." and select "TestNG", "Testngclass" in the window that opens, as shown in 2.2.2.
Figure 2.2.2 New TestNG Class
3, create a new testngclassic, enter the test case name in the popup dialog box: HelloWorld, and select Beforetest and Aftertest, and enter Xmlsuitefile after Testng.xml. Clicking the Finish button creates the corresponding Helloworld.java test case file and the testng configuration file testng.xml. As shown in 2.2.3:
Figure 2.2.3 Creating a test case file
4. Write the test case code. Write the following code in the test case file Helloworld.java that we test to create:
Helloworld.java
Package testcases;
Import Org.testng.annotations.Test;
Import Org.testng.annotations.BeforeTest;
Import Org.testng.annotations.AfterTest;
public class HelloWorld {
@Test
Publicvoid F () {
System.out.println ("Hello world!");
}
@BeforeTest
Publicvoid Beforetest () {
System.out.println ("Beforetest running!");
}
@AfterTest
Publicvoid Aftertest () {
System.out.println ("Aftertest running!");
}
}
Detailed Explanation:
In the main test function f (), output our classic string "helloworld!"; Output "beforetestrunning!" in the Beforetest () function and output "aftertestrunning!" in the Aftertest () function. The purpose of our doing this is simply to check that the program output is not what we expected. Learn about the execution flow of testng test cases to facilitate the writing of our subsequent test cases.
5. Run the test case. Right-click the Helloworld.java file, then select Runasàtestngtest, and then view the output status:
The output sequence is the same as we expected, so in the case of specific test cases, we usually put the variable definition or the test case's predecessor action into the Beforetest () function, while the data produced by the test use clears or closes the browser and puts it into the aftertest () function.
So far, a simple testng test case has been written, and we've learned about TestNG's use case execution order, but we don't seem to use webdriver?
6,webdriver+testng test Case detection. We use Webdriver to open Baidu, and then we can according to our needs, do some of the children operation. The specific code is as follows:
Helloworld.java
Package testcases;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
Add a reference to Webdriver
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
Public class HelloWorld {
@Test
Public void F () throws exception{
System.out.println ("Hello world!");
Create a browser and open Baidu
Webdriver Driver = new firefoxdriver ();
Driver.get ("http://www.baidu.com");
Do anything and Baidu what want!
Thread. Sleep (5000);
Driver.quit ();
}
@BeforeTest
Public void beforetest () {
System. out. println ("beforetestrunning!");
}
@AfterTest
Public void aftertest () {
System. out. println ("aftertestrunning!");
}
}
Run Helloworld.java again, you can open Firefox browser, and open the Baidu Web page, wait 5 seconds after the browser close. In fact, after you open the Web page, you can use Webdriver to do any action related to the specific test case. Of course, will also output the corresponding test information, error when the location of the wrong position and so on.
2.3 Summary of this chapter
In this chapter we explain the setup of the TESTNG automated test environment and write two simple test cases to detect if the environment is successful. In the process of setting up the environment may be due to the JDK or eclipse and other issues caused by the error, it is necessary to find a solution on the Internet according to the specific situation. With the environment in this chapter, we have the environment for writing test cases, and we will explain the relevant knowledge points to improve the learning of your automated tests.
Chapter II TESTNG Environment construction