Automated testing of complex WEB applications using a layered selenium framework

Source: Internet
Author: User
Tags xml example testng

Test the tool to accurately reproduce the test cases steps written by the software tester by simulating the various actions of the user on


Development environment Configuration
Take Java as a test language as an example, create a new Java project in Eclipse test Search Engine, download the SELENIUM-RC software package, and selenium server/selenium Java client The driver jar and the JUnit library are added to the project's Java Build Path.

Figure 1. Build Path Configuration

Starting Selenium-server, you can run the executable jar package directly from the command line using the Java-jar command (for the Chinese Windows operating system and the IBM JDK, you need to add the parameter-dibm.stream.nio=true). If you want to start/stop Selenium-server in a Java program, first create a new Remotecontrolconfiguration object, RCC, and specify the remote control parameters (including configuring the listener port for selenium Server, Firefox browser profile, etc.), and then create a new Seleniumserver object that passes the RCC into the Seleniumserver constructor (for the Chinese Windows operating system, using the IBM JDK, the run in Eclipse Add-dibm.stream.nio=true to configurations VM arguments).
Listing 1: Starting/stopping selenium Server using Java
Remotecontrolconfiguration rcc=new remotecontrolconfiguration ();
Rcc.setport (4444);//Specify Selenium server open port
Seleniumserver Selenium_server;
Selenium_server=new Seleniumserver (RCC);
Selenium_server.start ();//Start SERVER
Test code
Selenium_server.stop ();//Stop SERVER
In addition, Selenium server can control start/stop through an ant script, which provides another flexible and powerful way to control your project.
Listing 2. Start/stop Selenium Server with ant script
After the Selenium-server is started, an instance of the Selenium class is created selenium, and interacts with Selenium-server through this instance, as follows.
Listing 3. Start/stop of a selenium instance
Selenium selenium=new Defaultselenium (java.lang.String serverhost,
int ServerPort,
Java.lang.String Browserstartcommand,
Java.lang.String Browserurl);
Selenium.start ();
Simulates various user actions via the Selenium control browser
Selenium.stop ();
Selenium instances contain rich interfaces that can operate on a variety of web elements. For example, in the Google page, enter "DeveloperWorks", click the Search button, on the results page to verify whether the "DeveloperWorks China" words.
Listing 4. A simple example of a selenium test
Selenium selenium=new defaultselenium ("localhost", 4444, "*firefox",
"http://www.google.cn");
Selenium.start ();//Start browser
Selenium.open ("/");//Open Www.google
Selenium.type ("Q", "Developerworks");//Input text box
Selenium.click ("btng");//Click the Search button
Selenium.waitforpagetoload ("30000");//wait for loading results page
Verifytrue (Selenium.istextpresent ("Developerworks China"));//Verify that the specified character exists
Selenium.stop ();//Close browser


When Selenim met TestNG
When testing a Web page with selenium, the various behaviors that are reproduced depend on the tester's input parameters, such as selecting items for a drop-down menu, entering characters in a text box, and so on. Different test cases corresponding to different input, if the method can be easily and effectively passed the test parameters, will greatly improve the test case reusability and maintainability. When selenium encounters testng, these can be achieved. Ng in testng, meaning next Generation, in fact, introduces a number of new features: Flexible test configurations, support for JDK 5 annotations, data-driven testing, robust execution models, and more.

Continue with the Google search scenario above, and use examples to understand the usage and functionality of testng.
Listing 5. TESTNG Application Examples
@Parameters ({"url", "query-string", "Btn-id", "Txt-id", "verify-string"})
@Test
public void Testgoogle (String url,string querystring,string Btnid,
String txtid,string verifystring) {
Selenium=new defaultselenium ("localhost", 4444, "*firefox", url);
Selenium.start ();
Selenium.open ("/");
Selenium.type (txtid,querystring);
Selenium.click (Btnid);
Selenium.waitforpagetoload ("30000");
Verifytrue (Selenium.istextpresent (verifystring));
Selenium.stop ();
}
In the code listing above, note parameters specifies the parameters specified in the TESTNG test framework's configuration file Testng.xml, as follows:
Checklist 6.testng.xml Example
It is not difficult to think that as long as the parameter values in the Testng.xml are modified, the different test cases can be driven by the input parameters. However, there are significant limitations in specifying parameters only in Testng.xml, and it is clear that too many parameters are difficult to maintain and that the input of different test cases cannot be organized neatly. In the following, we will solve this problem.
Back to top of page
A layered testing framework based on selenium
At work, the author tests multiple plug-ins based on the OSGi platform. Each plug-in implementation of unique features, there are multiple test paths need to cover, at the same time, the various plug-ins have a common point, you can extract some parts for reuse. In this case, we will assume that the search for various keywords in google, Baidu, and Bing, and verify the existence of the target string in the returned results page. Each search engine can be thought of as a component to be tested, writing test Cases for them, and organizing it into a test Suite for performing tests. In fact, the testing of 3 search engines, because of homogeneity, can also be combined into a test, using different input parameters to specify the search engine to be tested. This is considered to be three components, just to illustrate how to organize multiple test modules in a selenium+testng environment.
From top to bottom, the test scenario described in the previous paragraph can be decomposed. Test suite contains three types of test Cases (Google, Baidu and Bing), one test case for each type of test Cases is made up of several reusable test tasks, and the test task accomplishes homogeneous behavior by passing in different parameters. Under the test task, define the relevant file that contains the location information for the Web page element that you want to test. As a result, the layered selenium framework has three levels:
Appobjects--web page element positioning information, such as buttons and text boxes;
tasks--the reusable behavior in the test steps;
Test cases--consists of tasks.
Web element Locators Definition and collection
Selenium to locate web elements based on XPath, the knowledge of XPath is not part of this article. In the previous example, it is not a good practice to define text boxes and button locators in the testng configuration file Testng.xml for complex test scenarios. Therefore, we set up files on the appobjects layer, locators the elements of the Web page element, so that it is easy to maintain. The Find feature of Selenium-ide is suitable for this step. The contents of the file googlepages.properties are as follows:
Listing 7.locators File Examples
#define the keys and corresponding xpaht locators of Google page.
googlesearchtxtfield=//input[@name = ' Q ']
googlesearchbtn=//input[@name = ' btng ')
At this time, in the Testng.xml, delete locators related parameters, only need to parse. properties file, generate locators's properties backup. In the attached source code, you can see the simple implementation of the parser proputils for the. properties file.


Test task decomposition and implementation
To illustrate task decomposition, take a simple search process, for example, to enter search keywords, click the Search button, and verify the results page. The actual code looks like this, and it is not difficult to find that the test task, which determines the behavior by the parameters, accepts a PARAMAP data structure and takes appropriate action within the method based on its content. In this way, test cases can drive the test task to implement its desired behavior in a parameter configuration file.
Listing 8. Test Task code example
public void Opensite () {
Selenium.open ("/");
}
public void Typesearchtxtfield (HashMap paramap) {
Utils.waitforelement (String) Elemmap
. Get (Testgoogleconstants.google_search_txt_field), 30);
Selenium.type (String) Paramap
. Get (Testgoogleconstants.google_search_txt_field),
(String) Elemmap
. Get (Testgoogleconstants.google_search_txt_field));


}
public void clicksearchbtn () {
Utils.waitforelement (String)
Elemmap.get (TESTGOOGLECONSTANTS.GOOGLE_SEARCH_BTN), 30);
Selenium.click (String) elemmap.get (TESTGOOGLECONSTANTS.GOOGLE_SEARCH_BTN));
}
public void Verifyresult (HashMap paramap) {
Stc.verifytrue (Selenium.istextpresent (String)
Paramap.get (testgoogleconstants.verify_string));
}
The Clicksearchbtn method does not require parameter input because its task is simply to click on the Search button and invoke it when test cases needs it. This is just a simple example of how to decompose a task, which can be a lot more complex for the actual test case, but the reuse and flexible invocation after that is worth the effort.
Test cases implementation with test tasks call
Test cases is a collection of test steps that can be implemented by invoking several tests tasks. Take the search engine that runs through this article as an example to invoke the test TASKSS defined in the previous section to reproduce the entire testing process. The simple code is shown below.
Listing 9. Test Case code Example
@Parameters ({"Google_se_para_1"})
@Test
public void Testgoogle_1 (String parafile) {
Paramap= (HASHMAP) xmlparser.getinstance ()
. Parserxml (Parafile);
Tgtasks.opensite ();
Tgtasks.typesearchtxtfield (PARAMAP);
TGTASKS.CLICKSEARCHBTN ();
Tgtasks.verifyresult (PARAMAP);
}
Test cases, under the layered Selenium testing framework, calls the existing test Tasks as required. It is worth noting that the Paramap parameter in the above two code list. This hash table is obtained by parsing the parameter definition file by the parser we implement. The testng parameters parameter mechanism allows the test Cases to flexibly specify the parameter file to drive different test Cases.
Listing 10. Example of a parameter definition file
Developerworks
Developerworks China
Establish input parameter parser (parser) and parameter file
The smooth work of test cases and test tasks requires the input of the parameter file and the corresponding parameter parser. We illustrate the format of the parameter file with the example of the above code listing. The label is in the outermost layer, where the child element is a specific parameter value, such as a label that represents a page element with its corresponding input. The id attribute of the element corresponds to the locators defined by the. properties file in Listing 6, and its child elements represent the input values of the locator. The specific implementation of the parser can be found in the attached sample source code.
Back to top of page
Export executable jar packages for deployment to various test servers
In order to be able to export the selenium test script into an executable jar package, we can implement the relevant content defined in Testng.xml in the Java code, as shown below.
The Java representation of the manifest 11.testng.xml
Suite Tag
Xmlsuite suite=new xmlsuite ();
Set Suite Name
Suite.setname ("Test Search Engine");
Set parameter tag
HashMap para=new HashMap ();
Para.put ("Google_se_para_1", "/src/resources/google_se_para_1.xml");
Suite.setparameters (para);
Test tag
XmlTest testgoogle=new XmlTest (suite);
Testgoogle.setname ("LDAP configuration Template");
List classes=new ArrayList ();
Classes.add (New Xmlclass (Testgoogletestcase.class));
Testgoogle.setxmlclasses (classes);
This code listing is functionally identical to the previous testng.xml, adding the code to the main function of the newly defined class Searchenginesuite, starting and shutting down the selenium server in the method of this class, and hitting the relevant file into an executable jar, which can be done via the command line of JAV A–jar command, perform selenium test. Of course, because they are universal, they can be deployed to a variety of test servers to perform tests.
It is recommended to export the Runnalbe jar using Eclipse's plug-in fat jar. First, fill in the name of the output jar and set the Searchenginesuite to Main-class. Then, tick "merge individual-sections of all MANIFEST." MF files ", the latter item is checked as required.

Figure 2. Fat Jar Export Step 1

Go to the next step, select the file you want to export, include the source code and the jar file you used, and click Done.


Figure 3. Fat Jar Export Step 2

After exporting the runnable Jar, run with the command Java–jar Testsearchengine_fat.jar.


Conclusion
It is not difficult to read through this article, selenium provides a new method of Web testing, with the support of layered framework, its function is more powerful, flexible, and has strong reusability. By specifying different parameter files, the example of this article can be used to test the different keywords of Google, the same method can be applied to Baidu and Bing. and run through Testng.xml or its Java code form into a test suite. You can see the detailed implementation in the attached source code. In the actual work of the author, the complex test requirements can also be able to cope.

This article was selected from: http://www.spasvo.com/ceshi/open/kygncsgj/Selenium/2014128164908.html

Automated testing of complex WEB applications using a layered selenium framework

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.