How to do web automation test using Selenium

Source: Internet
Author: User
Tags seleniumhq

The project consultant suggested using selenium for automated testing of Web applications, and decided to automate testing of functional modules with simple business logic and stable functions, such as simple login modules. The following is a brief summary of the process.

I ran selenium on Ubuntu.

1. Prepare the 1.1 Java environment

Install JDK on/usr/local/jdk1.6.0 _ 24

Java ide 1.2

Because selenium supports Java/C #/Ruby/python, I chose Java here. Download the latest eclipse on the eclipse official website, which is free of installation and can be decompressed.

1.3 Selenium

Go to the official selenium website selenium IDE, selenium server, selenium client Driver (only Java version is selected here)


Selenium ideIt must be installed. Currently, it is only supported by the Firefox browser. After the installation is complete, it serves as the Firefox plug-in. Click Tools-selenium ide To start recording the script.

Selenium ServerThe downloaded jar package is put under/usr/local/. You need to add this package when writing test in eclipse.

Selenium client driverDownload a zip package, decompress it to/usr/local/with the unzip, there is/usr/local/selenium-2.8.0/selenium-java-2.8.0.jar package in the decompressed path, add this package when writing test in eclipse

Between the above three download itemsLinkYes:

Selenium IDE can record scripts, execute scripts, modify scripts, and export the recorded scripts to other languages (such as Java );

Selenium server is used in combination with selenium client. Server is used to start a browser, accept client requests, and execute test;

The client sends a request to the server.

2. Recording script

2.1 Open Firefox and Click Tools-selenium IDE (recording is enabled by default)

2.2 enter in the address bar of Firefox and press enter to open the Google homepage.

2.3 enter selenium in the search box and click Google search to go to the result page.

2.4 select selenium on the result page, right-click, and select verifytextpresent selenium from the shortcut menu.

2.5 click the first hyperlink on the result page.

2.6 stop recording

3. Export to Java junit4 (Remote Control)

In selenium IDE, click File-export test cases as..., select junit4 (remote control), and save

In addition, we can use the options-format function to directly display it as a Java Script. However, for the new version of selenium, this is disabled by default. You only need to start the format:

To turn it on, goOptions-> options and select 'Enable experimental features'

ReferenceHttp:// Tid = 1849 & page = e, it says "format option has been turned off in the latest release. as per the release notes, format changing is now marked as experimental due to possible issues, you can turn it on from the Options dialog."

4. Edit and run

4.1 create a Java project: file-New-Java project, and enter the project name (junittest). Others can be used by default. Of course, you can also modify which JRE to use.

4.2 create a new JUnit file in this project and the JUnit package will be imported automatically

4.3 add selenium package 4.3.1

Right-click the blank area of the left package and click build path-add External Archives ..., add the selenium-java-2.8.0.jar (client) and selenium-server-standalone-2.8.0.jar (server) downloaded/decompressed to usr/local, note that the jar of this server is required, although this package may not be imported in the Code (only when the Server API is used), if this package is not added, an error message will appear during execution: Java. lang. noclassdeffounderror: COM/Google/common/base/charsets, because this class is in the server package.

4.3.2 introduce the two packages in the newly created JUnit File

Import org. openqa. Selenium. server. seleniumserver; // server
Import com. thoughtworks. Selenium. *; // Client

4.4 copy the exported code in step 3 to the newly created JUnit file and modify it as appropriate.

4.5 run case4.5.1 to start selenium Server

(1) the startup method can be in the form of a command line Java-jar selenium-server-standalone-2.8.0.jar (pay attention to Java whether to configure the environment variables and jar package path)

In my system you can run/usr/local/jdk1.6.0 _ 24/bin/Java-Jar/usr/local/selenium-server-standalone-2.8.0.jar like this

(2) It can be called in testcase

Static seleniumserver server;
* @ Throws java. Lang. Exception
@ Beforeclass
Public static void setupbeforeclass () throws exception {
System. Out. Print ("setupbeforeclass \ n ");
Server = new seleniumserver ();
Server. Start ();

@ Afterclass
Public static void teardownafterclass () throws exception {
System. Out. Print ("teardownafterclass \ n ");
Server. Stop ();

4.5.2 possible problems

1. If Server jar is not added, a java. Lang. noclassdeffounderror: COM/Google/common/base/charsets error occurs.

2. If a new window needs to be opened in case, we can modify the settings of Firefox because Firefox does not allow pop-up:Edit-preferences-content: remove the check box before block pop-up windows.

5 References



Http:// /? Uid-146979-action-viewspace-itemid-242645



Http:// Tid = 1849 & page = E

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: 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.