32
Copyright GlobalLogic 2010 1 Connect. Collaborate. Innovate. Web Application Testing using Selenium Amit Saxena 17/08/2011

GL_Web application testing using selenium

Embed Size (px)

Citation preview

Page 1: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 1

Connect. Collaborate. Innovate.

Web Application Testing using Selenium

Amit Saxena17/08/2011

Page 2: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 2

Connect. Collaborate. Innovate.

Training Agenda: Selenium Introduction Selenium Components Recording & Testing with Selenium IDE Exercise 1 – Selenium IDE Testing using Selenium RC Exercise 2 – Selenium RC Parallel testing using Selenium Grid

Web Application Testing using Selenium

Page 3: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 3

Connect. Collaborate. Innovate.Selenium – What is it?

• Selenium is a set of tools to automate web application testing across many platforms.

• Selenium runs in many browsers and operating systems.• Selenium can be controlled by many programming languages and

testing frameworks.• Selenium is highly flexible.• Selenium is an Open Source project. The code can be modified and

enhancements can be submitted for contribution.

Page 4: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 4

Connect. Collaborate. Innovate.Selenium Components

Selenium IDE - is a Firefox add-on that records clicks, typing, and other actions to make a test, which you can play back in the browser.

Selenium RC - runs your tests in multiple browsers and platforms. Tweak/Customize your tests in your preferred programming language.

Selenium Grid - extends Selenium RC to distribute your tests across multiple servers, saving you time by running tests in parallel.

Page 5: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 5

Connect. Collaborate. Innovate.Selenium Components…

Page 6: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 6

Connect. Collaborate. Innovate.Platforms Supported by Selenium

Page 7: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 7

Connect. Collaborate. Innovate.Selenium Commands – Selenese

Actions are commands that generally manipulate the state of the application. They do things like “click this link” and “select that option”. If an Action fails, or has an error, the execution of the current test is stopped.

Accessors examine the state of the application and store the results in variables, e.g. “storeTitle”. They are also used to automatically generate Assertions.

Assertions are like Accessors, but they verify that the state of the application conforms to what is expected. Examples include “make sure the page title is X” and “verify that this checkbox is checked”.

Selenium commands come in three “flavors”: Actions, Accessors and Assertions.

All Selenium Assertions can be used in 3 modes:

Assert – Upon failure test is abortedVerify – Upon failure error is logged and test continuesWaitFor – Waits for a conditions for a given timeout

Page 8: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 8

Connect. Collaborate. Innovate.Selenium Scripts Syntax

Selenium commands are simple, they consist of the command and two parameters. For example:

type id=phone (555) 666-7066

Parameters vary, however they are typically:a locator for identifying a UI element within a page.a text pattern for verifying or asserting expected page contenta text pattern or a selenium variable for entering text in an input field or for selecting an option from an option list.

Page 9: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 9

Connect. Collaborate. Innovate.Commonly Used Selenium Commands

openopens a page using a URL.

click/clickAndWaitperforms a click operation, and optionally waits for a new page to load.

verifyTitle/assertTitleverifies an expected page title.

verifyTextPresentverifies expected text is somewhere on the page.

verifyElementPresentverifies an expected UI element, as defined by its HTML tag, is present on the page.

verifyTextverifies expected text and it’s corresponding HTML tag are present on the page.

verifyTableverifies a table’s expected contents.

waitForPageToLoadpauses execution until an expected new page loads. Called automatically when clickAndWait is used.

waitForElementPresentpauses execution until an expected UI element, as defined by its HTML tag, is present on the page.

Page 10: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 10

Connect. Collaborate. Innovate.Locating Elements

For many Selenium commands, a target is required. This target identifies an element in the content of the web application, and consists of the location strategy. Some of the location strategies using in Selenium are:

Locating by IdentifierThis is probably the most common method of locating elements and is the catch-all default when no recognized locator type is used. With this strategy, the first element with the id attribute value matching the location will be used. If no element has a matching id attribute, then the first element with a name attribute matching the location will be used.

Locating by IdThis type of locator is more limited than the identifier locator type, but also more explicit. Use this when you know an element’s id attribute.Locating by NameThe name locator type will locate the first element with a matching name attribute.

Locating by XPathXPath is the language used for locating nodes in an XML document. As HTML can be an implementation of XML (XHTML), Selenium users can leverage this powerful language to target elements in their web applications. One of the main reasons for using XPath is when you don’t have a suitable id or name attribute for the element you wish to locate. You can use XPath to either locate the element in absolute terms (not advised), or relative to an element that does have an id or name attribute.

Page 11: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 11

Connect. Collaborate. Innovate.Selenium IDE

Selenium IDE is an integrated development environment for Selenium scripts. It is implemented as a Firefox extension, and allows you to record, edit, and debug tests.

Features:• Easy record and playback• Intelligent field selection will use IDs, names, or XPath as needed• Autocomplete for all common Selenium commands• Walk through tests• Debug and set breakpoints• Save tests as Html, Java, .NET, Php, Perl, Ruby scripts etc.• Support for Selenium user-extensions.js file

Page 12: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 12

Connect. Collaborate. Innovate.Selenium IDE Installation

Selenium IDE is installed as a Firefox extension. It can be installed directly within Firefox by going to http://seleniumhq.org/download/ and downloading http://release.seleniumhq.org/selenium-ide/1.0.7/selenium-ide-1.0.7.xpi (XPIs are the Firefox Extensions)

Page 13: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 13

Connect. Collaborate. Innovate.Opening Selenium IDE

Selenium IDE is opened from the Tools menu of Firefox.

Page 14: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 14

Connect. Collaborate. Innovate.Selenium IDE Features

Menu Bar

Toolbar

Test Case Pane

Log/Reference Pane

Test Case Pane

Page 15: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 15

Connect. Collaborate. Innovate.

Speed Control: controls how fast your test case runs.

Run All: Runs the entire test suite when a test suite with multiple test cases is loaded.

Run: Runs the currently selected test. When only a single test is loaded this button and the Run All button have the same effect.

Pause/Resume: Allows stopping and re-starting of a running test case.

Record: Records the user’s browser actions.

Selenium IDE Basic Toolbar Commands

Step: Allows you to “step” through a test case by running it one command at a time. Use for debugging test cases.

Page 16: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 16

Connect. Collaborate. Innovate.Building Test Cases using IDE

Recording

During recording, Selenium-IDE will automatically insert commands into your test case based on your actions. Typically, this will include:• clicking a link - click or clickAndWait commands• entering values - type command• selecting options from a drop-down listbox - select

command• clicking checkboxes or radio buttons - click command

When Selenium-IDE is first opened, the record button is ON by default.

Adding Verifications and Asserts With the Context Menu

With Selenium-IDE recording, go to the browser displaying your test application and right click anywhere on the page. You will see a context menu showing verify and/or assert commands.

Your test cases will also need to check the properties of a web-page. This requires assert and verify commands.

Insert Commands in Table View or Source ViewCommands can also be inserted in the Table view and Source View manually.

Page 17: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 17

Connect. Collaborate. Innovate.Exercise 1 – Selenium IDE

1. Install Selenium IDE2. Record & Save the following test cases in Selenium IDE:

Open Google.com. Search GlobalLogic in Google. Click GlobalLogic link in search result. GlobalLogic homepage should open up.

Open GlobalLogic.com. Click About Us. Verify that the About Us page opens up.

Open GlobalLogic.com. Click Careers Page. Go to current openings section. Conduct Search. Verify that the search results show up.

3. Execute the above test cases individually in Selenium IDE.4. Execute the above created test cases as a test suite in Selenium

IDE.

Page 18: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 18

Connect. Collaborate. Innovate.Selenium RC

Selenium-RC is the solution for tests that need more than simple browser actions and linear execution. Selenium-RC uses the full power of programming languages to create more complex tests like reading and writing files, querying a database, and emailing test results.

You’ll want to use Selenium-RC whenever your test requires logic not supported by Selenium-IDE. What logic could this be? For example, Selenium-IDE does not directly support:

condition statements iteration logging and reporting of test results error handling, particularly unexpected errors database testing test case grouping re-execution of failed tests test case dependency screenshot capture of test failures

Page 19: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 19

Connect. Collaborate. Innovate.Selenium RC Components

Selenium-RC components are:

The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an HTTP proxy, intercepting and verifying HTTP messages passed between the browser and the AUT (Application Under Test).

Client libraries which provide the interface between each programming language and the Selenium-RC Server.

Page 20: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 20

Connect. Collaborate. Innovate.How Selenium RC Works

Page 21: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 21

Connect. Collaborate. Innovate.Installing Selenium RC Server

• The Selenium Server is written in Java, and requires the Java Runtime Environment (JRE) version 1.5.0 or higher in order to start.

• Go to the http://seleniumhq.org/download/ and download selenium RC (Latest version is 1.0.3) - http://selenium.googlecode.com/files/selenium-remote-control-1.0.3.zip

• Unzip and extract it any directory.

Page 22: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 22

Connect. Collaborate. Innovate.Start the RC Server

• Open command prompt and go to the directory where you have extracted the Selenium RC.

• Run the command – java -jar selenium-server.jar

Page 23: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 23

Connect. Collaborate. Innovate.Using Java Client Driver

• Extract the file selenium-java-client-driver.jar from selenium-remote-control-1.0.3.zip• Open your desired Java IDE (Eclipse, NetBeans, IntelliJ etc.)• Create a new project.• Add the selenium-java-client-driver.jar files to your project as references.• Add to your project classpath the file selenium-java-client-driver.jar.• From Selenium-IDE, export a script to a Java file and include it in your Java project, or write

your Selenium test in Java using the selenium-java-client API. • Run Selenium server from the console.• Execute your test from the Java IDE or from the command-line.

Page 24: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 24

Connect. Collaborate. Innovate.Exercise 2 – Selenium RC

1. Install Selenium RC.2. Create a Java Project in Eclipse and add JUnit and Selenium

Client Driver in project classpath.3. Export Test Cases From Selenium IDE as JUnits.4. Export Test Case Suite From Selenium IDE as JUnit Test Suite. 5. Import the Test Cases in Eclipse.6. Start the Selenium Server.7. Execute the test cases individually from Eclipse as JUnit.8. Execute the test cases as a JUnit Suite from Eclipse.

Page 25: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 25

Connect. Collaborate. Innovate.Selenium Grid

Selenium-Grid allows the Selenium-RC solution to scale for large test suites or test suites that must be run in multiple environments.

With Selenium-Grid, multiple instances of Selenium-RC are running on various operating system and browser configurations; Each of these when launching register with a hub. When tests are sent to the hub they are then redirected to an available Selenium-RC, which will launch the browser and run the test. This allows for running tests in parallel, with the entire test suite theoretically taking only as long to run as the longest individual test.

Page 26: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 26

Connect. Collaborate. Innovate.Typical Selenium Grid Setup

Page 27: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 27

Connect. Collaborate. Innovate.Installing Selenium Grid

• Selenium Grid requires the following to be installed:• JDK 1.5 or above• Apache Ant 1.7 or above

• Go to the http://selenium-grid.seleniumhq.org/download.html and download Selenium Grid (Latest version is 1.0.8) –

http://release.seleniumhq.org/selenium-grid/selenium-grid-1.0.8-bin.zip

• Unzip and extract it any directory.

Page 28: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 28

Connect. Collaborate. Innovate.Starting Selenium Grid

Selenium Grid requires the Hub and RCs to be started :

HubTo start Hub, open command prompt and go to the directory where you have extracted Selenium Grid and type: ant launch-hub

RCsTo start RCs, open command prompt and go to the directory where you have extracted Selenium Grid and type: ant -Denvironment=“*firefox“ -Dport=5555 launch-remote-controlant -Denvironment=“*iexplore” -Dport=5556 launch-remote-controlThe above commands would start RCs on ports 5555 and 5556 for firefox and internet explorer environments.

When having multiple machines for RCs, following parameters should be used to provide the Hub URL and RC host:ant -Dport=<port> -Dhost=<hostname> -DhubURL=<hub url> launch-remote-control

Grid ConsoleOpen http://localhost:4444/console to view the registered RCs and their status.

Page 29: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 29

Connect. Collaborate. Innovate.Exercise 3 – Selenium Grid

1. Install Selenium Grid.2. Start Selenium Grid & RCs.3. Add Parallel JUnit to the classpath in the Eclipse project.4. Export Test Case Suite From Selenium IDE as JUnit Suite.5. Import the Test Case Suite in Eclipse.6. Execute the Test Case suite using Parallel JUnit from Eclipse.7. View the available/active RCs in Grid Console.

Page 30: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 30

Connect. Collaborate. Innovate.References

Selenium Documentationhttp://seleniumhq.org/docs/

Page 31: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 31

Connect. Collaborate. Innovate.Questions

??

Page 32: GL_Web application testing using selenium

© Copyright GlobalLogic 2010 32

Connect. Collaborate. Innovate.

Thank You!

Web Application Testing using Selenium