web testing with selenium and junit

16
WEB TESTING WITH SELENIUM AND JUNIT David Ross Drake - SDET

Upload: quon-cash

Post on 03-Jan-2016

136 views

Category:

Documents


1 download

DESCRIPTION

WEB TESTING WITH SELENIUM AND JUNIT. David Ross Drake - SDET. 06/21/11. 2. What is Selenium?. Selenium Portable software testing framework for web applications Selenium IDE Selenium Client Drivers Selenium RC Selenium Grid. 06/21/11. 3. Why use Selenium?. Why automated testing? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: WEB TESTING WITH SELENIUM AND JUNIT

WEB TESTING WITH SELENIUM AND JUNITDavid Ross Drake - SDET

Page 2: WEB TESTING WITH SELENIUM AND JUNIT

What is Selenium?

Selenium – Portable software testing framework for web

applications

• Selenium IDE • Selenium Client Drivers• Selenium RC• Selenium Grid

06/21/11 2

Page 3: WEB TESTING WITH SELENIUM AND JUNIT

Why use Selenium?

Why automated testing?• Speed• Repeatability• Reliability• Cost• Requalification (security patches)• Verisimilitude (vs HTMLUnit)

06/21/11 3

Page 4: WEB TESTING WITH SELENIUM AND JUNIT

DEMONSTRATION: SELENIUM IDERecording and playing back a test with Selenium IDE

06/21/11 4

Page 5: WEB TESTING WITH SELENIUM AND JUNIT

Selenium Locators

• id – fastest method• name• link• dom• css• xpath – slowest method

06/21/11 5

Page 6: WEB TESTING WITH SELENIUM AND JUNIT

DEMONSTRATION: EXPORTING TESTS

06/21/11 6

Exporting tests from Selenium IDE into JUnit and running using Selenium RC

Page 7: WEB TESTING WITH SELENIUM AND JUNIT

Selenium RC

06/21/11 7

Image: www.seleniumhq.org

Page 8: WEB TESTING WITH SELENIUM AND JUNIT

Selenium Challenges• Web app latency can cause tests to fail waiting on

page events• Tests tend to lock in web pages, slowing iterative

development• Selenium core is JavaScript based

• Not all browsers interpret the same way• Can't perform cross-domain testing• Testing secure connections is tricky, and often browser

specific• Execution is slow (compared to most unit tests)• Formatting problems are untested

06/21/11 8

Page 9: WEB TESTING WITH SELENIUM AND JUNIT

Handling Event LatencyExample: An event is triggered on the page when a user enters invalid data.

public static void waitForPageValidation(DefaultSelenium selenium, String id) throws InterruptedException {

for (int second = 0;; second++) { if (second >= 5) {

fail("timeout waiting for event); } try { if (selenium.isVisible(id)) { break; } } catch (Exception e) {} Thread.sleep(1000); } }

06/21/11 9

Page 10: WEB TESTING WITH SELENIUM AND JUNIT

Managing Change

Separate page content used in tests from actual test logic. Most frequently used content:

• Page titles• Field locators (ids, names)

06/21/11 10

Page 11: WEB TESTING WITH SELENIUM AND JUNIT

Selenium 2

06/21/11 11

Advantages Disadvantages

No Selenium RC installation No final version released (summer 2011)

Cross-domain testing capabilities

Limited backwards compatibility

Object-oriented API Smaller number of supported browsers

Mobile browser testing

Page 12: WEB TESTING WITH SELENIUM AND JUNIT

Speeding Up Selenium

• Avoid xpath; have id attributes for all tested fields• Reuse browser sessions (although this can cause

subsequent tests to fail if the session fails)• Test local builds to reduce network latency• Avoid using multi-page test flows for atomic tests• Use parameterized tests to test multiple browsers at

once• Parallelize tests to run multiple test classes at once• Use cloud-based testing services

06/21/11 12

Page 13: WEB TESTING WITH SELENIUM AND JUNIT

Testing Tools in Sauce Labs

• Sauce Scout – Manual testing using dozens of os/browser/version combinations

• Sauce OnDemand – Automated cloud-based testing using a drop in replacement for selenium RC

• Sauce Connect – Tunneling utility for connecting to an application under test behind a firewall

• Sauce Builder – Selenium IDE modified to run tests using Sauce OnDemand

06/21/11 13

Page 14: WEB TESTING WITH SELENIUM AND JUNIT

Cloud-based testing in Sauce Labs

06/21/11 14

Image: www.saucelabs.com

Page 15: WEB TESTING WITH SELENIUM AND JUNIT

DEMONSTRATION: SAUCE LABSRunning tests using Sauce Labs and viewing the results

06/21/11 15

Page 16: WEB TESTING WITH SELENIUM AND JUNIT

Additional Resources

http://www.seleniumhq.com - selenium docs and downloads

http://www.saucelabs.com - cloud-based selenium testing.

http://cargo.codehaus.org - thin wrapper for java containers

http://ce-games.blogger.com - links to this presentation

06/21/11 16