Automated WebTesting with Selenium RC

本文深入探讨了Selenium工具的使用,包括其版本、许可、定价、支持渠道、功能概述、安装流程、.NET客户端驱动开发示例、优势、注意事项、测试结构改进方法(如Page Objects模式)、维护自动化测试原则以及其它Selenium项目介绍。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.methodsandtools.com/tools/tools.php?selenium

 

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: http://seleniumhq.org
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, see http://seleniumhq.org/support/

Overview

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

  • Selenium Server: Uses Selenium core and browser抯 built-in JavaScript interpreter to process selenese commands (such as click, type) and report back results.
  • Selenium Client Libraries: Are the API抯 for the programming languages to communicate with Selenium server.

Running Selenium Server

Download Selenium RC from http://Seleniumhq.org/download/, 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)

  1. Create a test project.
  2. Add a reference to ThoughtWorks.Selenium.Core.dll on the project (this is found in the Selenium RC zip under Selenium-remote-control-1.0.3\Selenium-dotnet-client-driver-1.0.1 directory).
  3. Create a test class with the following structure:
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:

  • Host name on which the Selenium server is running (localhost).
  • The port on which Selenium server is listening (when we started Selenium server by default it listens on port 4444).
  • The command string used to launch the browser, e.g. "*firefox", "*iexplore" or "c:\\program files\\internet explorer\\iexplore.exe",
  • The starting URL, Selenium starts the browser pointing at the Selenium resources on this URL (http://seleniumhq.org/).

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:

  • Execute regression tests easily and have quick feedback about the application抯 status.
  • Run the same set of tests with different browsers, we抳e caught functional errors present in one browser and not in the others.
  • Run the same set of tests on different code branches (and browsers) on daily basis in a continuous integration environment.

When writing Selenium tests remember

  • Tests that access elements by id run faster than accessing elements using xpath expressions.
  • Use tools like xpather and firebug to quickly locate elements.
  • Selenium IDE is handy to record Selenium commands while executing interactions on the UI.
  • Run your Selenium tests automatically in a controlled environment using continuous integration tools which involves automated build, deploy and testing process.
  • You can run multiple tests at the same time running Selenium server on different ports.

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

  • Code duplication and tests have high dependency with the page抯 HTML structure. This means that changes in a single page will affect different tests. When the application changes, tests will start breaking and this will be hard to maintain over the time.
  • Readability issues: Tests are not easy to read. Is difficult to know what the test is doing.

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:

  • Compose a new email
  • Read a single email

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

  1. Represent the screens of your web app as a series of objects
  2. Do not need to represent an entire page
  3. Public methods represent the services that the page offers
  4. Try not to expose the internals of the page
  5. Generally don't make assertions
  6. Methods return other PageObjects
  7. Different results for the same action are modeled as different methods
  8. Check that the "Test Framework" is on the correct page when we instantiate the PageObject

Benefits achieved by applying page objects

  • There is one place having the knowledge of the structure of the pages (the page object)
  • Navigation between the pages.
  • Changes in a page are in one place (reducing duplication).
  • Easy to locate code.
  • Less dependency between the test cases and Selenium, since most Selenium code will be located on the page object.
  • As the amount of tests increases, the page objects represent a smaller percentage of the overall test code.

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:

  1. Readability: We want tests to be written in a way that even a final user can read them and understand them.
  2. Maintainability: Writing automated test with c# (or other programming language) and Selenium is equivalent as writing application code, so we should follow coding best practice and OO principles.
  3. Robustness & Flexibility: Robust tests that won抰 break with small changes, being able to do changes with reduced impact. Tests should be repeatable: I can run it repeatedly and it will pass or fail the same way each time.
  4. Collaboration & Team Work: We want our tests structured in a way that allows easy collaboration and reuse between team members.

Summary of other Selenium projects

  • Selenium IDE is a Firefox add-on that allows you to record and playback actions performed on a webpage. Also you can format recorded tests to port them to Selenium RC (C#, java, perl, php, python, ruby), when doing so modify them with maintainability considerations mentioned on the article. Using this is fine to start, but it quickly becomes faster coding the tests directly.
  • Selenium Grid is a solution to scale Selenium RC tests, allowing running tests on parallel, different machines and environments.
  • Selenium 2 is the next version of Selenium RC, which is the result of merging WebDriver and Selenium RC. WebDriver is another tool for writing automated tests of websites but was designed to address some Selenium RC limitations like Same Origin Policy. The difference is that WebDriver controls the browser itself using native methods of the browser and operating system.
  • Selenium 2 supports the WebDriver API and is backward compatible with Selenium RC, which means you can still run tests developed with this version.

References

Selenium client libraries: http://seleniumhq.org/docs/05_Selenium_rc.html#programming-your-test

Xpather: https://addons.mozilla.org/en-US/firefox/addon/xpather/

Firebug: https://addons.mozilla.org/es-es/firefox/addon/firebug/

Page Objects: http://code.google.com/p/Selenium/wiki/PageObjects


More Software Testing Content

基于Spring Boot搭建的一个多功能在线学习系统的实现细节。系统分为管理员和用户两个主要模块。管理员负责视频、文件和文章资料的管理以及系统运营维护;用户则可以进行视频播放、资料下载、参与学习论坛并享受个性化学习服务。文中重点探讨了文件下载的安全性和性能优化(如使用Resource对象避免内存溢出),积分排行榜的高效实现(采用Redis Sorted Set结构),敏感词过滤机制(利用DFA算法构建内存过滤树)以及视频播放的浏览器兼容性解决方案(通过FFmpeg调整MOOV原子位置)。此外,还提到了权限管理方面自定义动态加载器的应用,提高了系统的灵活性和易用性。 适合人群:对Spring Boot有一定了解,希望深入理解其实际应用的技术人员,尤其是从事在线教育平台开发的相关从业者。 使用场景及目标:适用于需要快速搭建稳定高效的在线学习平台的企业或团队。目标在于提供一套完整的解决方案,涵盖从资源管理到用户体验优化等多个方面,帮助开发者更好地理解和掌握Spring Boot框架的实际运用技巧。 其他说明:文中不仅提供了具体的代码示例和技术思路,还分享了许多实践经验教训,对于提高项目质量有着重要的指导意义。同时强调了安全性、性能优化等方面的重要性,确保系统能够应对大规模用户的并发访问需求。
标题基于SpringBoot的学生学习成果管理平台研究AI更换标题第1章引言介绍研究背景、目的、意义以及论文结构。1.1研究背景与目的阐述学生学习成果管理的重要性及SpringBoot技术的优势。1.2研究意义分析该平台对学生、教师及教育机构的意义。1.3论文方法与结构简要介绍论文的研究方法和整体结构。第2章相关理论与技术概述SpringBoot框架、学习成果管理理论及相关技术。2.1SpringBoot框架简介介绍SpringBoot的基本概念、特点及应用领域。2.2学习成果管理理论基础阐述学习成果管理的核心理论和发展趋势。2.3相关技术分析分析平台开发所涉及的关键技术,如数据库、前端技术等。第3章平台需求分析与设计详细分析平台需求,并设计整体架构及功能模块。3.1需求分析从学生、教师、管理员等角度对平台需求进行深入分析。3.2整体架构设计设计平台的整体架构,包括技术架构和逻辑架构。3.3功能模块设计具体设计平台的核心功能模块,如成果展示、数据分析等。第4章平台实现与测试阐述平台的实现过程,并进行功能测试与性能分析。4.1平台实现详细介绍平台的开发环境、关键代码实现及技术难点解决方案。4.2功能测试对平台各项功能进行全面测试,确保功能正确无误。4.3性能分析分析平台的性能指标,如响应时间、并发处理能力等。第5章平台应用与效果评估探讨平台在实际教学中的应用,并对其效果进行评估。5.1平台应用案例选取典型应用案例,展示平台在实际教学中的使用情况。5.2效果评估方法介绍平台效果评估的具体方法和指标。5.3评估结果分析根据评估数据,对平台的应用效果进行深入分析。第6章结论与展望总结论文的主要研究成果,并指出未来研究方向。6.1研究结论概括性地阐述论文的研究结论和主要贡献。6.2研究展望针对当前研究的不足之处,提出未来改进和扩展的方向。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值