本文共 10941 字,大约阅读时间需要 36 分钟。
Selenium RC (or Selenium 1) is a popular tool for writing automated tests of web applications. You can develop automated tests in the programming language of your choice such as c#, java, python, php, perl and ruby as well as running those tests on different combination of browsers such as Chrome, Firefox or IE.
Web Site: Version: Selenium RC 1.0.3
License & Pricing: All Selenium projects are licensed under the Apache 2.0 License. Support: There are many places where you can find support, seeOverview
Selenium project gathers a set of tools for writing automated tests of websites: Selenium RC (remote control), Selenium IDE, Selenium Grid and Selenium 2 (on beta) which is the next version of Selenium RC.
These tools emerged from a javascript library that was used to drive interactions on a webpage on multiple browsers called Selenium Core.
Selenium RC is a client/server based application that allows you to control web browsers using the following components
Running Selenium Server
Download Selenium RC from , the zip contains Selenium server, a Java jar file (Selenium-server.jar).
Selenium server must be running to be able to execute the tests. You can run it using the following command:
C:\>java -jar [SeleniumServerPath]\selenium-server.jar -interactive
Hello World Selenium RC
The following example uses c#, but a similar approach can be followed using others client driver libraries to develop tests in java, python, php, perl and ruby.
Using Selenium .Net client driver and Visual Studio 2010 (or 2008 Professional Edition)
using Selenium;namespace TestProject1{ [TestClass] public class SeleniumPageTest { private ISelenium Selenium; [TestInitialize()] public void MyTestInitialize() { Selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://seleniumhq.org/"); Selenium.Start(); } [TestCleanup()] public void MyTestCleanup() { Selenium.Stop(); } [TestMethod] public void CheckProjectsLink() { Selenium.Open("http://Seleniumhq.org/"); Selenium.Click("link=Projects"); Selenium.WaitForPageToLoad("3000"); Assert.IsTrue(Selenium.IsTextPresent("Selenium IDE")); } }}
Run this test in Visual Studio like you do with a regular unit test.
MyTestInitialize
This method initializes Selenium by creating an instance of DefaultSelenium (Default implementation of Selenium interface) specifying the following parameters:
The start method lunches the browser and begins a new Selenium testing session.
MyTestCleanup
The stop method ends the Selenium testing session and kills the browser.
CheckProjectsLink
This is a simple test that opens http://seleniumhq.org page, clicks on the "Projects" link, waits for the page to load (with a timeout of 3 seconds) and asserts that the text "Selenium IDE" is present on the page.
Benefits of having Selenium automated tests
Selenium automated tests have provided the following benefits on my projects:
When writing Selenium tests remember
Unstructured Tests
One common approach is to start developing automated tests having basic structure: test method, test initialize and cleanup, as shown in the SeleniumPageTest class.
This may work well at the beginning, but projects ends up with tests like the following:
[TestMethod]public void RegisterUserTest(){// Starting Register User TestSelenium.Open("www.mysite.com");Selenium.Click("lnkRegister");Selenium.WaitForPageToLoad("3000");Selenium.Click("btnRegister");Assert.IsTrue(Selenium.IsTextPresent("Please enter required fields"));Selenium.Type("id_password", "123456");Selenium.Type("id_password_2", "654123");Selenium.Click("btnRegister");Assert.IsTrue(Selenium.IsTextPresent("Passwords must match"));Selenium.Type("id_email", "mytest@email.com");Selenium.Type("id_first_name", "John");Selenium.Type("id_last_name", "Doe");Selenium.Type("id_password", "xxx#ZZ1");Selenium.Type("id_password_2", "xxx#ZZ1");Selenium.Click("id_acept_terms"); Selenium.Click("btnRegister");Selenium.WaitForPageToLoad("3000");Assert.IsTrue(Selenium.IsTextPresent("Welcome John Doe, logout"));Selenium.Click("lnkLogout");Selenium.WaitForPageToLoad("3000");// RegisterUsTest Completed}
The above test has the following issues:
Page Objects
Page Objects is a pattern that helps structure automated test code to overcome maintainability issues; this is how page objects helps:
Methods on a page object represent the "services" that a page offers (rather than exposing the details and mechanics of the page). For example the services offered by the Inbox page of any web-based email system:
How these are implemented shouldn't matter to the test.
The benefit is that there is only one place in your test suite with knowledge of the structure of the HTML of a particular (part of a) page.
Summary of Page Objects
Benefits achieved by applying page objects
Page Objects Tests
This is how unstructured tests will look after applying page object pattern:
[TestMethod]public void TestRegisterUserEmptyFieldsValidation(){ var user = new User(); // empty user var registrationPage = RegisterUserExpectingErrors(user); Assert.IsTrue(registrationPage.IsRequiredFieldsMessagePresent());}[TestMethod]public void TestRegisterUserPasswordMustMatch(){ var user = new User() { Password = "123456", Password2 = "654123" }; var registrationPage = RegisterUserExpectingErrors(user); Assert.IsTrue(registrationPage.IsPasswordsMustMatchMessagePresent());}[TestMethod]public void TestRegisterUserSucessfully(){ var user = new User() { Email = "mytest@email.com", FirstName = "John", LastName = "Doe", Password = "xxx#ZZ1", Password2 = "xxx#ZZ1" }; var homePage = new HomePage(Selenium).SelectRegisterUser(); var adminPage = registrationPage.FillUpRegistrationForm(user); .RegisterUserSucessfully(); Assert.IsTrue(adminPage.IsWelcomeBackMessagePresent(user.FirstName)); adminPage.Logout();}private RegistrationPage RegisterUserExpectingErrors(User user){ var registrationPage = new HomePage(Selenium).SelectRegisterUser(); return registrationPage.FillUpRegistrationForm(user) .RegisterUserExpectingErrors();}
Writing maintainable automated tests
Below are key principles our team follows when writing automated tests:
Summary of other Selenium projects
References
Selenium client libraries:
Xpather:
Firebug:
Page Objects:
More Software Testing Content
转载地址:http://jhjab.baihongyu.com/