Selenium Introduction (c)
--A simple application based on RC
Author: torrent
Read recommended before reading:
Selenium Introduction (i)--General introduction
Introduction to Selenium (II.)--a simple application based on Core/ide
In the "Selenium full interpretation" of this article, we briefly introduced the principle of selenium RC, below, let's review:
First, Selenium RC principle and introduction
Selenium RC mode, is the client using a variety of programming languages, through the network to Selenium server to send instructions, Selenium server received the test instructions, launch the browser and send a JavaScript call to the implementation of the HTML page of the full tracking, and returns the execution result to the caller through the network.
The principle of Selenium server is that when it opens the browser, it embeds its JavaScript file into the Web page. The Selenium Web page then embeds the target page through a frame. This allows you to use selenium JavaScript objects to control the target Web page.
The selenium client typically uses unit test techniques to determine whether the program is running correctly by determining whether the returned results are consistent with expectations.
The following figure:
Ii. Preparatory work
1. Language choice
Selenium supports the following languages:
Language |
Selenium Remote Control |
C# |
Library ("Driver") support |
Java |
Library ("Driver") support |
Perl |
Library ("Driver") support |
Php |
Library ("Driver") support |
Python |
Library ("Driver") support |
Ruby |
Library ("Driver") support |
Others |
Commands via HTTP requests** |
You can write selenium test scripts using any of the above languages you are familiar with. This paper is based on the most widely used Java language to introduce the selenium RC
2, software preparation
Selenium Remote Control Server
Download Address: http://seleniumhq.org/download/
Eclipse
Download Address: http://www.eclipse.org/downloads/
3, start selenium Remote control Server
First in the download of the RC package, after decompression to find Selenium-server.jar, and then start it in the Command Line window, the specific startup format is as follows (in brackets for optional parameters):
Java-jar Selenium-server.jar [-interactive] [options]
-port <nnnn>: Selenium port number used by the server (default 4444)
-timeout <nnnn>: The number of seconds to wait before we give up (timeout)
-interactive: Enter interactive mode. Reference tutorials For more information
-multiwindow: Enter the model that the tested site is open in a separate window and selenium support frame
-forcedbrowsermode <BROWSER>: Set Browser mode (for example, all sessions use "*iexplore", regardless of what parameters are passed to getnewbrowsersession)
-userextensions <file>: Specifies a JavaScript file to be loaded into selenium
-browsersessionreuse: Stop reinitialization and replacing browsers between tests.
-alwaysproxy: By default, we make as few proxies as possible, and setting this flag will force all browser traffic through the proxy
-firefoxprofiletemplate <dir>: In general, we generate a clean Firefox setting before each boot. You can specify a directory to let us copy your settings to replace the one we generated.
-debug: Enter debug mode, there will be more tracking debugging information
-htmlsuite <browser> <startURL> <suiteFile> <resultfile>: Use the specified browser (for example, "*firefox") at the specified URL (for example, " Http://www.google.com "), run a separate HTML Selenese (Selenium Core) test suite and exit immediately. You need to specify the absolute path to the HTML test suite and the path to the HTML test results file that we will generate.
-proxyinjectionmode: Enter the proxy injection mode, in which the selenium server acts as a proxy server for all content entering the test program. In this mode, you can access across multiple domains and also support the following additional parameters:
-dontinjectregex <regex>: An attached regular expression that can be used by the proxy injection mode to determine whether to inject
-userjsinjection <file>: Specify a JavaScript file and inject it into all pages
-usercontenttransformation <regex> <replacement>: A regular expression that matches all the measured HTML content, and the second string replaces all matches. This flag can be used multiple times. A simple example for using this parameter: If you add "-usercontenttransformation HTTPS http", then all "https" strings in the HTML of the test application will be replaced with "http".
In addition, two Java System properties are supported:-dhttp.proxyhost and-dhttp.proxyport. Using the Selenium server as a proxy server, Selenium RC generally overloads your proxy server configuration. Use this parameter to use your own proxy server while using the Selenium server proxy. This configuration is used when using a proxy server:
Java-dhttp.proxyhost=myproxy.com-dhttp.proxyport=1234-jar Selenium-server.jar
If your HTTP proxy server needs to be validated, you can also set up-dhttp.proxyuser and-dhttp.proxypassword after Http.proxyhost and Http.proxyport.
Java-dhttp.proxyhost=myproxy.com-dhttp.proxyport=1234-dhttp.proxyuser=joe-dhttp.proxypassword=example-jar Selenium-server.jar
iii. writing selenium RC scripts
1. Create a new project and introduce Selenium-java-client-driver.jar in project
2, create a new Java class, enter the following code:
Package selenium.test; Import com.thoughtworks.selenium.*; public class Seleniumtest {private Selenium Selenium. public void SetUp () {Selenium = new Defaultselenium ("10.5.41.55", 4444, "*iexplore", "http://www.google.com/"); Selenium.start (); public void Testgoogle () {Selenium.open ("/"); Selenium.type ("Q", "Selenium"); Selenium.click ("btng"); Selenium.waitforpagetoload ("30000"); Boolean TestResult = (selenium.istextpresent ("Selenium Web Application Testing System")); if (TestResult) {//Use case Success System.out.print ("Search Selenium web is ok!");} else {//Use case failure System.out.print ("Selenium web not Found! ");}} public static void Main (string[] args) {Seleniumtest st = new Seleniumtest (); St.setup (); St.testgoogle ();}}
Try to run it and look at the results. The previous code, do the following several things:
l Start Selenium case
Selenium = new Defaultselenium ("Server address",
Port number, "Start Mode", "Object Url--baseurl");
Selenium.start ();
Where the startup mode is the way to start the browser, which is commonly used in *firefox, *iehta, *iexplore. All modes are as follows:
*konqueror
*firefox: Launch Firefox as Test browser
*iexploreproxy
*firefoxproxy
*safari
*safariproxy
*iexplore: Start IE as a test browser
*pifirefox
*chrome
*firefox2
*piiexplore
*googlechrome
*iehta: Test with the local application HTA mode
*firefox3
*mock
*opera
*custom
L Send instructions to the browser via Selenium server simulate user actions
Open BaseURL in the browser, that is http://www.google.cn
Selenium.open ("/");
In the text input box with the ID Q, type "Selenium"
Selenium.type ("Q", "Selenium");
Click on ID or name is the button of btng, that is, click on Google's Google search button
Selenium.click ("btng");
Wait for page load, parameter unit is milliseconds
Selenium.waitforpagetoload ("30000");
l Judgment Results
Istextpresent is to find the specified text from the current page, and the return value is Boolean. There are, of course, many ways of judging results. Refer to Selenium reference (http://release.seleniumhq.org/selenium-remote-control/1.0-beta-2/doc/java/)
Boolean TestResult = (selenium.istextpresent ("Selenium Web Application Testing System"));
Iv. Summary
Through the above introduction, I think you can already use selenium RC to write test script. Of course, before we start, we need to take a detailed look at the reusability and maintainability of the test scripts, and plan the test project in detail, such as data-driven, scripted modularity, test suite, and so on.