Selenium Simplified - Selenium Training

Transcription

Selenium SimplifiedA tutorial guide to using the Selenium API in Java with JUnitAlan RichardsonPublished by Compendium Developments

Copyright 2010 by Alan RichardsonThe right of Alan Richardson to be identified as the author of this work has been asserted by himin accordance with the Copyright, Design and Patents Act 1988.The views expressed in this book are those of the author.First published in Great Britain in 2010 by:Compendium mcontact details:alan@compendiumdev.co.ukAuthor's Software Testing Blog: www.eviltester.comCompendium Developments: www.compendiumdev.co.ukEvery effort has been made to ensure that the information contained in this book is accurate at thetime of going to press, and the publishers and author cannot accept any responsibility for anyerrors or omissions, however caused. No responsibility for loss or damage occasioned by anyperson acting, or refraining from action, as a result of the material in this publication can beaccepted by the editor, the publisher or the author.Apart from any fair dealing for the purposes of research or private study, or criticism or review, aspermitted under the Copyright, Design and Patents Act 1988; this publication may only bereproduced, stored or transmitted, in any form or by any means, with the prior permission of thepublishers, or in the case of reprographic reproduction in accordance with the terms and licensesissued by the Copyright Licensing Agency, 90 Tottenham Court Road, London, W1T 4LP. Enquiriesconcerning reproduction outside these terms should be sent to the publishers.Version @ 19/01/20112nd Printinge-book ISBN : 978-0-9567332-0-7paperback book ISBN : 978-0-9567332-1-4

Table of ContentsIntroduction.9Getting the Source-code.9Windows, Linux or Mac.10Acknowledgments.10Chapter 1:Getting Started With The Tools.11What Tools?.11Selenium IDE.11Java.11Eclipse.12Selenium-RC.12Chapter 2:Selenium IDE Basics.13Install Firefox.13Install Selenium-IDE.13Capture Play Back – Recording a Script with the IDE.14Command, Target and Value.17Save The Test.17Further reading.17Chapter 3:Install Java.19Chapter 4:Install and Run Selenium-RC.20Step One - Download & Install Selenium-RC.20Overview of the Contents of the Selenium Archive.21Step Two - Run Selenium-RC.22Step Three – Stop the Selenium Server.23From the command line.23From a browser URL.24Running an IDE Generated Test in Different Browsers.24End Notes.26Chapter 5:The Eclipse IDE.27First - Install Eclipse.27Second - Run Eclipse.28Third - create a new Java project.31Chapter 6:Create a JUnit test Using the JUnit export from Selenium-IDE.33Introduction.33Export an IDE script as a JUnit test.33Load an existing script into the IDE.33Change the format of the script to Java.34Create a New Class in Eclipse.35Copy & Paste the code from Selenium IDE into Eclipse.38Resolve Import Errors.40Add JUnit to the build path.42Remove the unused import.44Run the JUnit test.45Allow it through the Firewall.45Seeing the test running.46Check the results.47It went too quickly!.481

Run the test in debug mode.49Create and Import some more IDE Converted tests.52Exercises.55Chapter 7:The Annotated Generated Test.56Use Attach Source to see the Selenium Driver Source Code.56Why should we do this?.59MyFirstSeleniumTests.java Annotated.59A little more about selenium-java-client-driver.jar.63A little about JUnit.65A look at SeleneseTestCase.java.65Use the SeleneseTestCase Functionality.67Using the IDE to add asserts and verifies.67First record the setup steps.67Add the Verify Statements.68Add the Asserts.70What is the difference between an Assert and a Verify?.71How to find line numbers in Eclipse?.73Creating Screenshots on Failure with SeleneseTestCase.74Chapter 8:Let's get coding.77A brief pause, because you have already learned some Java.77What we will now learn.77Create a New Test From Scratch.77Our First Test with Annotations.78Back to Our Selenium Test.80Making Something Happen.82Your Final Test Code.83The DefaultSelenium constructor explained.84Retrospectives.85Well Done.85Why did you do that?.85Run Selenium-RC in a DOS window?.85Start it easily.85See the logging messages.85Find The Window.86Stop and Restart Selenium Easily.86Final Notes.86Back to Eclipse.87Removing the Source Code Association.87Chapter 9:Chapter - What if it all goes wrong?.88Check that Selenium Server is running.88Close any Selenium-RC windows.89Stop and restart the Selenium-RC server.89Remove any Java threads from the Debug perspective.89Close any blocked browser windows.90Firefox Update Dialog.90Summary.91Chapter 10:Essential Firefox Add-Ons.92What is Firebug?.92What is XPather?.92Basic Firebug Usage.932

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

Our refactored code.135Automatically create JUnit Test Structure.136Summary.138Chapter 13:How do I upgrade my Selenium Server when a new version comes out?.139First Download the New Version.139Configure Eclipse Project.139Remember to re-attach the source.141Chapter 14:Chapter - Basic HTML Theory.142Page Structure.142Elements & Tags.142Attributes.143Expanded.143Chapter 15:Basic XPath Theory.144XPath Expressions.144Node Types.144Selections .145Predicates.145Advanced.146Combining Match Queries.146Wild Card Matches.146Boolean Operators.147XPath Functions.147XPath optimisation.148Use the ID.148Use the attributes.148Start at the first unique element.149Selenium XPath Usage.149Chapter 16:Basic CSS Selector Theory.150Firefinder for Firebug.150CSS Selector Expressions.151Selenium & CSS.151Selections.152Direct Descendents and Absolute Paths.152Sub Elements and Relative Paths.153Attribute and Index matching.153Attribute Matching.153Special attribute selectors.153Indexed Matching.154Advanced.155Combining Matches.155Wild Card Matches.155Attribute Substring matching.155Boolean Operators.155Sibling Combinators.156Useful Links.156Chapter 17:Learning The Selenium API.157Chapter 18:Testing HTML Forms.158An HTML Form.159Submit a Form with Default Values.159Commands Used.1624

Submit Form without clicking button.162Amend a Text, Password, TextArea or File Field.162Text Fields.162Type vs TypeKeys.163Password Fields.163TextArea Fields.163File Field.164Attachfile command.165Amending Checkboxes and Radio Items.165Click.165Check .166Uncheck.167Amending Multiple Select Values.168Amending Dropdown Elements.169Amending Hidden Field Values.170Checking the Values of the Input Fields.171Text, Password, TextArea, File.171CheckBox & Radio Items.171Multiple Select & DropDown.172getValue.173isSomethingSelected.

Selenium Simplified A tutorial guide to using the Selenium API in Java with J