37
Selenium and Grinder: A powerful duo for load testing Eric Pugh Principle, OpenSource Connections [email protected] CvilleJUG March 21st, 2006

Selenium and The Grinder

Embed Size (px)

DESCRIPTION

This is a presentation I did back in 2006 about using Selenium and Grinder. Looking at it today (2011) it doesn't focus enough on the Selenium Grinder intersection....

Citation preview

Page 1: Selenium and The Grinder

Selenium and Grinder:A powerful duo for load testing

Eric PughPrinciple, OpenSource Connections

[email protected]

CvilleJUGMarch 21st, 2006

Page 2: Selenium and The Grinder

What we’re going to learn!

• Just enough about Selenium to get started!• Load testing with Grinder• Whether WEBrick or lighttpd is faster for serving

pages!

Page 3: Selenium and The Grinder

What is Selenium?

Page 4: Selenium and The Grinder

• From Selenium’s web site:– “Selenium is a test tool for web applications. Selenium tests run

directly in a browser, just as real users do.”

Page 5: Selenium and The Grinder

Selenium can be run two ways: “TestRunner” and “Driven”

Page 6: Selenium and The Grinder

“TestRunner” Mode

• Selenium application consisting of HTML and JavaScript is deployed along side the application to be tested.

• Tests are written in HTML tables.

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 7: Selenium and The Grinder

TestRunner control panel

Page 8: Selenium and The Grinder

“Driven” Mode

• Browser is under the control of another process that specifies what commands to be run

QuickTime™ and aTIFF (Uncompressed) decompressor

are needed to see this picture.

Page 9: Selenium and The Grinder

“Driven” Mode

• Controlling process is typically a Java, .NET, Ruby or Python application

// Javapublic void testRugbyHome() { selenium.open("http://localhost:3000/sprint"); selenium.click("submit"); selenium.assertTitle("Rugby"); selenium.click("link=Name"); selenium.assertTitle("Rugby");}

# Rubydef testRugbyHome puts selenium.open "http://localhost:3000/sprint" puts selenium.clickAndWait "submit" puts selenium.assertTitle "Rugby" puts selenium.clickAndWait "link=Name" puts selenium.assertTitle "Rugby"end

# Pythonselenium.open('http://localhost:3000/sprint')print selenium.clickAndWait('submit')print selenium.assertTitle('Rugby')print selenium.clickAndWait('submit')print selenium.assertTitle('Rugby')

Page 10: Selenium and The Grinder

“Driven” Mode

• Pros:– Better if you need conditional logic or looping– “White Box” testing. Need in depth knowledge of webapp

structure– Test writers prefer programming in a “real” language

• Cons:– Significantly more infrastructure to setup– Test writers need programming skills– Tests that require conditional logic can become brittle quickly

FYI: “Driven” will be renamed “Remote Control” in the next version of Selenium

Page 11: Selenium and The Grinder

We’ll focus on “TestRunner” mode!

Page 12: Selenium and The Grinder

Intro to Selenese

• Selenese is a simple language for specifying commands instructing Selenium what to do

• Consists of three parts: command, target, and value laid out as columns in a an HTML table:

<table> <tr> <td>command</td><td>target</td><td>value</td> </tr></table>

Page 13: Selenium and The Grinder

Selenese Commands

• Commands are either “Actions” or “Checks”

• Actions are commands that tell Selenium to do something:– Open a web page

– Click a link

– Select an option

– Type some text

• Checks validate the applications:– Title of the page

– Verify that a checkbox is checked

– Ensure a table structure contains a specific string

Page 14: Selenium and The Grinder

Selenese Element Locators

• Element Locators specify an HTML element that a command applies to.

• Multiple strategies for finding an element:

id=idSelect the element with the specified @id attribute.

name=nameSelect the first element with the specified @name attribute.

identifier=idSelect the element with the specified @id attribute. If no match is found,

select the first element whose @name attribute is id.link=textPattern

Select the link (anchor) element which contains text matching the specified pattern.

Page 15: Selenium and The Grinder

Selenese Element Locator using DOM

• Selenium supports using JavaScript to traverse the HTML Document Object Model

• DOM locators must begin with “document.”

•dom=document.forms['myForm'].myDropdown•dom=document.images[56]

Page 16: Selenium and The Grinder

Selenese Element Locator using XPath

• XPath expressions are the most powerful way of selecting elements in Selenium.

• XPath locators must begin with “//”

•xpath=//img[@alt='The image alt text']•xpath=//table[@id='table1']//tr[4]/td[2]•xpath=//div[@id='workflowMenu']//a[@id='goToPreviousStatus']/

Tip: IE’s implementation of XPath is buggy, your mileage may vary…

Page 17: Selenium and The Grinder

Common Actions: open

• open(url)

• Opens both relative and absolute URL’s.

Tip: Selenium can only open URL’s that are on the same site due to “Cross Site Scripting” security policy in browsers. We’ll discuss how to get around this later.

Page 18: Selenium and The Grinder

Common Actions: click/clickAndWait

• clickAndWait(elementLocator)

• Click on a link, button, checkbox, or radio button.

• If the click action loads a new page then use clickAndWait to tell Selenium to pause until the new page loads

Page 19: Selenium and The Grinder

Common Actions: type

• type/typeAndWait(elementLocator,value)

• Sets the value of an input field, as though you typed it in.

Page 20: Selenium and The Grinder

Common Actions: select

• select/selectAndWait(elementLocator, optionSpecifier)

• Select an option from a drop down based on the optionSpecifier

Page 21: Selenium and The Grinder

Select Option Specifiers

• Selenium supports multiple ways of specifying options of an HTML select statement.

• If multiple options match then the first one is selected:

label=labelPatternmatches options based on their labels, i.e. the visible text.•label=regexp:^[Oo]ther

value=valuePatternmatches options based on their values.•value=other

id=idmatches options based on their ids.•id=option1

index=indexmatches an option based on its index (offset from zero).•index=2

Page 22: Selenium and The Grinder

Check Commands

• Used to verify the state of the application

• Can check the value of a form field, presence of some text, the structure of the HTML, or the URL of the page.

• All checks can be used in two modes: “assert” or “verify”

• Assert failures cause the test to be aborted

• Verify failures are recorded but the test continues

Tip: A good practice is to Assert that you are on the correct page and then Verify the contents of the page.

Page 23: Selenium and The Grinder

String Matching with Check Commands

• Selenium supports various pattern syntaxes for matching string values:

glob:patternMatch a string against a "glob" pattern. "Glob" is a kind of limited

regular-expression syntax typically used in command-line shells. In a glob pattern, "*" represents any sequence of characters, and "?" represents any single character. Glob patterns match against the entire string.regexp:regexp

Match a string using a regular-expression. The full power of JavaScript regular-expressions is available.exact:string

Match a string exactly, verbatim, without any of that fancy wildcard stuff.

Page 24: Selenium and The Grinder

Common Checks: assertTitle

• assertTitle(titlePattern)

• Verify the title of the current page

Page 25: Selenium and The Grinder

Common Checks: assertValue

• assertValue(elementLocator, valuePattern)

• Verify the value of an input field (or anything with a value parameter)

• For checkbox/radio elements the value will be “on” or “off” depending on if it is checked or not

Page 26: Selenium and The Grinder

Common Checks: assertAttribute

• assertAttribute(elementLocator@attributeName, valuePattern)

• Verify the value of an element attribute.

• Great for testing CSS style sheet tags!

Page 27: Selenium and The Grinder

Common Checks: assertTextPresent

• assertTextPresent(text)

• Verify that some text shows up somewhere in the HTML.

• The inverse is of course assertTextNotPresent(text)!

Page 28: Selenium and The Grinder

There are many more commands, but that’s enough to get started!

QuickTime™ and aTIFF (Uncompressed) decompressorare needed to see this picture.

Page 29: Selenium and The Grinder

Isn’t there an easier way then writing the HTML by hand?

Page 30: Selenium and The Grinder

Selenium IDE

• Firefox Plugin, requires Firefox 1.5• Available from http://www.openqa.org/selenium-ide/

• More then just a recorder, it’s also a great editor for your tests.

Tip: Quickly build the skeleton of your test by using the recorder, then go back and add your verify checks manually.

Page 31: Selenium and The Grinder

Now how do we do load testing?

Page 32: Selenium and The Grinder

The Grinder

• Java based load testing framework. Requires J2SE 1.3.

• Licensed under a BSD-style open source license.

• Support for testing HTTP based services as well as others such as SMTP, FTP, and JDBC.

• Comes with a plugin to facilitate recording HTTP test scripts in Jython.

• “The Grinder 3” is the recommended version to use

• http://grinder.sourceforge.net/

FYI: the official name is “The Grinder” not “Grinder”!

Page 33: Selenium and The Grinder

What you need to know

• You don’t need to know either Java or Python!

• Basic shell/batch scripting to get tool started– Configuring path statement and environmental variables

Page 34: Selenium and The Grinder

The Grinder Design

• Goal: Minimize system resource requirements while maximizing the number of test contexts (“virtual users”)

• Multi threaded, multi process implementation. – Each test contexts runs in it’s own thread.

– Threads can be split over many separate processes depending on the capabilities of the agent machine

• Distributed– Very simple to set up multiple agents and coordinate and monitor their

activity

– Console app can centrally manage tests and deploy them to the agents. Console starts and stops the agents.

Page 35: Selenium and The Grinder

Generating Grinder script from Selenium Test

• 1) Setup Firefox to use as proxy “localhost:8001”• 2) Start up Grinder’s TCP Proxy• 3) Replay script using Selenium IDE

• Note: Make sure to turn off caching in Firefox so you get all the requests recorded!

• Make sure in Firefox’s Proxy page localhost is set to proxy!

• Use Proxy filter to control what gets recorded!

Page 36: Selenium and The Grinder

Getting Grinder Installed

• One controller and two agents

Page 37: Selenium and The Grinder

You’ve got Questions? I’ve got Answers!

Email me at epugh@opensourceconnections!