Test web applications with Selenium RC

[复制链接]
查看11 | 回复9 | 2017-9-26 13:06:30 | 显示全部楼层 |阅读模式
Summary:Selenium is a testing framework used for automated Web application testing. Get to
know Selenium Remote Control (Selenium RC), which allows you to build tests for different
browsers to ensure your Web applications are of the highest quality.
回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
Unit testing frameworks, such as JUnit, let you test the code that runs on your
servers. However, in a typical web application, that service code is just a small part
of the total code in the application. Such an application can also have a lot of code
that is really only testable using a tool that works with a browser to test the
application.


One of the more challenging aspects of testing web applications is testing the
application's UI—the part of the application's code that is typically generated
from HTML and JavaScript code. It runs in a browser, not in a server process, so it is
only really testable from an Internet browser. Examples of this type of code include
JavaServer Pages (JSP) pages, PHP code, and Ruby.


This article introduces one tool in the community—Selenium—that you
can use to create and automate web tests. You will learn how to quickly create some
sample tests, how to expand them, and how to use Selenium Remote Control to run them as automated tests.
回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
Get to know Selenium RC

Selenium RC is part of a suite of tools from the Selenium project. It allows you
to run tests that you've created in an automated fashion. Selenium RC runs on
many different operating systems and can start an instance of different browsers,
including Windows? Internet Explorer?, Mozilla Firefox, and Opera.


Using Selenium RC, you can run tests automatically as many times as you want. The
tool also gives you the ability to create more complicated tests than you
can create using Selenium IDE alone. You can add conditional statements and
iteration to tests, which can help if you want to run tests with a set of data. You
can also handle expected exceptions using the constructs available to you in
unit testing frameworks such as JUnit.
回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
Downloading and installing Selenium RC

To get started with Selenium RC, you must first download and install it.
The Selenium server is simply a JAR file that you can run using the Java?
Runtime Environment (JRE). You can download the JAR file and other
supporting files from the SeleniumHQ site (see Resources
for a link).


When the download is complete, extract the archive file containing Selenium RC,
and expand it to a location that you'll remember later. You can execute the
Selenium server by running the following command:

java -jar selenium-server.jar
回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
Sample application to test

This article uses a simple dynamic web application with JSP pages to demonstrate
how Selenium RC works and can be used in automated testing. The simple web
application has two pages: a login page and a page on which you can enter
information such as your name and birth date, from which the application calculates
your age and says "Hello."


This sample application provides an opportunity to show how you can execute tests to
work with different conditions. The first page is shown in Listing 1.
This is the simple login page that lets you log in to the web application. To
keep things simple for this example, the login information is compared against
some hard-coded strings.

Listing 1. The index.jsp page





Test Login Page

" method="POST">

Username:


Password:





回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
The second page is shown in Listing 2. When you have entered
your first name and birth date, the page simply says "Hello" and tells you your
age as of today. This example is a bit contrived, because in most web applications,
such information would require a login, and the data would be stored for your next
login.

Listing 2. The enterInfo.jsp page






Insert title here

" method="POST">

Your name:


Your birth date (in MM/DD/YYYY format):


Please enter a valid date.
");
} else {

// display the nice messages...
}

}
%>



回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
Writing the first test

To quickly write some tests that you can use as a foundation for your automated
tests, you can use Selenium IDE to get started. Selenium IDE is a plug-in for
Firefox that lets you record tests. You can then export the recorded tests
so that you can add fanciness to them—conditions, iterations, and so on.


To get started with Selenium IDE, download it from the link in Resources. Install it as a Firefox plug-in by
clicking the link. Firefox should prompt you to install the plug-in, and then you must
restart the browser for the changes to take effect.


After you have the plug-in installed, start your server so you can begin using your web
application. Open Selenium IDE by clicking Tools > Selenium IDE
in Firefox. When Selenium IDE is open, click Record. After Selenium
IDE is recording, it will remember all of the actions that you take in the browser. To
log in to your sample application, perform the following steps:


With Selenium IDE recording, navigate to the index.jsp web page.Type your user name.Type the valid password in the Password box.Click Login.
Upon successful login, your web application should go to the enterInfo.jsp page. At
this point, your test has the actions in it, but so far, there is nothing to verify
that your actions worked. Selenium needs to know what to look for in order to
know that your enterInfo.jsp page is being rendered as expected.


You can add verify actions to make sure your application is displaying the correct
content. While Selenium is still recording, perform the following steps:


Right-click one of the HTML elements, such as the
Your name label, and then click
verifyTextPresent Your name:.Repeat step 1 with the Your birth date label.Click Record to stop recording.
If you want to see your test in action, click Run All (see
Figure 1). The test will execute and pass.



Figure 1. Clicking Run All


回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
To see how Selenium IDE is really testing your application, go to your IDE and
change the value of the valid user name. Re-deploy your application, and then click
Run All again. This time, your tests will fail, because the web
application will not display the enterInfo.jsp page with the correct labels.


Exporting the test to JUnit

Now that you have recorded your first test, you can export it for use in JUnit.
With your test highlighted in Selenium IDE, click File > Export
Test Case As > Java (JUnit) - Selenium RC. Type the name for
your test (for example, IndexTests.java), and save it where you
can remember it so that you can import it into Eclipse.


Perform the following steps:

Create a new Java project that includes the Java unit tests
built using JUnit.Download the Selenium RC binaries (see Resources
for a link). Save the archive file to a location where you can
import the files inside it into your new Java project in Eclipse.Create a new folder in your Java project called lib.Click File > Import, and then click File System
from the list. Click Next.Browse to the location in which you extracted the Selenium RC files,
and choose the selenium-java-client-driver-1.0.1 directory.Select the selenium-java-client-driver.jar from the list, and click
OK to import the Selenium Java client driver
JAR file into your project's lib directory.Import the Java file that you exported from Selenium IDE into the src
directory of your new Java project.
回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
The first thing you'll notice is that you will get many compile errors. First, your
new Java file is probably not in the correct package, because it's directly in
the src folder. Second, the JUnit or Selenium classes cannot be found.
Fortunately, these errors are easy to resolve using Eclipse.


Resolving errors in Eclipse

Resolve the package errors by clicking the error, and then pressing Ctrl-1 to
open the hints. Select Move MyTest.java to package 'com.example.mywebapp.tests'.
Eclipse creates the package structure for you and moves the file.


Adding the JUnit test

Now, add the Selenium and JUnit test by clicking the Java project in Package
Explorer and clicking Build Path > Configure Build Path.
On the Libraries tab, click Add JARs, and
select the selenium-java-client-driver.jar file. Click OK.


When the Java source file is compiling, it should look similar to Listing 3.
回复

使用道具 举报

千问 | 2017-9-26 13:06:30 | 显示全部楼层
Listing 3. The JUnit test based on the exported code
from Selenium IDE



package com.example.mywebapp.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import org.junit.BeforeClass;
import org.junit.Test;
public class MyTest extends SeleneseTestCase {

@BeforeClass
public void setUp() throws Exception {
setUp("http://localhost:8080/tested-webapp/index.jsp", "*chrome");
}

@Test
public void testMy() throws Exception {
selenium.open("/tested-webapp/index.jsp");
selenium.type("username", "user");
selenium.type("password", "secret");
selenium.click("//input[@value='Login']");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("Your name:"));
verifyTrue(selenium.isTextPresent("Your birth date (in MM/DD/YYYY format):"));
}
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行