automated testing based on SELENIUM2 and testng
0 Reviews2013-09-06 11:10 it168 website Original author: Huang Fu Peng Chenyu editor: Shen An
The "IT168 technology" selenium is a popular web-oriented automated testing tool, which has many people's pro-gaze for its high efficiency and wide coverage of browsers. TESTNG is a test framework derived from JUnit and nunit that, in addition to inheriting the advantages of both, has added some new features to make it more powerful and easy to use. This article will focus on how SELENIUM2 combines testng to do automated testing, and also introduces Reportng, an extension of testng report, which is more aesthetically and easily readable than TestNG's own report.
Advantages of using Selenium for Web automation testing
Compared to QTP,RFT expensive cost, as the open Source tool selenium webdriver naturally needless to say. And Selenium is a browser-based testing tool, so it can run more quickly in response to UI requests, save time and improve execution efficiency. The integration with most test platforms, as well as the variety of Extensible scripting languages (Java, DotNET, Perl, Python, Ruby, C #, etc.) also has a great advantage over other tools, and finally, Selenium supports multi-browser operations (Ie,firefox,safari) , which is not available in other testing tools, of course, there is no universal testing tools, after a comprehensive assessment of the system and testing requirements, the right is the best, and in the automated testing process, often can not only use a single automation tool, Combining the advantages of different automation tools to achieve our goal is the best practice.
The evolution of Selenium
Selenium has evolved from the previous 1.0 (RC) to the present Selenium2 (Selenium1+webdriver).
Before running the Selenium1.0 program, we must start the selenium server side, which is selenium Remote control, we call RC,RC mainly includes three parts, Launcher,http proxy, Selenium core, where selenium core is made up of a bunch of JavaScript functions, calls these functions to implement various actions on the browser. Now that you can implement your browser, why do you need Selenium2 (Selenium1+webdriver)? SELENIUM1 mainly has the following disadvantages 1. No native mouse and keyboard events, 2.xss/http homologous data problem, 3.popup dialog problem. Webdriver different browser processing and Selenium1.0 have a clear difference, Selenium1.0 no matter what browser, is handled by JavaScript, and Webdriver is the most easily recognized browser language to handle, For example, in Firefox, JavaScript is the easiest, in IE, C + + is the easiest to identify, through the flexible selection of the most easily recognized language to handle multi-browser, we can be very good to avoid some browser security restrictions on JavaScript, webdriver Not only can you handle this problem, but you can invoke the operating system API, especially if the user needs to simulate mouse or keyboard operations. By contrast, it seems necessary to evolve from Selenium1.0 to Selenium2.0.
Automate testing with selenium Web driver
In order to enrich the whole article, the author takes a simple scenario as an example to tell the reader how to use the selenium webdriver+testng+reportng to realize the Web Automation in one step. Note: The scenario will run through the entire article.
Sample Scenario:
Brief description: Ask a question on jazz.net
Step: 1. Open jazz.net:https://jazz.net/
2. Click Log In connection
3. Log in with a user name
4. Enter your password
5. Click Log In button
6. Verify that the profile image appears
7. Click Forum Connection
8. Click the Ask a Question button
9. Fill in all required fields on the question page and submit the questions
10. Verify that the issue is successfully submitted
One. Log out
1. In order to quickly generate an automated script, we can use the Selenium IDE to record the original script and then refine the script as an easy-to-maintain version (if you are familiar with Selenium's Web driver APIs, of course, And have a good test framework can be adopted, you can also write scripts directly. Selenium IDE is a Firefox add-on, easy to install and use, this is not introduced here, only with one through the Selenium IDE recorded above the sample scenario, Figure 1:
▲ Figure 1 Selenium IDE
Once the recording is complete, it is exported to the Java/junit4/webdriver format (Figure 2) and named Postaquestioninjazzdotnet.
▲ Figure 2 The format of the case that can be exported
2. Create a Java project in the Eclipse IDE and create the following folder structure, Figure 3
▲ Figure 3 The initial folder structure
Put the newly-exported Postaquestioninjazzdotnet.java into the cases folder, the Data folder is used to store the information needed in the script, the map folder to store the object used in the script's properties, the Lib folder is used to place the required dependent jar package. Currently, the selenium Web driver-related jar package needs to be placed in Lib and added to the build path.
Open Postaquestioninjazzdotnet.java to fix compilation errors, such as modifying the package name to cases,4:
Figure 4 The JUnit-formatted Java script exported via the Selenium IDE
The script after fixing the compilation error should be ready to run, but we need to refactor the script for the following reasons:
A. The recorded script is generally readable.
B. Data/object attributes are mixed with test logic, which is inconvenient for maintenance.
C. The exported script is based on junit and we need to modify it to testng.
3. Refactoring Scripts
3.1 Separates the test data from the test logic, which is the parameterized data.
We use CSV as the format for storing data, as shown in 5: (For display styles opened with Excel)
Figure 5 Storing data in CSV format
The first behavior each data item specifies a logical name, separated by commas, from the second line, in the corresponding data column to fill in the script to use the data values, to support the storage of multiple rows of data, for later implementation of data-driven.
Creates a Datahelper class that is used to fetch data from a data file.
The main methods that are included are:
Initfullappdata for reading the data into memory, the code snippet is as follows:
▲ Figure 6 Initfullappdata code Snippet
Getappdata, which is used to read the value of a row based on the logical name of the data item, the code snippet is as follows:
▲ Figure 7 Getappdata code Snippet
This can be used in scripts:
Called in the Setup method: Datahelper.initfullappdata (This, ProjectPath, cassname);
Then in other test methods can be directly through the Datahelper.getappdata ("Postaquestioninjazzdotnet_data:jazzurl", 1); Get the data value, where the first parameter is the full name of the data item, The second parameter is the first value of the data item (this is to consider doing iteration, which is set by the data-driven test)
3.2 Separating the property values of elements on the web from the test logic
Here you need to know how selenium locates Web elements.
By.id
By.cssselector
By.linktext
By.name
By.xpath
By.partiallinktext
Through the above locator can be seen selenium support the use of a variety of property positioning, then we need to store the object's properties in a separate file, easy to maintain and reuse, so we use the following XML format to store the element's property values, such as:
▲ Figure 8 Storing object properties in XML format
Where, for example, Loginlink is the logical name of an object, it can be defined by the tester itself; PropertyName to specify the way to locate the element, the optional values are: ID, name, link, Dom, XPath, CSS, Partiallink; Value is used to store property values.
Create the Autoobjectfactory class to manage the object properties file, with the following code snippet:
▲ Figure 9 Autoobjectfactory code Snippet
This can be used in scripts:
Defined in case: private autoobjectfactory factory = Autoobjectfactory.createinstance (
"Postaquestioninjazzdotnet_map.xml", ProjectPath
+ file.separator + "map");
Then in the test method pass: webelement element = factory.getwebelement ("Postaquestioninjazzdotnet_map:username", Driver, DefaultTimeout), obtain the object, and then invoke the appropriate method to manipulate it, for example:
Element.sendkeys (Datahelper.getappdata ("Postaquestioninjazzdotnet_data:username", 1));
After two-step separation, the script structure is clearer, as shown in:
Figure 102 The code snippet of the script after the step separation
It seems that the separation between the test data and the test logic, the separation of the object properties from the test logic, some additional efforts to be done, but in the long run there are many benefits to the reuse and maintenance of scripts, such as: objects and data can be common among multiple scripts, and if objects or data change, they need to be modified in their separate files. , the modified content will naturally take effect in multiple scripts that reference it.
Combine testng to keep automated testing processes in control
Although the current scripting structure is clear, data and test logic are separated and easy to maintain, there is a need for further improvement from the point of view of test scheduling and reusability.
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, both for unit testing and for integration testing.
The process of writing a test typically has three typical steps:
* Write the business logic of the test and insert the testng in the code annotation
* Add test information to the Testng.xml file or Build.xml
* Run testng
In the above paragraph, our sample scenario is a relatively long test case, if this is used for manual testing, it should be a good test case, but in automated testing, we expect to be able to modularize our script as much as possible for later reuse. After modularity, we can use TestNG's annotation to specify when the module will run, and the order in which it runs, and can be reused in multiple test cases.
The re-organized sample scenario is as follows:
1. Open jazz.net:https://jazz.net/
2. Open the login page
3. Enter valid login data
4. Go to the question page
5. Fill in the content of the question
6. Submit a question
7. Verify that the submission is successful
8.Logout
Before refactoring the script and adding testng annotation to the test method, you need to remove the junit annotation and then introduce the TESTNG related jar package: Testng-6.*.jar, add testng annotation. After refactoring, the script fragment is as follows:
▲ Figure 11 Adding the testng annotation
With the advantage of testng, we can use @beforesuite/@AfterSuite, @BeforeGroups/@AfterGroups, @BeforeTest/@AfterTest and other marking methods for initializing/finishing work. , you can also use some properties to control whether it executes regardless of the circumstances, define its dependent methods, and so on.
For example:
▲ the usage of Figure @BeforeSuite
▲ Figure @AfterSuite and the use of attribute Alwaysrun
To define the order in which tests are executed, you need to create an XML file that defines the order of execution. Refer to the TESTNG official site for the meanings of the individual nodes and attributes in the specific XML.
Figure 14 XML file to define the order of execution
The main method then reads the XML file through testng to drive the execution of the test, with the following code snippet:
Figure 15 Calling TestNG via program
After running the test case, the resulting testng report is as follows:
▲ Figure testng Self-brought report
We are fully aware of TestNG's various annotation and how to control its execution behavior, can be the modular test method of scheduling better, but also can be very good reuse.
Generate beautiful and easy-to-read test reports using reportng
From the last section, we can see that testng has its default report, although it is more comprehensive, but not easy to read, so we want to use reportng to replace the testng default report.
REPORTNG provides a simple way to view test results, color the results, and modify the template to customize the content, modify the CSS to replace the default output style, and so on.
In order to use reportng, we first introduce Reportng-1.1.4.jar and Velocity-dep-1.4.jar, or directly import the source code, to be customized.
Illustrate some of the customizations we have made:
1. The default reportng report, which is the alphabetical order of the executed methods, is not expected, we expect to be sorted by the execution sequence of the methods, so we have modified the Testresultcomparator class, such as:
▲ Figure Testresultcomparator Code snippet
2. You want the information shown in the report to be more detailed and, therefore, customized template files/localization files, etc.:
▲ Figure 18 Localization properties File
▲ Figure 19 Report output template file
3. The associated method added in Reportngutils, as shown in:
▲ related code snippet in Figure 20
4. Modify the code in the main method so that TestNG uses the customized report as the output.
▲ Figure 21 Modifying the Main method
Run the test case again and the resulting test report is as follows:
Overview
▲ Overall preview of the report in Figure 22
Details
▲ Detailed test results in Figure 23 Report
Can be viewed in larger/fullscreen, such as:
▲ the effect of screenshot in Figure 24 report
Conclusion
A set of mature automation framework is needed in the project practice of continuous optimization, only continuous practice to identify problems, solve problems, accumulate experience, and gradually improve. Hope that the author of this practice can give readers some reference, but this practice is far from the perfect point, we are willing to work with you to think more, more exchanges, combined with their own project characteristics of flexible use, reasonable improvement has been achieved can really use tools to liberate daily tedious work, improve efficiency, ensure the quality of the effect.
About the author
Huang Fu-peng: IBM CDL Senior Software engineer, QA lead, engaged in the design and development of automated testing tools, as well as the development of innovative tools. Published numerous articles in Developworks and other journals.
Chen Yu: IBM GDC software Test engineer, mainly engaged in functional testing, automated testing
Yunuo Rong: IBM GDC Advanced software Testing engineer, mainly engaged in web automation testing.
Automated testing based on SELENIUM2 and testng