linskey patrick testingajax selenium

38
Testing AJAX Applications with Selenium Patrick Lightbody Gomez, Inc.

Upload: alexmursa

Post on 25-Oct-2015

4 views

Category:

Documents


0 download

DESCRIPTION

ajax and selenium

TRANSCRIPT

Page 1: Linskey Patrick TestingAjax Selenium

Testing AJAX Applications

with SeleniumPatrick Lightbody

Gomez, Inc.

Page 2: Linskey Patrick TestingAjax Selenium

Agenda

• Four demos: basic Selenium, Google Maps, Dojo, and Scriptaculous

• Learn to speak Selenese

• Testing AJAX

• Selenium Remote Control

• Tips & Tricks

Page 3: Linskey Patrick TestingAjax Selenium

• Audience: Testers? Developers? Managers? Mix?

• Continuous integration: Yes? No? Compile only? Unit tests? Functional tests?

• Toolkit: Home-brewed? Using a framework? Using multiple frameworks?

Quick Poll

Page 4: Linskey Patrick TestingAjax Selenium

• Browser fragmentation

• Application fragmentation

• Functionality impact

• Performance impact

A Growing Problem

Page 5: Linskey Patrick TestingAjax Selenium

• A cross-platform browser automation tool.

• Written primarily in JavaScript.

• Supports tests written in JavaScript, “Selenese”, or just about any programming language.

• Has several sub-projects (Core, IDE, RC, Rails, etc)

• Is part of OpenQA, the home of many other open source QA tools.

About Selenium

Page 6: Linskey Patrick TestingAjax Selenium

• Best way to get started with Selenium is to use it...

Crash Course

DEMO

Page 7: Linskey Patrick TestingAjax Selenium
Page 8: Linskey Patrick TestingAjax Selenium
Page 9: Linskey Patrick TestingAjax Selenium

Getting Fluent in Selenese

Page 10: Linskey Patrick TestingAjax Selenium

• A simple language that is structured like Fit.

• Has three core components

• Actions - the things that actually control the browser

• Accessors - how you work with data in the browser

• Element Locators - how you identify data in the browser

• Has limited support for variables, but no control structure.

Selenese Language

Page 11: Linskey Patrick TestingAjax Selenium

• Are where your command actually does something.

• Most actions typically take one or two arguments: an element locator and possibly a value.

• All actions have an additional “AndWait” sister-action.

Actions

Page 12: Linskey Patrick TestingAjax Selenium

Action Examples

Action Target Value

check some_checkbox

open google.com

type username fred_flinstone

Page 13: Linskey Patrick TestingAjax Selenium

• Are always “data related”.

• Typically take only one argument: an element locator.

• Have seven permutations:

• store (locator, variable)

• verify and verifyNot (locator, pattern)

• assert and assertNot (locator, pattern)

• waitFor and waitForNot (locator, pattern)

Accessors

Page 14: Linskey Patrick TestingAjax Selenium

Accessor Examples

Action Target Value

verifyValue username fred_flintstone

waitForElementPresent some_div

assertVisible error_box

Page 15: Linskey Patrick TestingAjax Selenium

• Go hand-in-hand with actions and accessors

• Have a syntax of:

• [locator_type =] locator_value

• Are the trickiest part of Selenium, but most powerful

Element Locators

Page 16: Linskey Patrick TestingAjax Selenium

Element Locator TypesType Returns an element...

id with the specified IDname with the specified name

identifier with the specified ID or namedom that is returned by the eval’d JSxpath that is represented by the XPathlink of type a and surrounds given textcss that is represented by given selector

Page 17: Linskey Patrick TestingAjax Selenium

• dom, if the locator starts with “document.”

• Example: click document.forms[0].elements[4]

• xpath, if the locator starts with “//”

• Example: verifyElementPresent //img[contains(@src, ‘close.gif ’)]

• identifier, for all others

• Example: click btnG

Default Locator Strategy

Page 18: Linskey Patrick TestingAjax Selenium

• Allows for basic logic in your scripts.

• Use the storeXxx permutation of the accessors:

• storeValue nameField firstname

• storeEval ‘Mr’ title

• assertTextPresent ${title} ${firstName}

• Does not pretend to be full-featured... if you need complex tests, you probably need a more complex language.

Variable Support

Page 19: Linskey Patrick TestingAjax Selenium

Testing AJAX

Page 20: Linskey Patrick TestingAjax Selenium

• The A in AJAX makes testing much more interesting.

• We’ve seen the “AndWait” variations of commands...

• ... but what about when there never is another page load (Google Maps, Yahoo Mail, and at least partly almost every new web app)?

No longer page-centric

DEMO?

Page 21: Linskey Patrick TestingAjax Selenium

• You can test any application written on any AJAX toolkit with Selenium, but...

• Some toolkits make it easier than others.

• You can compensate for the more difficult frameworks by writing your own user extensions.

AJAX Toolkits

Page 22: Linskey Patrick TestingAjax Selenium

Dojo

• Selenium isn’t aware of your widget IDs

• Give your widgets HTML IDs

• If the widget isn’t under your control, there are other options...

DEMO

Page 23: Linskey Patrick TestingAjax Selenium

Scriptaculous

• Autocompleter

• use waitForVisible to pause until the options appear

• keyPress is required to simulate typing in characters (different from type

DEMO

Page 24: Linskey Patrick TestingAjax Selenium

Ext

• Unlike Dojo, the HTML elements all have IDs...

• Unfortunately, they are not predictable (ext-gen245, ext-gen500, etc)

• Result: Don’t use Ext’s IDs - use techniques like you would with Dojo

Page 25: Linskey Patrick TestingAjax Selenium

Selenium Remote Control

Page 26: Linskey Patrick TestingAjax Selenium

• Selenium RC solves three problems:

• Cross-site scripting restrictions are painful in this composite world (ie: Hotmail -> MS Passport).

• Not easy to automate the process of running your tests on many browsers (continuous integration).

• Selenese is a very basic language that offers no reuse and no control structure.

• Selenium IDE only works on Firefox and Selenium Core requires you to modify your AUT.

Advanced Selenium

Page 27: Linskey Patrick TestingAjax Selenium
Page 28: Linskey Patrick TestingAjax Selenium

• Treat like a daemon process (httpd, sendmail, etc).

• java -jar selenium-server.jar

• The client drivers simply issue HTTP GET commands to the server

Getting Started with Selenium RC

Page 29: Linskey Patrick TestingAjax Selenium

Sample HTTP Traffic

cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com

cmd=open&1=/

cmd=type&1=q&2=GOOG

cmd=clickAndWait&1=btnG

cmd=clickAndWait&1=link=Google Finance

cmd=verifyTable&1=//table[@id='fd'].0.3&2=6,138.56

Page 30: Linskey Patrick TestingAjax Selenium

Tips & Tricks

Page 31: Linskey Patrick TestingAjax Selenium

• Give your elements IDs! (Design for testability)

• Make application state easy to reset. Invest in fixtures.

• Use good tools: Firebug, Selenium IDE, XPath Checker.

• Try writing your own tests and feel the pain.

• Be prepared to refactor tests just as often as you do with code.

Development Tips

Page 32: Linskey Patrick TestingAjax Selenium

Selenium IDE Tips

• When in doubt, try in Selenium IDE

• Use all the features of Selenium IDE:

• autocomplete helps you learn the commands

• Logs tab help you debug issues and get help in the forums

• Reference tab documents every single command

• Find button helps you determine if your locator is correct

Page 33: Linskey Patrick TestingAjax Selenium

• //table[3]/tbody/tr[1]/td[4]

• session_199238237132_search_results

• //img[@src = ‘http://staging.acme.com/images/logo.png’]

• //a[@href = ‘http://staging.acme.com/login’]

Avoid Tight Coupling

Page 35: Linskey Patrick TestingAjax Selenium

Embrace Loose Coupling

• //td[text() = ‘ISBN XYZ’]

• //div[contains(@id, ‘_search_results’)]

• //img[contains(@src, ‘logo.png’)]

• link=Login

Page 36: Linskey Patrick TestingAjax Selenium

Embrace Loose Coupling

• //td[text() = ‘ISBN XYZ’]

• //div[contains(@id, ‘_search_results’)]

• //img[contains(@src, ‘logo.png’)]

• link=LoginGOOD!

Page 37: Linskey Patrick TestingAjax Selenium

• Reality QA: http://www.realityqa.com

• RealityCheck - run your Selenium scripts on any browser/OS

• RealityView - check out your site design on any browser/OS

• Takes screenshots and a movie of each step along the way.

• Supports advanced test refactoring and analysis.

Commercial Options

Page 38: Linskey Patrick TestingAjax Selenium

Questions?You can also email me at

[email protected]

if you have additional questions.