advanced selenium

NEAL FORD software architect / meme wrangler

ThoughtWorks 3003 Summit Boulevard, Atlanta, GA 30319 blog: twitter: neal4d


Questions, Slides, and Samples
Please feel free to ask questions anytime The slides and samples will be available at I’ll show that address again at the end Samples denoted via => document_name

What This Session Covers
Selenium’s scope TestRunner techniques Remote Control Ajax IDE Extensions Best practices

Selenium’s Scope Selenium is an acceptance testing tool for web applications Selenium tests are fragile by design .

Selenium Test Runner Mode Selenium Core Selenium Test Runner Application Under Test .

The Selenium IDE FireFox extension Not just a recorder Intelligent field selection Autocomplete for all common Selenium commands Walk through tests Debug and set breakpoints .

exact:string Match a string exactly.String Matching Patterns glob:pattern Match a string against a "glob" (aka "wildmat") pattern. "*" represents any sequence of characters "?" represents any single character. regexp:regexp Match a string using a regular-expression. without any of that fancy wildcard stuff. The full power of JavaScript regular-expressions is available. verbatim. => End 2 End Test . Glob patterns match against the entire string. Selenium assumes that it's a "glob" pattern. If no pattern prefix is specified.

TestRunner Techniques .

End-to-end Testing Example of end to end testing => End 2 end Test Always make sure you leave your application in a known good state .

getTime()) .Generating Unique Values The problem: you need unique user names Solution: create user names based on time values 'fred ' + (new Date().

Data Driven Tests Test cases (and suites) don’t have to be static HTML .


Verifying # of Rows in a Table Assert that nth row exists and that nth+1 does not assertElementPresent //table[@id='mytable']/tr[10] assertElementNotPresent //table[@id='mytable']/tr[11] Use the assertXPath extension to count columns assertXpath count(//table[@id='mytable']/tr) 10 .

storeValue. and storeText store values for later access Internally. keyed to variable names . Selenium uses a map called storedVars.Parameters and Variables Parameter and variable declarations range from simple values to Javascript evaluation store.

Variable substitution or javascript evaluation storeValue( inputLocator. variableName ) Stores the value of an input field into a variable. .store. variableName ) Stores a value into a variable. storeValue store( valueToStore.

storeAttribute (elementLocator@attributeName. variableName) Stores the value of an element attribute into a variable.storeText. storeAttribute storeText(elementLocator. . variableName ) Stores the text of an element into a variable.

Variable Substitution Provides a simple way to access variables using ${xxx} syntax .

JavaScript Evaluation You can use JavaScript to construct whatever values you want The entire parameter value is prefixed with ‘javascript{‘ with a trailing ‘}’ Text inside the braces is a pure JavaScript expression .

Harvesting Values Variables allow you to harvest information from an “information only” page Steps: Build a page with values On startup of the test. harvest those values Use the variables for assertions in subsequent tests => harvesting values .

Selenium Remote Control .

Selenium Remote Control Selenium Core JUnit Test Proxy Server Application Under Test .

Interactive Selenium Start the proxy Create an instance of the browser The proxy will provide a unique ID that must be used in all subsequent commands You’ll see a separate instance of the browser launch Issue Selenium commands When done. kill the browser instance => Interactive Selenium .

Test Code Reuse One of the advantages of remote control Create “helper” methods for common tasks Login helper => DecisionDemo When designing tests Keep them modular Refactor frequently .

Selenium Remote Control Ruby Use the language that makes sense for the situation .Multiple Language Support The Selenium IDE generates tests in a variety of languages All your tests don’t have to be in the same language Java == Ruby example => Selenium Remote Control.

Decisions TestRunner tests can’t make deciscions The Selenium language was designed to be strictly declarative. not imperative In remote control. IE doesn’t Different roles for different users Remote control gives you imperative tests in Selenium => Decision Demo . you write unit tests Allows you to make testing decisions Firefox makes you logon.

The Selenium IDE More than just a pretty face! Supports multiple languages TestRunner (declarative) Java. (imperative) A round-trip tool… …but only for declarative tests Don’t make the imperative plunge lightly . Ruby. Python. etc. C#.

testing Ajax is easy Testing XmlHttpRequest => Testing collapsable divs => End 2 End Test Testing absence of controls => .Ajax Because Selenium works directly with the DOM.

Extending Selenium Selenium allows you to add your own actions. and locator strategies Selenium uses naming patterns to discover extensions at run-time . checks.

Actions All methods in the form of doFoo are added as actions For each foo. and fooAndWait is created .

Checks All assertFoo methods are added as checks For each foo. you get assertFoo & verifyFoo .

Locator Strategies All locateElementByFoo methods on PageBot are added as locator strategies Locators take 2 parameters Locator string (minus the prefix) Document in which to search Add a "valuerepeated=" locator. that finds the first element a value attribute equal to the the supplied value repeated. .

Custom Locator .

without needing to modify the core Selenium sources. Selenium looks for a file called "user-extensions. A convenient location for adding features to Selenium.js". This file doesn’t exist by default (you’ll have to add it) .User Extensions By default. and loads the javascript code found in that file.

org/display/SEL/Contributed+User-Extensions .Handy User Extensions Alert => Interactive debugger => GUI_Map => Allows you to handle auto-generated fields names more gracefully Include => Check out http://wiki.openqa.

applets.” to JavaScript permissions block you from filling in an input path for a file upload Workaround: Set the browser property “signed.PrivilegeManager.doType Currently only works in Firefox .codebase_principal_support” to true Add “netscape.enablePrivilege(”UniversalFileRead”).Uploading Files Normally.js in function Selenium.

Selenium Best Practices Create modular test methods for remote control Allows for test code reuse Easier to refactor Not as fragile Record acceptance tests End users/business analysts only have to verify it once Regression tests catch errors .

Selenium Best Practices Write selenium tests late in the development cycle User acceptance tests are fragile Require lots of refactoring The danger is that you’ll stop running regression tests if they break too much .

Questions? Please fill out the session evaluations Samples & slides at . NY 10005 This work is licensed under the Creative Commons AttributionNoncommercial-Share Alike 2. Suite 2019.blogspot.5/ www.5 www. New NEAL FORD thoughtworker / meme wrangler ThoughtWorks 14 Wall St. http://creativecommons.

Sign up to vote on this title
UsefulNot useful