automated web application testing with selenium
Post on 19-Jan-2015
6.777 Views
Preview:
DESCRIPTION
TRANSCRIPT
BEA Confidential. | 1
Automated Web Application Testing with Selenium
September 12, 2006
BEA Confidential. | 2
Outline
What is Selenium?
Writing Maintainable Selenium Tests
Writing Testable Web Applications
BEA Confidential. | 3
What is Selenium?
Open source test tool for web applicationshttp://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. | 4
Demo
Record a test in Selenium IDE
Demo same test written in Java
BEA Confidential. | 5
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. | 6
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. | 7
A Few Selenium Commands
typeisTextPresentgetCursorPosition
refreshgoBackfireEvent
opengetValuedragdrop
mouseOvergetTitlecreateCookie
keyPressgetHtmlSourceclose
isVisiblegetEvalclick
BEA Confidential. | 8
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. | 9
How It Works
BEA Confidential. | 10
Outline
What is Selenium?
Writing Maintainable Selenium Tests
Writing Testable Web Applications
BEA Confidential. | 11
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. | 12
Fragile automated tests
Exercising irrelevant featuresLogging 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. | 13
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 Mercury’s Quick Test Pro
Use the recorder to create reusable code
BEA Confidential. | 14
Unit Testing vs. Integration Testing
Selenium tests are integration tests, not unit testsFunctional/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. | 15
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. | 16
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 thissetup! foo! bar! teardown!
BEA Confidential. | 17
Outline
What is Selenium?
Writing Maintainable Selenium Tests
Writing Testable Web Applications
BEA Confidential. | 18
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. | 19
Model View Controller
Model is separately testable
That’s 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. | 20
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 DBselenium.open(“/search.faces”);selenium.type(“query”, “foo”);selenium.click(“search”);assertTrue(selenium.isTextPresent(“foo found”);
Writing your tests/webapp in same language
BEA Confidential. | 21
Self-testing web applications
Create a special JSP that runs Selenium testsConfigure Session-scoped beans
top related