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

Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Apr 06, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Testing AJAX Applications

with SeleniumPatrick Lightbody

Gomez, Inc.

Page 2: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Agenda

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

• Learn to speak Selenese

• Testing AJAX

• Selenium Remote Control

• Tips & Tricks

Page 3: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• Browser fragmentation

• Application fragmentation

• Functionality impact

• Performance impact

A Growing Problem

Page 5: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

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

Crash Course

DEMO

Page 7: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all
Page 8: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all
Page 9: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Getting Fluent in Selenese

Page 10: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Action Examples

Action Target Value

check some_checkbox

open google.com

type username fred_flinstone

Page 13: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Accessor Examples

Action Target Value

verifyValue username fred_flintstone

waitForElementPresent some_div

assertVisible error_box

Page 15: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Testing AJAX

Page 20: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Scriptaculous

• Autocompleter

• use waitForVisible to pause until the options appear

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

DEMO

Page 24: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Selenium Remote Control

Page 26: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all
Page 28: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Tips & Tricks

Page 31: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• //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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Embrace Loose Coupling

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

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

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

• link=Login

Page 36: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Embrace Loose Coupling

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

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

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

• link=LoginGOOD!

Page 37: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

• 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: Testing AJAX Applications with Seleniumajaxexperience.techtarget.com/assets/documents/...TestingwithSele… · Selenium IDE Tips • When in doubt, try in Selenium IDE • Use all

Questions?You can also email me at

[email protected]

if you have additional questions.