Design and implementation of selenium combined with TESTNG automation framework __selenium

Source: Internet
Author: User
Tags new set prepare soapui testng selenium grid
design and implementation of automatic test based on testng and selenium

1. Introduction

Software testing is an important link of software development and maintenance cost. Any software product must undergo rigorous testing before it is officially released. With the rapid development of computer technology, the structure of software is becoming more and more complex and competition is more and more fierce. In order to ensure the high reliability and competitiveness of software products, many software development organizations devote their major research and development power to software testing.

Regression testing is an important part of software testing, which occupies a large proportion. A regression verification of the existing functionality of the software is required before each routine package is released, to ensure that the error can be sent to all parts of the scene, we all know that the telecommunications industry is a rapid development of industries, the demand for faster, shorter iterations, resulting in regression testing is very frequent, this time if the single hand to test, will consume a lot of time and energy, Testers, too, have been overwhelmed by the decline in patience. In order to avoid this situation, the automatic testing of the original function is particularly important.

2. Introduction to Tools

When it comes to open source automation testing tools, online talk about the wind and cloud, but other people said things are not necessarily suitable for you, we are in the combination of automated testing tools, according to their actual situation selected Selenium + TestNG + dbunit combination, I first introduced these tools.

Selenium, it is based on the principle of recording the application generated by the linear script playback to achieve the purpose of automated testing. The advantage is that it's easy to get the script you need by recording. There are a lot of tools like recording/playback testing, and I chose it because it is open source, and it tests directly in the browser, just as the real user does. Selenium tests can be run on Windows, Internet Explorer, Mozilla, and Firefox on Linux. Other test tools do not cover so many platforms, and more importantly, selenium supports multiple languages, JAVA, Ruby, Python, etc. (Figure 1)

Figure 1

Supporting so many languages means that our test scripts developed through selenium can be ported to multiple platforms at will, inheriting the selenium API to extend some of our own test classes, and can even develop a set of our own automated test platforms, and other tools, Including the famous qtp,silktest is somewhat inferior, although these charge software has the strong technical support, the study document and the reference material is also quite rich, but these tools the limitation is too big, once use these tools, you will become more and more depend on others ' things, thus cannot precipitate out own technology, This is the root cause of my choice of selenium.

Selenium Series A total of 4 tools

Selenium Core: A test case that supports DHTML (the effect is similar to a data-driven test), which is the engine for the Selenium IDE and Selenium RC.

Selenium Ide:firefox is a plug-in that supports script recording.

Selenium Rc:selenium Remote Control.

Selenium Grid: Allows multiple test tasks to be run concurrently and in different environments, dramatically speeding up the functional testing of Web applications.

The idealized use of selenium to write test case is to perfect the test script by selenium the IDE record script, through Firebug to help locate the page element, and then through the selenium RC. The Selenium IDE is a Firefox plug-in that can be scripted and case converted, so Selenium Ide+firebug will be the two biggest helpers for your future test case, but unfortunately both of these tools rely on the Firefox browser, If your program does not support Firefox browser, you can only hand-coded to complete the automated test script, for beginners, without the aid of these two tools, in writing test script is still more difficult, but familiar with the Selenium API and master the method of page element lookup, These two tools seem less important.

TestNG, a typical unit test tool, probably most developers are only familiar with JUnit, and JUnit is the current one-stop solution for the Java language Unit test. This framework is commendable because it introduces test-driven development ideas to Java developers and teaches them how to effectively write unit tests. However, JUnit has not improved much in the past few years, so writing tests for today's complex environments has become an increasingly difficult task: JUnit must integrate with some other complementary test frameworks. And TestNG is a new framework for testing Java applications. I chose testng because it is an annotation based test framework that overcomes some of the drawbacks of JUnit by adding features such as flexible devices, test classifications, parameter tests, and dependency methods. In addition, testng runs on top of Java 5.0 (via annotations) and Java 1.4 (through Javadoc-style annotations). Let me enumerate the advantages of NG:

Keywords: parameterized. TestNG The concept of data-driven automated testing to the fullest, I give an example, a test method based on different parameters combined 20 cases, according to the idea of data-driven testing, we only need to write a test method, and then prepare 20 kinds of parameter combinations of data. If you use junit you may need to write 20 test methods, and testng can be parameterized by annotations. 20 different combinations of parameters are organized into an Excel file to manage, the file can increase the use case description even test scenarios and other related information, if you add new cases, only need to add one row of test data, as shown:

The test method only needs to write one, as shown in the figure:

Keywords: failed and rerun, this ability to rerun failed tests is particularly handy in large test suites. This is a unique feature of testng. In JUnit 4, if the test suite includes 1000 tests, 3 fail, it is likely to force you to rerun the entire test suite (after the error has been modified). Needless to say, this kind of work can take several hours. Once a failure occurs in testng, it creates an XML configuration file (Figure 4) that describes the failed test. If you use this file to execute testng run the program, TESTNG will only run failed tests. So, 1000 tests have 3 Failed, and this scenario requires you to rerun three failed test scripts, not the entire test suite.

Here only listed the testng more obvious advantages, in addition to the above advantages, there are many, here is not elaborated.

Figure 4

DBUnit, which simplifies the work of using databases by effectively managing the data in test scenarios. Its design idea is before testing, back up the database, and then to the object database to embed the data we need to prepare, and finally, after testing, read into the backup database, back to the test before the state, through the Dbunit can also assist the data persistence layer of the test work, If you verify that an entity is persisted through the test program (DAO), verify that the data entity is written to the database as expected, and provide the ability to convert the data from the database to the XML file store.

The figure above is a data exported from the Customer table, and if this data is needed during testing, it can be initialized to the database by Dbunit.

In another case, if a new customer is added, the data in this file can be used to make assertions that the Dbunit can convert the XML to a dataset or even JavaBean, and you can compare it directly to the dataset rather than each field.

However, Dbunit also has its own flaws, as shown in the figure above, when interacting with the database through the Dbunit needs to check the table's primary key, if a table does not set a primary key will not be able to use the Dbunit API, can only be implemented in other ways.

FitNesse, a tool representative for business-driven testing, fit is a common open framework that converts test methods written by testers into tabular form to customers, often used for automated acceptance tests, which record test case input, expected output in tabular form on the page, automate running, and display test execution results. But to increase the amount of work a developer has to do, to communicate with your software, you need to write fit fixture to enable the transformation of business and program logic.



3. Automated Test Implementation process

Through the combination of the above several tools, flexible use, you can build a set of suitable for their own automated test platform.

Let me introduce you to the use of these tools under different test scenarios:

1) interface test

(Testng+dbunit) interface Automation testing can be done through unit testing, using TestNG to write unit test code for each interface, initialize the database by DBUnit, and define one or more tests as <suite> tags. Bulk execute test code and generate test report.

2) WebService Interface Automation test

Most Internet companies now adopt an SOA architecture, so testing for WebService interface types is more important. Typically, test engineers may use tools such as SOAPUI to test Web service, undeniably soapui have very good results in a single WebService interface test, but in the interface combination test, And when the test results need to be verified by the database is not so automated, always require manual intervention, which in part leads to low test efficiency, so we are here to explain how to use FitNesse this open source products to implement Interface test automation (not finished time to add)

3 Automation of Web application Systems (Selenium + TestNG + DBUnit)

TestNG is particularly suitable for use with selenium to implement tests that other test frameworks cannot implement, such as using dependencies for testing, rerun failed tests, and parameterized tests using parameters defined in separate files. All of these features combine to make it stand out among the many Web application test frameworks.

In test automation, the test code contains not only the test logic, but also many other code, such as URL stitching, Html/xml parsing, accessing UI controls, and so on. If you mix the test logic with these unrelated code, the test logic will be difficult to understand and not easy to maintain. The use of a layered structure can solve this problem. In the layered test framework, the three-tier structure is:

(1) Data layer, including UI data and test data, UI data refers to the data to be entered in the page, such as ordinary customers new clothes, you need to enter three information on the page, including products and resources ordered. The data injected through the page is collectively known as UI data and needs to be sorted into Excel before the test is performed, as shown in the figure:


Easy to implement, using the TestNG annotation feature, you can use the data as input to the page. The test data refers to the underlying data in the database, these data are used to support the operation of the entire system, such as operator and organization Authority, new set of ordered packages, package and product relationship data, etc., without such data, the system will not be able to operate normally, so the test script to execute before the basic data initialization There is also a situation where the new data that is generated when the test script is executed will affect the two run of the test script. This means that before you run any tests, you want the database to have a clean set of data, and use the DBUnit Clean_insert command to ensure that the rows created in the previously run tests are deleted. So I can rerun the test, which keeps creating data and does not have to consider database constraints. The test data is sorted ahead of time to the XML file, or it can be exported directly from the database to an XML file, as shown in the figure:

Before executing the test scripts, the data in these files is initialized to the database in advance by Dbunit, so the database is what the program expects.

(2) test case layer, including business logic and control logic. The driver Webdriver (Selenium2.0) is responsible for loading the UI data and entering the UI data into the page when the page is playback. After the front page playback completes, the data enters the persistence phase, then needs to compare to the background business processing logic, for instance the customer data is written correctly to the database, the product order is correct. Because of the different input, the scene is different, the business logic is different, so this script writing is the most important part of the whole process. To differentiate between these branches, you need to split the entire new process into multiple modules for management, and use testng to group test cases to form multiple test suite controls.

(3) The system layer to be tested is completely separated from the test end, and the system is only required to provide URL through the HTTP protocol can be executed by the test script call.

4, the continuous integration of automated testing

Continuous integration is a software development practice, which provides a theoretical basis for improving the efficiency of software development and guaranteeing the quality of software development, and continuous integration ensures that the code submitted by team members at each point in time can be successfully integrated. In other words, any point in time can be the first time to discover the software integration problems, but also quickly put the test script into production use.

With SVN unified code management, adding a test component configuration in an ant or Maven script, you can use Hudson to start the test script running at the end of routine compilation. Returns the test report to the tester after the run is completed. The entire iteration cycle is shown in the following illustration:

5. Conclusion

Software automation tests make up for the defects of manual testing, and can detect defects as early as possible in the process of software development, so it is necessary to implement automation test. In this paper, several automated test tools are introduced, which can be combined into their own automated test framework, which not only makes the automation test play its unique role in product testing, but also saves the resource cost, including the cost of testing tools purchase and human resource cost.

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.