Introduction to selenium (III)
-- RC-based simple application
Author: torrent
Read before reading:
Selenium introduction (1)-General Introduction
Introduction to selenium (II) -- simple applications based on core/ide
In this article "completely interpreting selenium", we briefly introduce the principle of selenium RC. Next, let's review it:
I. Principle and introduction of selenium RC
In selenium RC mode, the client sends commands to selenium server over the network using various programming languages. After selenium server receives the test commands, start the browser and send a javascript call to it to fully track the HTML page, and return the execution result to the caller through the network.
The principle of selenium server is to embed your Javascript file into a webpage when you open your browser. Then, the selenium webpage is embedded into the target Webpage Through frame. In this way, you can use the JavaScript Object of selenium to control the target webpage.
The selenium client is generally implemented using unit test technology. It determines whether the program runs correctly by judging whether the returned results are consistent with the expected ones.
For example:
2. Preparations
1. language selection
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 use any of the above languages to compile selenium test scripts. This article introduces selenium RC based on the widely used Java language.
2. Software preparation
Selenium Remote Control Server
: Http://seleniumhq.org/download/
Eclipse
: Http://www.eclipse.org/downloads/
3. Start selenium Remote Control Server
First in the downloaded RC package, decompress it, find the selenium-server.jar, and then start it in the command line window, the specific startup format is as follows (optional parameters in brackets ):
The Java-jar selenium-server.jar [-interactive] [Options]
-Port <NNNN>: the port number used by the selenium server (4444 by default)
-Timeout <NNNN>: the number of seconds that we waited for before giving up (timeout ).
-Interactive: Enter the interaction mode. Refer to the tutorial for more information
-Multiwindow: the mode in which all tested websites are opened in a separate window. Selenium supports frame.
-Forcedbrowsermode <Browser>: sets the browser mode (for example, all sessions use "* iexplore", regardless of the parameter passed to getnewbrowsersession)
-Userextensions <File>: Specifies a Javascript file loaded to selenium.
-Browsersessionreuse: Stop reinitializing and replacing the browser during the test.
-Alwaysproxy: by default, we can perform as few proxies as possible. setting this flag will force all browser communications to pass through the proxy.
-Firefoxprofiletemplate <dir>: Generally, a clean Firefox setting is generated before each startup. You can specify a directory for us to copy your settings instead of what we generated.
-Debug: enters the debug mode and more tracing debugging information is available.
-Htmlsuite <Browser> <starturl> <suitefile> <resultfile>: Use the specified browser (for example, "* Firefox") at the specified URL (for example, "http://www.baidu.com "), run a separate HTML selenese (selenium core) test suite and exit immediately. You must specify the absolute path of the HTML test suite and the path of the HTML test result file that we will generate.
-Proxyinjectionmode: Enter the proxy injection mode. In this mode, the selenium server acts as the proxy server that enters all the content of the test program. In this mode, access is allowed across multiple domains, and the following additional parameters are also supported:
-Dontinjectregex <RegEx>: the added regular expression. The proxy injection mode can be used to determine whether to inject the regular expression.
-Userjsinjection <File>: Specifies a Javascript file and injects it into all pages.
-Usercontenttransformation <RegEx> <replacement>: a regular expression that matches all tested HTML content. The second string replaces all matched content. This flag can be used multiple times. A simple example of using this parameter: If you add "-usercontenttransformation https http", all "HTTPS" strings in the HTML of the test application will be replaced with "HTTP ".
In addition, two types of Java System attributes are supported:-dhttp. proxyhost and-dhttp. proxyport. The selenium server is used as the proxy server. Selenium RC generally reloads the configuration of your proxy server. This parameter is suitable for using your own proxy server while using the selenium server proxy. This configuration is used when the proxy server is used:
Java-dhttp. proxyhost = myproxy.com-dhttp. proxyport = 1234-jar selenium-server.jar
If your HTTP Proxy server needs verification, you can also set-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
3. Compile the selenium RC Script
1. Create a new project, introduce selenium-java-client-driver.jar in the project
2. Create a New Java class and enter the following code:
Package selenium. test;
Import com. thoughtworks. Selenium .*;
Public class seleniumtest {
Private selenium;
Public void setup (){
Selenium = new defaselselenium ("10.5.41.55 ",
4444, "* iexplore", "" http://www.baidu.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 successful
System. Out. Print ("Search selenium web is OK! ");
} Else {
// Case failed
System. Out. Print ("Selenium web not found! ");
}
}
Public static void main (string [] ARGs ){
Seleniumtest ST = new seleniumtest ();
St. Setup ();
St. testgoogle ();
}
}
Package selenium. test;
Import com. thoughtworks. Selenium .*;
Public class seleniumtest {
Private selenium;
Public void setup (){
Selenium = new defaselselenium ("10.5.41.55 ",
4444, "* iexplore", "" http://www.baidu.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 successful
System. Out. Print ("Search selenium web is OK! ");
} Else {
// Case failed
System. Out. Print ("Selenium web not found! ");
}
}
Public static void main (string [] ARGs ){
Seleniumtest ST = new seleniumtest ();
St. Setup ();
St. testgoogle ();
}
}
Run the command and check the result. The previous Code does the following:
L selenium startup example
Selenium = new defaselselenium ("server address ",
Port number, "Start mode", "tested object URL-baseurl ");
Selenium. Start ();
Specifically, the startup mode is the method used to start the browser. commonly used methods include * Firefox, * iehta, * iexplore. The full mode is as follows:
* Konqueror
* Firefox: Start Firefox as a test Browser
* Iexploreproxy
* Firefoxproxy
* Safari
* Safariproxy
* Iexplore: start IE as a test browser.
* Pifirefox
* Chrome
* Firefox2
* Piistme
* Googlechrome
* Iehta: Test in HTA mode of a local application
* Firefox3
* Mock
* Opera
* Custom
L send commands to the browser through selenium server to simulate user operations
Open baseurl in a browser, that is, "http://www.baidu.com
Selenium. Open ("/");
In the text input box where ID is Q, enter "Selenium"
Selenium. Type ("Q", "Selenium ");
Click the button whose ID or name is btng, that is, click Google search.
Selenium. Click ("btng ");
Wait for the page to load. The parameter unit is milliseconds.
Selenium. waitforpagetoload ("30000 ");
L judgment result
Istextpresent searches for the specified text from the current page and returns a Boolean value. Of course, there are multiple ways to judge the 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 use selenium RC to compile the test script. Of course, before starting, we also need to consider the reusability and maintainability requirements of the test script in detail and plan the test project in detail, such as data-driven, script modularization, and test suite.
This article from csdn blog: http://blog.csdn.net/iamqa/archive/2009/08/04/4409209.aspx