Sie sind auf Seite 1von 21

Automated Web Application Testing with Selenium

September 12, 2006

BEA Confidential. | 1

Outline
What is Selenium? Writing Maintainable Selenium Tests Writing Testable Web Applications

BEA Confidential. | 2

What is Selenium?
Open source test tool for web applications
http://selenium.openqa.org

Runs in any mainstream browser


IE, Mozilla/Firefox, Opera, Safari, Konqueror

Write tests in many languages


Java, .NET, Perl, Python, Ruby Selenese (pure HTML, no Java/backend required)

Record/playback (Selenium IDE)

BEA Confidential. | 3

Demo
Record a test in Selenium IDE Demo same test written in Java

BEA Confidential. | 4

Java Test Example


public void testGoogleTestSearch() throws Throwable { selenium.open("http://www.google.com/webhp"); assertEquals("Google", selenium.getTitle()); selenium.type("q", "Selenium OpenQA"); selenium.click("btnG"); selenium.waitForPageToLoad("5000"); assertEquals("Selenium OpenQA - Google Search", selenium.getTitle()); }

BEA Confidential. | 5

Java setUp/tearDown Example


public void setUp() throws Exception { selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.google.com"); selenium.start(); } protected void tearDown() throws Exception { selenium.stop(); }
BEA Confidential. | 6

A Few Selenium Commands


click close createCookie dragdrop fireEvent getCursorPosition getEval getHtmlSource getTitle getValue goBack isTextPresent isVisible keyPress mouseOver open refresh type

BEA Confidential. | 7

Element Locators
selenium.click(____) ID: id=foo Name: name=foo Identifier (ID, then name): identifier=foo DOM: document.forms[myform].myDropdown XPath: //table[@id='table1']//tr[4]/td[2] Link Text: link=text CSS Selector: css=a[href=#id3] Default:
DOM if it starts with document. XPath if it starts with // Identifier otherwise
BEA Confidential. | 8

How It Works

BEA Confidential. | 9

Outline
What is Selenium? Writing Maintainable Selenium Tests Writing Testable Web Applications

BEA Confidential. | 10

Standard End-User Black Box Test


1. Login as administrator 2. Create a user 3. Log out 4. Login as that user 5. Create a folder 6. Create a thingy in that folder 7. Search for that thingy in the search box 8. Make sure your thingy shows up on the search results page

BEA Confidential. | 11

Fragile automated tests


Exercising irrelevant features
Logging in/Logging out Creating a folder Creating a thingy

If the UI for any one of those features changes, your search test fails

BEA Confidential. | 12

Know When to Record a Test


Recorded tests reuse no code Record & Tweak vs. Fire and Forget Slight change in Folder Creator means all of those tests have to be re-recorded from scratch
This happened to us with Mercurys Quick Test Pro

Use the recorder to create reusable code

BEA Confidential. | 13

Unit Testing vs. Integration Testing


Selenium tests are integration tests, not unit tests
Functional/Acceptance/User/Compatibility

Unit tests verify a unit in isolation


If FooTest.java fails, the bug must be in Foo.java A unit test should never fail because of a browser incompatibility Unit tests need to be completely isolated from each other

Integration tests verify that units work well together


Black Box tests Requires testing multiple configurations (browsers) Tend to build on the side-effects of earlier tests

BEA Confidential. | 14

JUnit and TestNG


JUnit is opinionated software JUnit: Everything gets teared down after every test method
Order isolation [see next slide] Constantly starting/stopping browser

Dependencies between tests is explicitly prevented


Separate tests are testing separate units

TestNG: dependsOnX Same test multiple times, different configurations?


Java Properties, multiple runs TestNG parameters

BEA Confidential. | 15

JUnit Test Execution


public void setUp() { log(setup!); } public void testFoo() { log(foo!); } public void testBar() { log(bar!); } public void tearDown() { log(teardown!); } Runs like this
setup! foo! teardown! setup! bar! teardown!

NOT like this


setup! foo! bar! teardown!
BEA Confidential. | 16

Outline
What is Selenium? Writing Maintainable Selenium Tests Writing Testable Web Applications

BEA Confidential. | 17

Testable HTML Pages


Human-legible (hand-coded) element IDs
id=obj3 is going to be fragile XPaths are very fragile JSF: TLD <required>true</required> on ID elements

Hidden flags: input fields, object properties


waitForCondition is your AJAX testing tool

BEA Confidential. | 18

Model View Controller


Model is separately testable
Thats where your business logic is! Write Integration tests of those!

Use Selenium to test your view and controller (navigation) UI Testing API Testing Model Unit Testing Classes

BEA Confidential. | 19

Another Code Reuse Strategy


Prepare your Search tests using Model API tests
FolderBean fb = new FolderBean(); fb.setParent(FolderBean.ROOT); fb.setName(foo); fb.createNewFolder(); adds a folder to the DB selenium.open(/search.faces); selenium.type(query, foo); selenium.click(search); assertTrue(selenium.isTextPresent(foo found);

Writing your tests/webapp in same language

BEA Confidential. | 20

Self-testing web applications


Create a special JSP that runs Selenium tests
Configure Session-scoped beans

BEA Confidential. | 21

Das könnte Ihnen auch gefallen