Sahi---implementing WEB automation Testing

Source: Internet
Author: User
Tags xpath ssl certificate

Sahi is a business-based open source WEB application Automation testing tool for Tyto software. Sahi runs as a proxy server and accesses elements in the Web page by injecting JavaScript. Sahi supports HTTPS and is independent of the Web site, simple but powerful. It has some advantages over dynamic ID element lookup and implicit page waiting processing, compared with Selenium and other automated testing tools. Choosing the Sahi tool to implement automated testing of specific WEB projects is a good choice.

Web Test Background

With the development of web technology and Internet, Web application products are more and more abundant, and the demand of Web page testing is increasing. In the current global software is in the pursuit of efficient, agile development model of the background, WEB Automation testing has become a new wave of technology exploration and research craze. Because traditional manual testing is not only inefficient, and testing quality is limited by the testers ' mood and mood. The quality of the tests is worrying when a tester is taking the jitters to measure the amount of repetitive work that is complicated. What's more, when this test involves testing in the context of globalization, the multi-language version of the test has multiplied the test effort, which is undoubtedly a huge test!

Features and benefits of Sahi:

When it comes to Web-oriented automated testing, it is believed that many readers will think of or use tools such as Selenium and Watir, which may be unfamiliar to Sahi. First, let's take a look at the next Sahi tool. It is a mature open source Web automation testing tool developed by Indian company Tyto Software. Sahi is easy to use and supports both Ajax and Web2.0 technologies, and is suitable for both agile and traditional different test modes. So, what are the differences and advantages of other very popular WEB automation test tools? Let's compare it to the main automated test tools Selenium and Watir, see Figure 1:

Figure 1. Comparison of Sahi with other tools

The comparison shows that Selenium supports a richer scripting language and comes with Selenium IDE auto-recording tools, and Watir executes faster than others. Sahi also has a self-contained recorder, and support almost all browsers, and JS support is good, have a page waiting to determine the mechanism, built-in Java exception report, support Ajax and other advantages.

Below, this article will explain in detail several advantages of Sahi.

Context-based page recognition mechanism:

Most Web automation testing tools, such as Selenium, or automated frameworks, employ page element positioning strategies such as DOM-based positioning strategies, Xpath positioning strategies and ID, name, identifier, and so on.

Identifier positioning is the most common positioning method, when it is not recognized as a different positioning mode, the default is Identifier positioning. Under this strategy, the first page element that uses the ID will be identified, and if no element with the specified ID is used, the first name will be identified with the element that matches the specified criteria.

For example, identifier identifies the positioning strategy for the username element: Identifier=username

ID positioning is a more precise positioning in the case where the element's specific ID feature is known. For example, position the page element Loginfrom:id=loginfrom

The name is positioned to identify the first UI element that matches a name attribute. If multiple elements have the same name attribute, you can use the value filter to further refine your targeting strategy. For example, the anchor page element is username:
Name=username

Xpath positioning is the method of locating elements in XML, and HTML can be thought of as an implementation of XML. XPath extends the above ID and name targeting methods, providing both absolute and equivalent paths.

Absolute path: html/body/div[1]/div[1]/div[3]/div[1]/form/span/input[1]

Relative path lookup://div[@id = ' FM ']/form/span/input

However, in the actual case, the page elements are not as clear as expected. The DOM trees of some dynamic pages often change frequently as Web products are updated. Many of the element values, such as IDs, Name, etc., are not required in the code and are often defaulted. Also, property values are often not unique, and elements of the same property are sometimes present on the page. When the default ID value or Xpath location fails, these methods of finding and locating are often helpless and vulnerable.

Sahi employs an active lookup mechanism, which is not constrained by specific element attributes. In the absence of an ID, Name value, it can use some properties, such as "Title,value", which are properties that are visible to the page. At the same time, Sahi will find recognition by passing in these visible and identifiable attribute values to the Sahi preset mechanism. Sahi allows the developer to set different attributes and a specific lookup order for each element, including those custom attribute names. So Sahi is more flexible and open than other WEB automation testing tools.

For example, _link ("ValueName") is used to locate a link defined as "ValueName", where the valueName is not necessarily the property value of value, it can be its ID, title, and so on.

The mechanism of Sahi active lookup is mentioned earlier, so how does it find the specific element under the DOM node? Sahi mainly provides three context-based elements api:_in,_near and _under.

In the literal sense, it is not difficult to understand that _in refers to looking for an element under a DOM node, which is more flexible than Xpath's absolute or relative path lookups, and does not cause the problem of missing elements because of changes in the internal structure of the DOM tree that causes the path to fail.

_near is the nearest element to find the appropriate set rule condition near an element, which provides a good solution for multiple instances of the same property value in a page, making the search more accurate.

_under is the search that starts at the bottom of an element, finds the most recent element that meets the criteria, and the general _under applies to the same column with the same offset. Let's take a look at an example to deepen our understanding of the Sahi-based context-aware lookup mechanism:

Figure 2. Case pages

Assuming that the name= "Q" of all text boxes in the Web page shown in Figure 2, then Sahi's detective device identifies them by some identification, such as (_textbox ("Q"), _textbox ("q[1") and _textbox ("q[2]")).

If we want to locate the text box in the line "Ruby for Rails", that is _textbox ("q[1]"). Traditional element recognition encounters multiple problems with the same attribute element, even if the XPath is positioned in a way that causes the XPath to fail because a new row of data is prepended to it.

At this point Sahi can be located by _near this way:

_textbox ("Q", _near (_cell ("Ruby for Rails"))

When we want to locate the check box, we find that the "Ruby for Rails" line has "recommend" and "already own" two check boxes, for more accurate positioning, we can combine _under, for example: _checkbox (0 , _near (_cell ("Ruby for Rails")), _under (_cell ("recommend")).

If there are multiple such tables throughout the page, we can also use _in to further narrow the scope, such as: _checkbox (0,_near (_cell ("Ruby for Rails"), _under (_cell ("recommend")),
_in (_cell ("cost)").

It is also worth mentioning that the identifier parameter in the Sahi API supports regular expressions, for example, _div (/name.*/) is used to identify all div that begins with a certain pre-attribute value that is name.

Implicit page load response wait mechanism:

More and more Web applications are using Ajax technology to support the asynchronous request response of Web page data. The current generic WEB automation test tool does not have an intelligent processing mechanism to determine when the next operation can continue. Automated test tools such as Selenium typically set a fixed wait time in the script. But this is often proven to be not necessarily accurate. In practical tests, it is difficult for a person to accurately determine a reasonable time value for each operation request. Because the wait time setting is too short, the next action is performed when the application request is not returned, or because the network factor causes the normal response time to grow, it can cause the test process not to find the corresponding page element, which causes the whole test case to fail. And if the time is set too long, it will cause in some normal response process of unnecessary waiting time wasted, reducing the test efficiency.

Of course, some testers will include some custom code in the automated test script. Determines whether the request response is returned by polling for a specified element on the interface, and then decides to continue the next operation or timeout. However, such a lookup process can cause the entire scripting code to become bloated, increasing the cost of development. Moreover, it is not an easy task to find the specified element in a dynamic page.

The Sahi has an intelligent page-waiting mechanism that automatically determines if an AJAX request has been processed and then proceeds to the next step. Also, this is "implicit" for the user and does not require additional code to be added.

Back to top of page

How the Sahi works:

To put it simply, there are three steps to automating testing with Sahi, recording, refining scripts and playback, such as:

Figure 3. Three main processes of Sahi work

such as Sahi is the first with its own recording tool, the approximate operation of the process recorded, and the Sahi code to record the entire operation process. Subsequently, the automatically generated code is further refined and developed, invoking some external APIs or writing specific code to implement specific operations. Finally, the Sahi will automatically define the test action for the WEB application by using Sahi to put the final script back and forth.

Below, the three procedures are described in detail in this article.

First step: Record Figure 4. How the recording process works

Sahi is done by running as a proxy server and by setting the browser proxy for the Sahi server. This allows the Sahi script to be injected into JavaScript through the request requests to access elements in the Web page. , it is clear that Sahi is an intermediate agent between a Web browser and a Web server.

Step two: Refine the script in Figure 5. How the Refine Script process works

The recorded script is the specified element and the only operation, then you need to refactor the code, extract the core of the function block, the elements of which are parameterized to achieve reuse. Such data can be read from an external DB or file. At the same time, you can invoke APIs such as the Sahi API or external Java to implement certain functions.

Step three: Replay Figure 6. How the Play back process works

Sahi runs refined scripts to automate test operations and generate test reports.

Back to top of page

Sahi deployment and configuration of the installation

Although Sahi is a Tyto company's product, its download is placed on the world's largest open source software development website SourceForge, which can be downloaded by clicking here.

Figure 7. Sahi Download

The default recommendation is to download Install_sahi_xxx.jar, an executable file that contains the Sahi installer and Sahi tools and their source code. Of course, you can also click on the Red box "Browse all Files" to select the historical version and some free-install compressed files. For example, select the sahi_xxx.zip file that contains only the Sahi tool, or the free-install compressed package file Sahi-src_xxx.zipthat contains the Sahi and source code.

It is recommended to select the recommended Sahi installation package file, which eliminates some setup actions and optionally installs the source code. Double-click the jar file to install it.

Figure 8. Sahi Installation

The installation process is simple, and when the installation is complete, double-click the desktop icon to open the Sahi program. Open the program will first appear a Sahi Dashboard, it can automatically turn on the Sahi Proxy service to start the browser, without the need for cumbersome proxy server Setup operation. You can, of course, manually modify these proxy settings if necessary.

Figure 9. Sahi Dashboard Interface

Sahi will automatically go to detective your system installed some of the browser, and on the Sahi Dashboard display, if you find that some other browsers are not accurate detective out, you can also click on the "Configure" below to configure the add in.

Next, launch the browser by clicking the Browser icon button on the Sahi Dashboard.

Figure 10. Sahi launch Firefox browser

You can start your test by entering the Web page URL of the start test. If the target URL for the test is the HTTPS protocol, you can also click on "SSL Manager" to view and manage the SSL certificate.

Figure 11. Sahi SSL Management Interface

Holding down the ALT key and double-clicking the page will pop up the Sahi control window, 12:

This window is equivalent to the Sahi console, where we can record and replay Sahi scripts, and edit and manage script information.

Figure 12. Sahi Controller Recording

In the record view interface, enter a script name, click "Record", and the Sahi recorder will begin to work. Move your mouse over the target page on your browser and all of your operations will be recorded. You can also customize to add a assertion. Holding down the CTRL key and moving the mouse to any HTML element of the target page, the Accessor will automatically appear in the Sahi controller. At this point, you can customize the operation of the element. Common actions are "click", "Highlight", "Assign value" and so on. You can also add to the script code by using the "Append to Script" button. Press Stop to end the entire process when recording is complete.

Figure 13. Sahi automatic generation of script refinement

Figure 13 is a simple Sahi automatic recording process to get the Sahi script code. Its approximate process is: Through the Baidu search "Sahi" keyword, check the Sahi official website assert whether there is, click to enter the Sahi official website after continuing to verify the Assert "Community Forums", click Enter. With the previous section "Sahi Controller Recording" To complete this procedure, you can find a script file named "Test_sahi" in the default directory "C:\Users\IBM_ADMIN\sahi\userdata\scripts" , we can make this code a refined and rich process, such as highlighting it before clicking on the "Community Forums" link:

_popup ("Sahi Web Test Automation Tool") _highlight (_link ("Community Forums"));

Or you want to call the built-in Java class in the Sahi script code, for example:

Functionprintthroughjava (s) {java.lang.System.out.println ("Through java:" +s);} Printthroughjava ("Hi there");

The "Through Java:hi there" will be output in the Sahi command line.

Figure 14. Sahi Controller Playback

When playing back, simply switch to the "Playback" tab on the Sahi console and find the path where the script is stored, with the start, pause, and end buttons to operate. It is important to note that you must set a Stat URL before you start or you will not be able to play back the script. When the script is played back, you can see the log of the script running in "statements", such as the operation steps and some error messages.

You can view detailed Sahi run log reports by clicking "View Logs" in the lower right corner:

Figure 15. Sahi Log Report

The diagram is visible so that the script code generated by the automatic recording is Sahi code, and we can invoke the Sahi code in the actual Java project for reuse. In fact, we can implement the automatic recording script in Java by opening the Sahi/config/sahi.properties file where the property is set to Controller.mode=java. It is important to note that the Sahi controller after the Java language recording is different from the original, its interface is more concise, the function is more simple, without the automatic playback function. Because, this is more to automatically generate some simple scripts, to improve the developer's development efficiency.

Back to top of page

Syntax and example application of Sahi

The Sahi script is JavaScript-based, and the Sahi script is parsed by proxy and can be executed effectively in the Rhino JavaScript engine. In addition to the mandatory $ before the variable, it is basically like JavaScript.

The Code declaration for the Sahi operation is a regular line of code that ends with a semicolon, such as:

_click (_link ("Login"));

Variable declaration :

Var$variablename =value;

Or declare the value first:

var$variablename;//Declaration$variablename =value;//Assignment

All variables start with the $ sign, and the keyword VAR is used for local variables, such as:

Var$username = "Sahitestuser"; var$password;//declaration; $password = $username + "_password";/"Sahitestuser_ Password

function declaration :

function Declarationfunctionlogin ($USR, $pwd) {_click (_link ("Login")) _setvalue (_textbox ("username"), $usr); _ SetValue (_password ("password"), $pwd); _click (_submit ("Login"));} function Calllogin ("Sahi_user", "secret");

In a Sahi code file, you can use _include to include calls to other Sahi files, such as:

_include ("Includes/common_functions.sah");

From the above syntax, Sahi is the beginning of the underscore, with the action or HTML elements, very simple, clear and easy to understand, these APIs are basically able to see the function of the literal understanding of it.

Sahi's API can be divided into 3 categories: browser Access API, browser action API , and hybrid API.

Browser Access API: Used to access the elements on the browser, and through the agent to inject these APIs into the browser.

Browser Action API: primarily to perform actions such as clicking, entering data, and declaring these elements on the browser.

Hybrid API: It can be used both in the browser and on the agent for handling exceptions and manipulating files and databases.

Since Sahi has a lot of APIs for accessing Web pages, you can learn by referring to the official API documentation.

From the above, Sahi script is a very straightforward declaration and operation process, on a running agent, by the Sahi Dashboard to manage execution. If we want to be able to integrate our Sahi automated process with our other project function modules, then translating these scripts into Java code and running them independently is one of the better things to do.

Next, this article introduces a simple example of using Sahi Java Driver to write Sahi Automation scripts for Web Automation testing.

First, add the Sahi SDK file Sahi.jar to the Java project, which is located under the Sahi installation path: \sahi\lib\, you can then refer to the Sahi Java API to develop automated test scripts. The following is a simple case script:

Listing 1. Import Sahi Class Package
Import Net.sf.sahi.client.browser;import net.sf.sahi.config.Configuration;

Set Sahi installation path and UserData path

Listing 2. Configuring the Sahi Agent
String sahibase = "c:/users/admin/sahi/"; String userdatadirectory = "C:/users/admin/sahi/userdata"; Configuration.initjava (Sahibase, userdatadirectory);

You can set any browser type, or you can create your own definition in the Sahi/userdata/config/browser_types.xml file

Listing 3. Configure the browser and start
String browsertype = "Firefox"; Browser Browser = new Browser (browsertype); Browser.open ();
Listing 4. Action code example
Browser.navigateto ("http://sahi.co.in/demo/training/"); Browser.textbox ("User"). SetValue ("test"); Browser.password ("password"). SetValue ("secret"); Browser.submit ("Login"). Click (); Browser.textbox ("Q"). SetValue ( "2"), Browser.textbox ("q[1]"). SetValue ("9"), Browser.textbox ("q[2]"). SetValue ("4"); Browser.button ("Add"). Click () ; System.out.println (":: Browser.textbox (\" Total\ "). Value () =" + Browser.
TextBox ("Total"). Value ()); Browser.close ();//Close the browser

It is important to note that in addition to the Firefox browser, other browsers must configure the server proxy and open the Sahi Proxy service before running the script.

The actual test results show that the Sahi code introduction, running light and fast, and it can be integrated with some external modules to send mail, read PDF files and other operations, the function is very perfect.

Back to top of page

Conclusion

To sum up, this article from the current predicament of web automation testing, and other open-source web automation testing to compare, introduced the characteristics and advantages of Sahi. Mainly from the context-based page recognition mechanism and intelligent page load response waiting mechanism two aspects are elaborated. At the same time, it introduces the working principle of Sahi and the process of how to install the deployment for automated test development. In short, Sahi provides a set of open source automation testing solutions in multi-browser and multi-programming languages, which solves the problem that some of the current page elements are hard to find and the page response is out of sync, causing the test to fail. Sahi is a fast-deployable, easy-to-develop, and powerful open source Web automation testing tool. Due to space limitations, this article can not Sahi all the functions of one by one elaboration, I hope that interested friends to study and explore.

Sahi---implementing WEB automation Testing

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.