Вы находитесь на странице: 1из 8

Table of Contents

Chapter 1:Getting Started With The Tools...........................................................................................9


Introduction......................................................................................................................................9
Section Overview.............................................................................................................................9
What Tools?...................................................................................................................................10
Selenium IDE............................................................................................................................10
Java............................................................................................................................................10
Eclipse.......................................................................................................................................11
Selenium-RC.............................................................................................................................11
Chapter 2:Install and Run Selenium-RC............................................................................................12
Step One - Download & Install Selenium-RC...............................................................................12
Overview of the Contents of the Selenium Archive.................................................................13
Step Two - Run Selenium-RC........................................................................................................14
Step Three – Stop the Selenium Server.........................................................................................15
From the command line............................................................................................................15
From a browser url....................................................................................................................15
Chapter 3:Selenium IDE Basics.........................................................................................................16
Install Firefox.................................................................................................................................16
Install Selenium-IDE.....................................................................................................................16
Capture PlayBack – Recording a Script with the IDE...................................................................17
Command, Target and Value..........................................................................................................20
Save The Test.................................................................................................................................20
Running the Test in Different Browsers........................................................................................20
End Notes.......................................................................................................................................23
Chapter 4:Install Java.........................................................................................................................24
Chapter 5:The Eclipse IDE.................................................................................................................25
First - Install Eclipse......................................................................................................................25
Second - Run Eclipse.....................................................................................................................26
Third - create a new Java project...................................................................................................28
Chapter 6:Create a JUnit test Using the Junit export from Selenium-IDE.........................................31
Introduction....................................................................................................................................31
Export an IDE script as a JUnit test...............................................................................................31
Load an existing script into the IDE.........................................................................................31
Change the format of the script to Java.....................................................................................32
Create a New Class in Eclipse..................................................................................................33
Copy & Paste the code from Selenium IDE into Eclipse.........................................................35
Resolve Import Errors...............................................................................................................37
Add JUnit to the build path.......................................................................................................39
Remove the unused import.......................................................................................................41
Run the JUnit test...........................................................................................................................42
Allow it through the Firewall....................................................................................................42
Seeing the test running..............................................................................................................43
Check the results.......................................................................................................................44
It went too quickly!........................................................................................................................45
Run the test in debug mode.......................................................................................................46
Create and Import some more IDE Converted tests......................................................................49
Exercises........................................................................................................................................52

1
Chapter 7:The Annotated Generated Test...........................................................................................54
Use Attach Source to see the Selenium Driver Source Code........................................................54
Why should we do this?.................................................................................................................57
MyFirstSeleniumTests.java Annotated..........................................................................................57
A little more about selenium-java-client-driver.jar........................................................................62
A little about JUnit.........................................................................................................................63
A look at SeleneseTestCase.java....................................................................................................64
Use the SeleneseTestCase Functionality........................................................................................66
Using the IDE to add asserts and verifies.................................................................................66
First record the setup steps...................................................................................................66
Add the Verify Statements....................................................................................................67
Add the Asserts.....................................................................................................................68
What is the difference between and Assert and a Verify?.........................................................69
How to switch on line numbers in Eclipse?..............................................................................72
Creating Screenshots on Failure with SeleneseTestCase..........................................................73
Chapter 8:Let's get coding..................................................................................................................76
A brief pause, because you have already learned some Java....................................................76
What we will now learn............................................................................................................76
Create a New Test From Scratch...............................................................................................76
Our First Test with Annotations................................................................................................77
Back to Our Selenium Test.......................................................................................................79
Making Something Happen......................................................................................................81
Your Final Test Code.................................................................................................................82
The DefaultSelenium constructor explained.............................................................................82
Retrospectives...........................................................................................................................84
Well Done.............................................................................................................................84
Why did you do that?...........................................................................................................84
Run Selenium-RC in a dos window?...............................................................................84
Start it easily...............................................................................................................84
See the logging messages............................................................................................85
Find The Window........................................................................................................85
Stop and Restart Selenium Easily...............................................................................85
Final Notes...........................................................................................................................85
Back to Eclipse..............................................................................................................................86
Removing the Source Code Association...................................................................................86
Chapter 9:Chapter - What if it all goes wrong?..................................................................................87
Check that Selenium Server is running..........................................................................................87
Close any Selenium-RC windows.................................................................................................87
Stop and restart the Selenium-RC server.......................................................................................88
Remove any Java threads from the Debug perspective.................................................................88
Check for any blocked browser windows......................................................................................89
Firefox Updates..............................................................................................................................89
Summary...................................................................................................................................90
Chapter 10:Essential Firefox Add-Ons...............................................................................................91
What is Firebug?............................................................................................................................91
What is XPather?...........................................................................................................................91
Basic Firebug Usage......................................................................................................................92
Basic XPather Usage.....................................................................................................................94
Chapter 11:First Steps with Test Automation Thinking.....................................................................98
Let's Automate Search...................................................................................................................98

2
Start writing some tests..................................................................................................................99
Make Selenium Type In Some Text..........................................................................................99
Start with a copy and Paste...................................................................................................99
Fix the duplicate method name..........................................................................................100
Add a Selenium Command.................................................................................................101
The “type” method.............................................................................................................102
Find the locator with FireBug............................................................................................102
A brief introdution to XPath...............................................................................................104
Add the locator into the code.............................................................................................105
Check that it works.............................................................................................................105
Now make it Click on the Search Button................................................................................106
Selenium “Click” Method..................................................................................................106
Locate the button with FireBug..........................................................................................107
Create an xpath so that selenium can locate it....................................................................107
Add the XPath details to our source code..........................................................................107
One last thing to do............................................................................................................107
Now check that it works.....................................................................................................107
Quick Summary......................................................................................................................108
But is it a test if we don't check any results?..........................................................................108
But we can't control the data..............................................................................................109
Automate the Acceptance Criteria...........................................................................................110
Acceptance Critieria: Selenium Remote Control homepage listed in returned results......110
Look for text in the page................................................................................................110
Make the test fail on error with assertions................................................................111
Debugging a Failing Test..........................................................................................112
Our final test with assertions.....................................................................................113
Scan the page source code.............................................................................................114
look for a specific URL in the page...............................................................................116
Most Robust Method.....................................................................................................119
Acceptance Criteria: Page title has “Selenium-RC” in it...................................................119
Acceptance Criteria: Search box on the page has Selenium-RC in it................................120
One test to rule them all..........................................................................................................120
Summary......................................................................................................................................121
Chapter 12:First Steps with Automation Refactored........................................................................123
Introducing Refactoring...............................................................................................................123
Automatically refactoring the Startup code into a new method..................................................124
Automatically refactor the tear down code into a new method...................................................126
JUnit @Before and @After ........................................................................................................128
Refactoring Plan and Analysis................................................................................................128
Remove Parameter from stopSeleniumAndCloseBrowser....................................................129
Move setup code into a new method.......................................................................................130
Remove the setup code from each @Test method..................................................................131
Annotate stopSeleniumAndCloseBrowser with @After........................................................131
Remove the teardown code from each @Test method............................................................131
The Refactored Code..............................................................................................................133
@BeforeClass & @AfterClass................................................................................................134
Our refactored code............................................................................................................135
Automatically create JUnit Test Structure...................................................................................136
Summary......................................................................................................................................139
Chapter 13:How do I upgrade my Selenium Server when a new version comes out?.....................140

3
First Download the New Version.................................................................................................140
Configure Eclipse Project............................................................................................................140
Remember to re-attach the source...............................................................................................142
Chapter 14:Chapter - Basic HTML Theory......................................................................................143
Page Structure..............................................................................................................................143
Elements & Tags..........................................................................................................................144
Attributes.....................................................................................................................................144
Expanded.....................................................................................................................................144
Chapter 15:Basic XPATH Theory....................................................................................................145
Xpath Expressions.......................................................................................................................145
Node Types..................................................................................................................................145
Selections ....................................................................................................................................146
Predicates.....................................................................................................................................146
Advanced.....................................................................................................................................147
Combining Match Queries......................................................................................................147
Wild Card Matches..................................................................................................................147
Boolean Operators...................................................................................................................148
Xpath Functions...........................................................................................................................148
Xpath optimisation.......................................................................................................................149
Use the ID...............................................................................................................................150
Use the attributes.....................................................................................................................150
Start at the first unique element..............................................................................................150
Selenium & Xpath.......................................................................................................................150
Chapter 16:Basic CSS Selector Theory............................................................................................152
Firefinder for Firebug..................................................................................................................152
CSS Selector Expresssions..........................................................................................................153
Selenium & CSS..........................................................................................................................154
Selections.....................................................................................................................................154
Direct Descendents and Absolute Paths..................................................................................155
Sub Elements and Relative Paths............................................................................................155
Attribute and Index matching......................................................................................................155
Attribute Matching..................................................................................................................155
Special attribute selectors........................................................................................................155
Indexed Matching...................................................................................................................156
Advanced.....................................................................................................................................157
Combining Matches................................................................................................................157
Wild Card Matches..................................................................................................................157
Attribute Substring matching..................................................................................................157
Boolean Operators...................................................................................................................158
Sibling Combinators...............................................................................................................158
Useful Links.................................................................................................................................158
Chapter 17:Learning The Selenium API..........................................................................................160
Chapter 18:Testing HTML Forms....................................................................................................161
An HTML Form...........................................................................................................................162
Submit a Form with Default Values.............................................................................................163
Commands Used.....................................................................................................................165
Submit Form without clicking button.....................................................................................165
Amend a Text, Password, TextArea or File Field........................................................................166
Text Fields...............................................................................................................................166
Type vs TypeKeys...................................................................................................................166

4
Password Fields.......................................................................................................................166
TextArea Fields.......................................................................................................................167
File Field.................................................................................................................................168
Attachfile command...........................................................................................................168
Amending Checkboxes and Radio Items.....................................................................................169
Click........................................................................................................................................169
Check .....................................................................................................................................169
Uncheck..................................................................................................................................170
Amending Multiple Select Values...............................................................................................171
Amending Dropdown Elements...................................................................................................173
Amending Hidden Field Values...................................................................................................174
Checking the Values of the Input Fields......................................................................................174
Text, Password, TextArea, File...............................................................................................174
CheckBox & Radio Items.......................................................................................................175
Multiple Select & DropDown.................................................................................................176
getValue..............................................................................................................................176
isSomethingSelected..........................................................................................................176
getSelectedId & getSelectedIds..........................................................................................177
getSelectedIndex & getSelectedIndexes............................................................................177
getSelectedLabel & getSelectedLabels..............................................................................177
getSelectedValue & getSelectedValues..............................................................................178
getSelectOptions.................................................................................................................178
Put It All Together..............................................................................................................179
Chapter 19:Testing Static HTML.....................................................................................................181
A Basic Static HTML Test...........................................................................................................181
getTitle – Check the page title.................................................................................................183
getText – Check the username................................................................................................184
isTextPresent – check the 'heading'.........................................................................................186
isElementPresent – check comments and filename................................................................186
getXPathCount – count the checkboxes entered.....................................................................187
click – navigate to the form.....................................................................................................189
The rest of the test...................................................................................................................189
The full test and summary.......................................................................................................190
getAttribute..................................................................................................................................191
assignId........................................................................................................................................191
getBodyText & getHtmlSource...................................................................................................192
goBack.........................................................................................................................................193
highlight.......................................................................................................................................193
Chapter 20:Using JavaScript with Selenium....................................................................................195
A Basic JavaScript Enabled Page................................................................................................195
The Class......................................................................................................................................196
FireEvent, isAlertPresent, and getAlert.......................................................................................197
getEval.........................................................................................................................................198
RunScript.....................................................................................................................................198
AddScript.....................................................................................................................................199
Chapter 21:Start Selenium Programmatically..................................................................................200
Add Selenium Server to the project.............................................................................................200
Start it in the code........................................................................................................................201
The Start Up routine explored.....................................................................................................203
BindException Handling.........................................................................................................203

5
Stop Existing Server................................................................................................................203
Custom Remote Control Configurations.................................................................................204
Chapter 22:Running Tests Outside Eclipse......................................................................................206
What is Ant?.................................................................................................................................206
What is Hudson?..........................................................................................................................206
What is Subversion?....................................................................................................................206
Chapter 23:Using Ant to Run Selenium Tests..................................................................................207
Install Ant.....................................................................................................................................207
Install Java jdk.............................................................................................................................207
Update the Environment Variables and Path...............................................................................208
Check that Ant works...................................................................................................................210
Check that Java works.................................................................................................................210
Create a Lib Folder......................................................................................................................211
Amend Eclipse Project Properties...............................................................................................211
Create an initial build.xml file.....................................................................................................212
Running the Ant file.....................................................................................................................216
Add the Tests which require no server.........................................................................................218
Refactor The Build File...............................................................................................................219
Summary......................................................................................................................................222
Additional Reading......................................................................................................................222
Chapter 24:Using Hudson to Run Ant..............................................................................................223
Introduction..................................................................................................................................223
Install Hudson..............................................................................................................................223
Using Hudson without Version Control.......................................................................................226
Configure the Hudson Project......................................................................................................228
Scheduling the build automatically..............................................................................................230
Chapter 25:Adding Subversion to the mix.......................................................................................232
Chapter 26:Take Stock of Where we are..........................................................................................233
Plan Of Action.............................................................................................................................233
Chapter 27:Evolving A Selenium Manager Class............................................................................236
Chapter 28:Create Our First Page Objects.......................................................................................243
Create a SearchPageTests class....................................................................................................243
Add the SeleniumManager .........................................................................................................244
Convert startSeleniumAndSearchForSeleniumRC......................................................................245
typeInASearchTermAndAssertThatHomePageTextIsPresent.....................................................249
typeInASearchTermAndAssertThatHomePageURLExists.........................................................250
typeInASearchTermAndCheckPageTitleHasSearchTermInIt.....................................................251
typeInASearchTermAndCheckSearchInputHasSearchTermInIt.................................................251
After all that ................................................................................................................................252
SearchPageTests.java..............................................................................................................252
SearchPage.java......................................................................................................................253
Final Notes...................................................................................................................................253
Chapter 29:More Page Object Creation...........................................................................................254
The Tests......................................................................................................................................254
HTML_form_tests.java...........................................................................................................254
Static_HTML_tests.java.........................................................................................................261
JavaScript_With_Selenium_Tests.java...................................................................................266
The Page Objects.........................................................................................................................268
BasicHTMLForm.java............................................................................................................268
Possible refactorings...........................................................................................................270

6
HTMLFormResultsPage.java.................................................................................................272
Discussion of getFilename.................................................................................................273
BasicAjaxPage.java.................................................................................................................274
Chapter 30:Page Object Models Summary......................................................................................275
Chapter 31:Data Driven Tests in JUnit.............................................................................................278
Basic Data Driven Testing.......................................................................................................278
JUnit Parameterized Class Runner And Constructor..............................................................279
Data supplied by an @Parameters annotated method.............................................................279
A Data Driven Test..................................................................................................................280
Running a Data Driven Test....................................................................................................280
The full code for the test.........................................................................................................281
Multiple Tests..........................................................................................................................282
Reading Data From Tab Delimited Files.................................................................................282
General Hints..........................................................................................................................283
Chapter 32:Screen Capture on Failure.............................................................................................285
Chapter 33:Run the tests on multiple browsers................................................................................290
Browser Codes.............................................................................................................................290
Property Files...............................................................................................................................291
System Properties........................................................................................................................293
Chapter 34:JavaScript And Dynamic HTML Testing Redux...........................................................295
Example Dynamic HTML Page..............................................................................................295
First Model the behaviour in the test......................................................................................296
Code the check in the page object model................................................................................296
First understand the page....................................................................................................296
Summary.................................................................................................................................297
Chapter 35:Cookie Handling............................................................................................................298
Cookies in the example application.............................................................................................298
How to see cookies on a web page?............................................................................................298
Firefox by Default...................................................................................................................298
Firecookie - Firebug Plugin....................................................................................................301
Selenium Cookie Commands.......................................................................................................302
Example Code For Cookie Testing..............................................................................................303
Basic Class..............................................................................................................................303
Check Cookie Creation using deleteAllVisibleCookies and isCookiePresent........................303
Get Cookie values using getCookie and getCookieByName..................................................304
getCookie............................................................................................................................304
getCookieByName.............................................................................................................305
deleteCookie.......................................................................................................................306
createCookie.......................................................................................................................307
Chapter 36:The Future of Selenium.................................................................................................309
If you want to experiment with 2.0..............................................................................................310
Chapter 37:Structuring the tests and code........................................................................................312
Java project folder and package structures..................................................................................312
Changing what goes into Version Control...................................................................................314
Configuring Source-code Folders in Eclipse..........................................................................315
Add libraries............................................................................................................................317
Re-organise the Classes and Packages.........................................................................................317
Share the Server among tests and create a base test class...........................................................319
Share the Server using a Singleton.........................................................................................319
Create a Base Class.................................................................................................................320

7
Get Ant Working..........................................................................................................................322
Make sure all tests are valid tests.................................................................................................324
Create end to end tests where we assert after executing functionality....................................324
Have all tests pass on multiple browsers................................................................................324
Locators as constants or methods............................................................................................325
Locators as constants..........................................................................................................325
Locators as methods...........................................................................................................326
Get Hudson Working...................................................................................................................327
Speed up the test execution with a suite......................................................................................331
Running Different Suites for Different Browsers........................................................................335
Nested Suites...........................................................................................................................335
Browser Specific Groups........................................................................................................336
Create a conditional Ant File..................................................................................................337
Get 2.0 working...........................................................................................................................339
Ways of using what we have built...............................................................................................339
Exercises For the reader:..............................................................................................................340
Chapter 38:In Closing.......................................................................................................................341
Additional Reading......................................................................................................................341
Chapter 39:Appendix – Selenium API MindMap............................................................................343
Chapter 40:Appendix – Using IE Developer Tools..........................................................................344
Find the locator with IE Developer Toolbar.......................................................................344
Represent the locator as an Xpath Statement.....................................................................347
Locate the Button...............................................................................................................348
Chapter 41:Appendix – JUnit 4 Summary.......................................................................................349
Chapter 42:Appendix - Playing along at home................................................................................350

Вам также может понравиться