Selenium Primer

37
Selenium–Functional Testing Tool by ThoughtWorks February 2007

Transcript of Selenium Primer

Page 1: Selenium Primer

Selenium–Functional Testing Tool by ThoughtWorks

February 2007

Page 2: Selenium Primer

© Copyright 2007 Sapient Corporation 2TestDirector -Test Management Tool

Agenda

What is Selenium?

Implementing Selenium

Functional Primer on Selenium

Key FAQ’s

References and Contacts

Selenium-Functional Testing ToolSelenium-Functional Testing Tool

Page 3: Selenium Primer

© Copyright 2007 Sapient Corporation 3TestDirector -Test Management Tool

What is Selenium?

Selenium OverviewWhat can be done with Selenium?What can’t be done with Selenium?Selenium ComponentsThe Selenium Testing Process

Selenium-Functional Testing Tool

Page 4: Selenium Primer

© Copyright 2007 Sapient Corporation 4TestDirector -Test Management Tool

Selenium Overview

Selenium is a framework for testing websites

Selenium tests run directly in a browser

A wide array of browsers supported by Selenium are Internet Explorer, Mozilla, Firefox on Windows, Linux, and Macintosh

Selenium uses JavaScript and I frames to embed a test automation engine in the browser

It work with any JavaScript-enabled browser

Selenium works with the following OS / Browser combinations:

Selenium-Functional Testing Tool

Internet Explorer Mozilla Firefox Safari

Windows XP 6.0 1.6+, 1.7+ 0.8+, 0.9+, 1.0

Red Hat Linux 1.6+, 1.7+ 0.8+, 0.9+, 1.0+

Mac OS X 10.3 Not supported 1.6+, 1.7+ 0.8+, 0.9+, 1.0+ 1.3+

Page 5: Selenium Primer

© Copyright 2007 Sapient Corporation 5TestDirector -Test Management Tool

What can be done with Selenium?

Browser compatibility testingTest your application to see if it works correctly on different browsers and operating systems

The same script can run on any Selenium platform

System functional testingCreate regression tests to verify application functionality and user acceptance

Integration testing

Run tests and see results on the same page

Generate Defect Reports

Selenium can be extended using Java scripts

Selenium supports handling dynamically generated elements

Selenium supports testing applications built using Ajax

Selenium-Functional Testing Tool

Page 6: Selenium Primer

© Copyright 2007 Sapient Corporation 6TestDirector -Test Management Tool

What can’t be done with Selenium?

Data driven TestingMeans that scripts read data from an external storage site, for example, from a file or database, rather than use values hard-coded in the script

Performance Testing

Load Testing

Test drag and Drop

Selenium-Functional Testing Tool

Page 7: Selenium Primer

© Copyright 2007 Sapient Corporation 7TestDirector -Test Management Tool

Selenium Components

Selenium Core

Selenium IDE

Selenium RC

Selenium-Functional Testing Tool

Page 8: Selenium Primer

© Copyright 2007 Sapient Corporation 8TestDirector -Test Management Tool

Selenium Core

Selenium Core is a test tool for web applications

Selenium Core tests run directly in a browser, just as real users do

Selenium Core uses a unique mechanism which allows it to run on so many platforms

Selenium Core is written in pure JavaScript/DHTML

You can copy Selenium Core tests directly into their application web server, allowing the tests to run in any supported browser on the client-side

You must have write access to the machine their web application server is running on to install Selenium Core

Selenium-Functional Testing Tool

Page 9: Selenium Primer

© Copyright 2007 Sapient Corporation 9TestDirector -Test Management Tool

Selenium-on-Rails

Selenium on Rails provides an easy way to test Rails application with Selenium Core

This plug-in does four things:The Selenium Core files don’t have to pollute /public, they can stay in the Selenium gem or in /vendor/selenium

No need to create suite files, they are generated on the fly — one suite per directory in /test/selenium (suites can be nested)

Instead of writing the test cases in HTML you can use a number of better formats like selenese(.sel), Rselenese(.rsel), HTML/RHTML

Loading of fixtures and wiping of session (/selenium/setup)

Selenium-Functional Testing Tool

Page 10: Selenium Primer

© Copyright 2007 Sapient Corporation 10TestDirector -Test Management Tool

Selenium IDE

Selenium IDE is an integrated development environment for Selenium testsIt is implemented as a Firefox extension, and allows user to record, edit, and debug testsIt includes the entire Selenium Core, allowing user to easily and quickly record and play back tests in the actual environment that they will runSelenium IDE is not only recording tool: it is a complete IDE

You can choose to use its recording capability, or you may edit your scripts by handWith autocomplete support and the ability to move commands around quickly, Selenium IDE is the ideal environment for creating Selenium tests no matter what style of tests you prefer

Features are:Easy record and playback Intelligent field selection will use IDs, names, or XPath as needed Auto complete for all common Selenium commands Walk through tests Debug and set breakpoints Save tests as HTML, Ruby scripts, or any other format Support for Selenium user-extensions.js file Option to automatically assert the title of every page

Selenium-Functional Testing Tool

Page 11: Selenium Primer

© Copyright 2007 Sapient Corporation 11TestDirector -Test Management ToolSelenium-Functional Testing Tool

Selenium IDE

Page 12: Selenium Primer

© Copyright 2007 Sapient Corporation 12TestDirector -Test Management Tool

Selenium Remote Control

Selenium Remote Control is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser

It provides a Selenium Server, which can automatically start/stop/control any supported browser. It works by using Selenium Core, a pure-HTML+JS library that performs automated tasks in JavaScript

The Selenium Server communicates directly with the browser using AJAX (XmlHttpRequest)

User can send commands directly to the Server using simple HTTP GET/POST requests; that means that user can use any programming language that can make HTTP requests to automate Selenium tests on the browser

Finally, the Selenium Server acts as a client-configured HTTP proxy, to stand in between the browser and user’s website which allows a Selenium-enabled browser to run JavaScript on arbitrary websites

The Selenium Server is great for testing complex AJAX-based web user interfaces under a Continuous Integration system

Selenium-Functional Testing Tool

Page 13: Selenium Primer

© Copyright 2007 Sapient Corporation 13TestDirector -Test Management Tool

The Selenium Testing Modes

The test management with Selenium involves:Selenium Core Mode

Selenium-Functional Testing Tool

Page 14: Selenium Primer

© Copyright 2007 Sapient Corporation 14TestDirector -Test Management Tool

Selenium Core ModeSelenium Core scripts, also referred to as test cases, are written in HTML using a simple table layout shown in Listing 1

Listing 1. Selenium test case structure<table border="1"> <tr>

<td>First command</td> <td>Target</td><td>Value</td>

</tr><tr>

<td>Second command</td><td>Target</td><td>Value</td>

</tr></table>

Test runner scripts are usually deployed on the same server as the application under test (AUT). This is because the browser bot uses JavaScript's to emulate user actions. These scripts run in a restricted sandbox environment. If you need to circumvent these restrictions, use a proxy

Selenium-Functional Testing Tool

Page 15: Selenium Primer

© Copyright 2007 Sapient Corporation 15TestDirector -Test Management Tool

Installing Selenium

Selenium-Functional Testing Tool

Page 16: Selenium Primer

© Copyright 2007 Sapient Corporation 16TestDirector -Test Management Tool

URL http://selenium.thoughtworks.com/index.html

License/Price Open Source, freeware

How to download? http://selenium.thoughtworks.com/download.html

Setup Click through Install

Installs Browser Bot – the automation engine of Selenium

Ramp up Elementary knowledge of HTML

Working knowledge of JavaScript

Selenium configuration files

Selenium-Functional Testing Tool

Installing Selenium

Page 17: Selenium Primer

© Copyright 2007 Sapient Corporation 17TestDirector -Test Management Tool

Browser-Based Testing Survey of Selenium with other tools

Selenium-Functional Testing Tool

Tool Test  Case Language

Browse Compatibility

Notes

Avignon XML IE •Acceptance testing framework. Includes a new "Internet Explorer bridge" which likely plugs into the IE API•Built on top of JUnit and fixtures are written in Java•Seems to focus on HTTPUnit, which is not a browser-based technology•Some background in the Acceptance Testing HTML white paper

IeUnit JavaScript IE •xUnit test framework designed for general web testing•Includes a GUI test runner front end•No mailing list•First release October 2002

Pamie Python IE •Written by Rob Marchetti. Patterned after Samie•The author has an alpha version for VB as well•Active mailing list since July 2004.

Page 18: Selenium Primer

© Copyright 2007 Sapient Corporation 18TestDirector -Test Management Tool

Browser-Based Testing Survey of Selenium with other tools

Selenium-Functional Testing Tool

Tool Test  Case Language

Browse Compatibility

Notes

Jiffie Java IE •Used internally at Tapster Rock in the UK•Active mailing list.

Samie Perl IE •Written by Henry Wasserman•Also uses Win32::GUITEST•Mailing list is largely inactive, but the author is reachable•First release was February 2003.

Selenium Table-based IE, Mozilla, Fire fox, Safari

•Implemented in JavaScript•Works in several different browsers on Windows and Linux•Uses a FIT-like table interface and an object-based language reminiscent of Worksoft Certify and the latest release of Mercury QuickTest Pro, though it's not a complete keyword-driven framework•Requires extra work to avoid cross-site scripting security protections•Supports a variety of languages in RC mode•Sponsored by ThoughtWorks

Page 19: Selenium Primer

© Copyright 2007 Sapient Corporation 19TestDirector -Test Management Tool

Selenium Vs QTP (Industry Comparison Matrix)

Each category in the matrix is given a rating of 1 – 51 = Excellent support for this functionality

2 = Good support but lacking or another tool provides more effective support

3 = Basic/ support only

4 = This is only supported by use of an API call or third party add-in but not included in the general test tool/below average

5 = No support

Selenium QTP

Record & Playback 3 1

Web Testing 2 1

Database tests 5 1

Data functions 3 2

Object Mapping 4 1

Selenium-Functional Testing Tool

Page 20: Selenium Primer

© Copyright 2007 Sapient Corporation 20TestDirector -Test Management Tool

Selenium QTP

Image testing 5 1

Test/Error recovery 5 2

Object Name Map 3 1

Object Identity Tool 2 2

Extensible Language 2 2

Environment support 3 1

Integration 3 1

Cost 1 5

Ease of use 3 2

Support 4 1

Object Tests 2 1

Total 50 25

Selenium-Functional Testing Tool

Selenium Vs QTP (Industry Comparison Matrix)

Page 21: Selenium Primer

© Copyright 2007 Sapient Corporation 21TestDirector -Test Management Tool

Functional Primer on Selenium

Getting StartedInstalling Selenium CoreWriting Test CaseAdding Test Case to Test SuiteRunning Test CaseGenerating Defect ReportsCommandsDemos

Selenium-Functional Testing Tool

Page 22: Selenium Primer

© Copyright 2007 Sapient Corporation 22TestDirector -Test Management Tool

Getting Started

If this is your first time using a Selenium tool, you may not want to just use Selenium Core directly; one good way to get introduced to Selenium's features is to use Selenium IDE, which embeds Selenium Core internally

Download Selenium IDE and you'll get Selenium Core along with it

Selenium-Functional Testing Tool

Page 23: Selenium Primer

© Copyright 2007 Sapient Corporation 23TestDirector -Test Management Tool

Installing Selenium Core

To use Selenium Core, you need to make it available from the same web server as the application you want to test

This is a JavaScript security requirement. If you can't/won't modify the web server you want to test, Selenium Core may not be the right tool for you; you may just want to use Selenium IDE or Selenium RC instead

Extract the Selenium Core zip file into the DocumentRoot, htdocs, or webroot of your web server

Open the browser

Click Install to install the Selenium

Selenium-Functional Testing Tool

Page 24: Selenium Primer

© Copyright 2007 Sapient Corporation 24TestDirector -Test Management Tool

Writing Test CaseA test-case is represented by an HTML document written in "Selenese", containing a table with 3 columns, room enough for a command and two arguments

Not all commands take two arguments; in this case either leave the column blank or use a &nbsp; to make the table look better

Example:

Selenium-Functional Testing Tool

MyTest

open /mypage

type nameField John

click submitbutton True

verifytext Name John

Page 25: Selenium Primer

© Copyright 2007 Sapient Corporation 25TestDirector -Test Management Tool

Adding Test to Test SuiteMake new test case available by linking to it from the TestSuite; for example:

<table id="suiteTable" cellpadding="1" cellspacing="1" border="1"> <tbody>

<tr><td><b>Test Suite </b> </td> </tr>

<tr><td><a href="MyTest.html">MyTest</a></td></tr>

...etc...

</tbody>

</table>

Selenium-Functional Testing Tool

Page 26: Selenium Primer

© Copyright 2007 Sapient Corporation 26TestDirector -Test Management Tool

Run Test CaseOpen selenium Core, following screen should be displayed:

Selenium-Functional Testing Tool

Page 27: Selenium Primer

© Copyright 2007 Sapient Corporation 27TestDirector -Test Management Tool

Locate the name of your test case in the Suite Panel (top left). Clicking on it should cause Selenium to load test into the Test Panel (top center). Now run the test by clicking the "Selected" button in the control area

Alternatively, click the "All" button to run the entire test suite

The Control Panel allows you to control how tests are executed

Selenium-Functional Testing Tool

Run Test Case (continued)

Page 28: Selenium Primer

© Copyright 2007 Sapient Corporation 28TestDirector -Test Management ToolSelenium-Functional Testing Tool

Generating Defect ReportsClick Show Log

Execute the script

Page 29: Selenium Primer

© Copyright 2007 Sapient Corporation 29TestDirector -Test Management Tool

Commands

A command is what tells Selenium what to do

Selenium commands come in two 'flavors', Actions and Assertions

Actions are commands that generally manipulate the state of the application

Each command call is one line in the test table of the form:

Command Target Value

Selenium-Functional Testing Tool

Page 30: Selenium Primer

© Copyright 2007 Sapient Corporation 30TestDirector -Test Management Tool

Commands (continued)

open( url )Opens a URL in the test frame. This accepts both relative and absolute URLs

The URL must be on the same site as Selenium due to security restrictions in the browser (Cross Site Scripting)

examples: Open /mypage open http://localhost/ 

click( elementLocator )Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), use "clickAndWait".

examples: Clicka Checkbox  clickAndWait submitButton clickAndWait anyLink 

Selenium-Functional Testing Tool

Page 31: Selenium Primer

© Copyright 2007 Sapient Corporation 31TestDirector -Test Management Tool

Commands (continued)

type( inputLocator, value )Sets the value of an input field, as though you typed it inCan also be used to set the value of combo boxes, check boxes, etc

examples: Type nameField John Smith typeAndWait textBoxThatSubmitsOnChange newValue 

select( dropDownLocator, optionSpecifier )Select an option from a drop-down, based on the optionSpecifierIf more than one option matches the specifier (e.g. due to the use of globs like "f*b*", or due to more than one option having the same label or value), then the first matches is selected

examples: Select dropDown Australian Dollars Select dropDown index=0 selectAndWait currencySelector value=AUD selectAndWait currencySelector label=Aus*lian D*rs

Selenium-Functional Testing Tool

Page 32: Selenium Primer

© Copyright 2007 Sapient Corporation 32TestDirector -Test Management Tool

Commands (continued)

selectWindow( windowId )Selects a popup windowOnce a popup window has been selected, all commands go to that windowTo select the main window again, use "null" as the targettarget: The id of the window to selectvalue: ignored

examples: selectWindow myPopupWindow selectWindow null 

close()Simulates the user clicking the "close" button in the titlebar of a popup window

examples: close  

pause( milliseconds )Pauses the execution of the test script for a specified amount of timeThis is useful for debugging a script or pausing to wait for some server side action

examples: Pause 5000 Pause 2000 

For More commands go to http://release.openqa.org/selenium-core/nightly/reference.html

Selenium-Functional Testing Tool

Page 33: Selenium Primer

© Copyright 2007 Sapient Corporation 33TestDirector -Test Management Tool

DemosDemo 1 – Passing simple tests

Click http://www.openqa.org/selenium-core/selenium/TestRunner.html?test=../ajaxdemo/TestSuite.html

Demo 2 – Failing Some Tests

Click http://www.openqa.org/selenium-core/selenium/TestRunner.html?test=../demo/failing/TestSuite.html

Demo 3 - Ajax Testing

Click http://www.openqa.org/selenium-core/selenium/TestRunner.html?test=../ajaxdemo/TestSuite.html

Selenium-Functional Testing Tool

Page 34: Selenium Primer

© Copyright 2007 Sapient Corporation 34TestDirector -Test Management Tool

Selenium – Key FAQ’s

How do you create test tables?

Selenium-Functional Testing Tool

Page 35: Selenium Primer

© Copyright 2007 Sapient Corporation 35TestDirector -Test Management Tool

Key FAQ’s

How do you create test tables?

The developers on the Selenium project use Mozilla Composer to create plain HTML text files for their tests. By default, Mozilla Composer writes very clean HTML without any extra, unnecessary markup

You can also use Selenium IDE to write your tests scripts

Another option is any text/html editor to create test tables. Future versions of Selenium may support RST (Restructured Text), or wiki-table syntax, natively. However, you are free to use another format now, as long as you remember to generate the HTML files from your source files, either during your build process or dynamically at run-time

Selenium-Functional Testing Tool

Page 36: Selenium Primer

© Copyright 2007 Sapient Corporation 36TestDirector -Test Management Tool

References & Contacts

References :

http://www.openqa.org/selenium

Contacts:

Sapient SME: Preeti Kakkar [email protected] +91-9811550015

QA Practice Lead: Gurmeet Singh [email protected] 240 353 1762

Selenium-Functional Testing Tool

Page 37: Selenium Primer

© Copyright 2007 Sapient Corporation 37TestDirector -Test Management Tool

What else?

Questions

Next steps

Feedback

Selenium-Functional Testing Tool