Selenium and The Grinder


Published on

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....

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Title slide
  • General slide (not title slide)
  • Selenium and The Grinder

    1. 1. Selenium and Grinder: A powerful duo for load testing Eric Pugh Principle, OpenSource Connections [email_address] CvilleJUG March 21st, 2006
    2. 2. What we’re going to learn! <ul><li>Just enough about Selenium to get started! </li></ul><ul><li>Load testing with Grinder </li></ul><ul><li>Whether WEBrick or lighttpd is faster for serving pages! </li></ul>
    3. 3. What is Selenium?
    4. 4. <ul><li>From Selenium’s web site: </li></ul><ul><ul><li>“ Selenium is a test tool for web applications. Selenium tests run directly in a browser , just as real users do.” </li></ul></ul>
    5. 5. Selenium can be run two ways: “TestRunner” and “Driven”
    6. 6. “TestRunner” Mode <ul><li>Selenium application consisting of HTML and JavaScript is deployed along side the application to be tested. </li></ul><ul><li>Tests are written in HTML tables. </li></ul>
    7. 7. TestRunner control panel
    8. 8. “Driven” Mode <ul><li>Browser is under the control of another process that specifies what commands to be run </li></ul>
    9. 9. “Driven” Mode <ul><li>Controlling process is typically a Java, .NET, Ruby or Python application </li></ul>// Java public void testRugbyHome() {;http://localhost:3000/sprint&quot;);;submit&quot;); selenium.assertTitle(&quot;Rugby&quot;);;link=Name&quot;); selenium.assertTitle(&quot;Rugby&quot;); } # Ruby def testRugbyHome puts &quot;http://localhost:3000/sprint&quot; puts selenium.clickAndWait &quot;submit&quot; puts selenium.assertTitle &quot;Rugby&quot; puts selenium.clickAndWait &quot;link=Name&quot; puts selenium.assertTitle &quot;Rugby&quot; end # Python'http://localhost:3000/sprint') print selenium.clickAndWait('submit') print selenium.assertTitle('Rugby') print selenium.clickAndWait('submit') print selenium.assertTitle('Rugby')
    10. 10. “Driven” Mode <ul><li>Pros: </li></ul><ul><ul><li>Better if you need conditional logic or looping </li></ul></ul><ul><ul><li>“ White Box” testing. Need in depth knowledge of webapp structure </li></ul></ul><ul><ul><li>Test writers prefer programming in a “real” language </li></ul></ul><ul><li>Cons: </li></ul><ul><ul><li>Significantly more infrastructure to setup </li></ul></ul><ul><ul><li>Test writers need programming skills </li></ul></ul><ul><ul><li>Tests that require conditional logic can become brittle quickly </li></ul></ul>FYI: “Driven” will be renamed “Remote Control” in the next version of Selenium
    11. 11. We’ll focus on “TestRunner” mode!
    12. 12. Intro to Selenese <ul><li>Selenese is a simple language for specifying commands instructing Selenium what to do </li></ul><ul><li>Consists of three parts: command, target, and value laid out as columns in a an HTML table: </li></ul><table> <tr> <td>command</td><td>target</td><td>value</td> </tr> </table>
    13. 13. Selenese Commands <ul><li>Commands are either “Actions” or “Checks” </li></ul><ul><li>Actions are commands that tell Selenium to do something: </li></ul><ul><ul><li>Open a web page </li></ul></ul><ul><ul><li>Click a link </li></ul></ul><ul><ul><li>Select an option </li></ul></ul><ul><ul><li>Type some text </li></ul></ul><ul><li>Checks validate the applications: </li></ul><ul><ul><li>Title of the page </li></ul></ul><ul><ul><li>Verify that a checkbox is checked </li></ul></ul><ul><ul><li>Ensure a table structure contains a specific string </li></ul></ul>
    14. 14. Selenese Element Locators <ul><li>Element Locators specify an HTML element that a command applies to. </li></ul><ul><li>Multiple strategies for finding an element: </li></ul>id= id Select the element with the specified @id attribute. name= name Select the first element with the specified @name attribute. identifier= id Select 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 .
    15. 15. Selenese Element Locator using DOM <ul><li>Selenium supports using JavaScript to traverse the HTML Document Object Model </li></ul><ul><li>DOM locators must begin with “document.” </li></ul>• dom=document.forms['myForm'].myDropdown • dom=document.images[56]
    16. 16. Selenese Element Locator using XPath <ul><li>XPath expressions are the most powerful way of selecting elements in Selenium. </li></ul><ul><li>XPath locators must begin with “//” </li></ul>• 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…
    17. 17. Common Actions: open <ul><li>open(url) </li></ul><ul><li>Opens both relative and absolute URL’s. </li></ul>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.
    18. 18. Common Actions: click/clickAndWait <ul><li>clickAndWait(elementLocator) </li></ul><ul><li>Click on a link, button, checkbox, or radio button. </li></ul><ul><li>If the click action loads a new page then use clickAndWait to tell Selenium to pause until the new page loads </li></ul>
    19. 19. Common Actions: type <ul><li>type/typeAndWait(elementLocator,value) </li></ul><ul><li>Sets the value of an input field, as though you typed it in. </li></ul>
    20. 20. Common Actions: select <ul><li>select/selectAndWait(elementLocator, optionSpecifier) </li></ul><ul><li>Select an option from a drop down based on the optionSpecifier </li></ul>
    21. 21. Select Option Specifiers <ul><li>Selenium supports multiple ways of specifying options of an HTML select statement. </li></ul><ul><li>If multiple options match then the first one is selected: </li></ul><ul><ul><ul><li>label= labelPattern </li></ul></ul></ul><ul><ul><ul><li>matches options based on their labels, i.e. the visible text. </li></ul></ul></ul><ul><ul><ul><li>• label=regexp:^[Oo]ther </li></ul></ul></ul><ul><ul><ul><li>value= valuePattern </li></ul></ul></ul><ul><ul><ul><li>matches options based on their values. </li></ul></ul></ul><ul><ul><ul><li>• value=other </li></ul></ul></ul><ul><ul><ul><li>id= id </li></ul></ul></ul><ul><ul><ul><li>matches options based on their ids. </li></ul></ul></ul><ul><ul><ul><li>• id=option1 </li></ul></ul></ul><ul><ul><ul><li>index= index </li></ul></ul></ul><ul><ul><ul><li>matches an option based on its index (offset from zero). </li></ul></ul></ul><ul><ul><ul><li>• index=2 </li></ul></ul></ul>
    22. 22. Check Commands <ul><li>Used to verify the state of the application </li></ul><ul><li>Can check the value of a form field, presence of some text, the structure of the HTML, or the URL of the page. </li></ul><ul><li>All checks can be used in two modes: “assert” or “verify” </li></ul><ul><li>Assert failures cause the test to be aborted </li></ul><ul><li>Verify failures are recorded but the test continues </li></ul>Tip: A good practice is to Assert that you are on the correct page and then Verify the contents of the page.
    23. 23. String Matching with Check Commands <ul><li>Selenium supports various pattern syntaxes for matching string values: </li></ul>glob: pattern Match a string against a &quot;glob&quot; pattern. &quot;Glob&quot; is a kind of limited regular-expression syntax typically used in command-line shells. In a glob pattern, &quot;*&quot; represents any sequence of characters, and &quot;?&quot; 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.
    24. 24. Common Checks: assertTitle <ul><li>assertTitle(titlePattern) </li></ul><ul><li>Verify the title of the current page </li></ul>
    25. 25. Common Checks: assertValue <ul><li>assertValue(elementLocator, valuePattern) </li></ul><ul><li>Verify the value of an input field (or anything with a value parameter) </li></ul><ul><li>For checkbox/radio elements the value will be “on” or “off” depending on if it is checked or not </li></ul>
    26. 26. Common Checks: assertAttribute <ul><li>assertAttribute(elementLocator@attributeName, valuePattern) </li></ul><ul><li>Verify the value of an element attribute. </li></ul><ul><li>Great for testing CSS style sheet tags! </li></ul>
    27. 27. Common Checks: assertTextPresent <ul><li>assertTextPresent(text) </li></ul><ul><li>Verify that some text shows up somewhere in the HTML. </li></ul><ul><li>The inverse is of course assertTextNotPresent(text)! </li></ul>
    28. 28. There are many more commands, but that’s enough to get started!
    29. 29. Isn’t there an easier way then writing the HTML by hand?
    30. 30. Selenium IDE <ul><li>Firefox Plugin, requires Firefox 1.5 </li></ul><ul><li>Available from </li></ul><ul><li>More then just a recorder, it’s also a great editor for your tests. </li></ul>Tip: Quickly build the skeleton of your test by using the recorder, then go back and add your verify checks manually.
    31. 31. Now how do we do load testing?
    32. 32. The Grinder <ul><li>Java based load testing framework. Requires J2SE 1.3. </li></ul><ul><li>Licensed under a BSD-style open source license. </li></ul><ul><li>Support for testing HTTP based services as well as others such as SMTP, FTP, and JDBC. </li></ul><ul><li>Comes with a plugin to facilitate recording HTTP test scripts in Jython. </li></ul><ul><li>“ The Grinder 3” is the recommended version to use </li></ul><ul><li> </li></ul>FYI: the official name is “The Grinder” not “Grinder”!
    33. 33. What you need to know <ul><li>You don’t need to know either Java or Python! </li></ul><ul><li>Basic shell/batch scripting to get tool started </li></ul><ul><ul><li>Configuring path statement and environmental variables </li></ul></ul>
    34. 34. The Grinder Design <ul><li>Goal: Minimize system resource requirements while maximizing the number of test contexts (“virtual users”) </li></ul><ul><li>Multi threaded, multi process implementation. </li></ul><ul><ul><li>Each test contexts runs in it’s own thread. </li></ul></ul><ul><ul><li>Threads can be split over many separate processes depending on the capabilities of the agent machine </li></ul></ul><ul><li>Distributed </li></ul><ul><ul><li>Very simple to set up multiple agents and coordinate and monitor their activity </li></ul></ul><ul><ul><li>Console app can centrally manage tests and deploy them to the agents. Console starts and stops the agents. </li></ul></ul>
    35. 35. Generating Grinder script from Selenium Test <ul><li>1) Setup Firefox to use as proxy “localhost:8001” </li></ul><ul><li>2) Start up Grinder’s TCP Proxy </li></ul><ul><li>3) Replay script using Selenium IDE </li></ul><ul><li>Note: Make sure to turn off caching in Firefox so you get all the requests recorded! </li></ul><ul><li>Make sure in Firefox’s Proxy page localhost is set to proxy! </li></ul><ul><li>Use Proxy filter to control what gets recorded! </li></ul>
    36. 36. Getting Grinder Installed <ul><li>One controller and two agents </li></ul>
    37. 37. You’ve got Questions? I’ve got Answers! Email me at epugh@opensourceconnections!