Download - Web testing with Selenium
XBOSoft, Inc. All Rights Reserved. 1
Web
Testing
with
Selenium
XBOSoft
Founded in 2006Dedicated to software quality
Software QA Consulting
Software Testing
Offices in San Francisco, Beijing and Amsterdam
XBOSoft, Inc. All Rights Reserved. 2
House RulesEveryone except the speakers are muted
Questions via the gotowebinar control on the right side of your screen or
through Twitter @XBOSoft
Questions can be asked throughout the webinar - we’ll try to answer them
at the end.
You will receive info on recording after the webinar
XBOSoft, Inc. All Rights Reserved. 3
Meet Our Speakers
• VP Sales & Marketing at XBOSoft
• 15 years Marketing and Sales in High Tech
• Love the outdoors, reading and parenthood
XBOSoft, Inc. All Rights Reserved. 4
Steve Gohre
• Sr. Software Developer in Test at Eid Passport
• Over 20 years of test automation experience
• Veteran speaker
• Quality Week 1999
• PNSQC 2007
• PNSQC 2011
• PNSQC 2013
• Better Software West 2015 (June)
• Enjoys golf and fine wine
Alan Ark
Sabrina Gasson
• Marketing Manager of XBOSoft
• Emails you all regularly to join our
industry hot topic webinars
• And invites you all to download our latest
trends in software testing whitepapers.
Who is Alan Ark?
Sr. Software Developer in Test at Eid Passport in Hillsboro, Oregon, USA
Over 20 years of automated testing experienceOver 8 years with WatirAbout a year with Selenium
Agenda
Intro on SeleniumTips and TricksPitfalls to avoid
Ask questions as we go!
Watir?
Web Application Testing in Ruby
A different open source project that drives browsers for test automation
What is Selenium?
A tool to automate browsers!
Quick regression testing across many browsers
Automate web based admin tasks
Why Selenium over Watir?
Choice
More widely supported
More bindings available
Regression testing!
Repetitive test efforts
Reproducible tests across many browsers
Time consuming
Automation of web based admin tasks!
Creation of dataReading of records on the browserUpdating of contentDeletion of records
What version of Selenium?
Don’t use Selenium 1.0 - Selenium IDERecorder is deprecatedJavascript Injection to drive a browser
Selenium 2 uses WebDriverhttp://docs.seleniumhq.org/projects/webdriver/
WebDriver?
A platform and language-neutral interface that allows programs or scripts to introspect into, and control the behaviour of, a web browser
http://www.w3.org/TR/2013/WD-webdriver-20130117/
How do I start?
Pick a language!JavaC#pythonrubyothers supported as well
Pick your browser/driver
FirefoxChromeIESafarimany more!
How do I interact with the browser?
Dev tools are built-in to browsersInspect the HTML to glean the locators to use
var inputElement = driver.FindElement(By.Name("myButton"));
inputElement.Click();
Built-in locators
Use these if you candriver.FindElement(By.Name("myName"));
driver.FindElement(By.Id("myId"));
driver.FindElement(By.ClassName("myClass"));
others as well
XPath vs. CSS
XPath//div[. ='Some Text of the Div']
CSStable[id='tblBadgeInfo'] thead td
Speed considerations?
Tips to avoid headaches….
GUI based tests sometimes thought of as fragile, brittle or unreliable
How to prevent your Selenium automation from becoming shelfware
Use unique locators
Very difficult if locators are not unique
Avoid using index numbers
Ask for some name/id/class on UI elements from the development team
Do not use hard coded sleeps
Makes test scripts brittle when run on different environments.
Thread.Sleep(5000); // Sleep for 5 secondsbutton.Click();
Use a polling wait
Be flexible and return as soon as possible but ignore exceptions
Use WebDriverWait
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));IWebElement myDynamicElement = wait.Until<IWebElement>((d) =>
{return
d.FindElement(By.Id("myButtonId"));});
Use ExpectedConditions
Convenience methods on things that are checked often. Use these with WebDriverWait.
http://selenium.googlecode.com/git/docs/api/dotnet/html/AllMembers_T_OpenQA_Selenium_Support_UI_ExpectedConditions.htm
Use Page Objects
Isolate UI elements from the test cases
If the UI changes, your tests only need to be modified in a single place - the Page Object that defines the UI
Reduces duplicate code
Login Page Example
Login Page Objectclass LoginPage : BasePage {public LoginPage() {}public void Login(string username,string password) {var nameElement = driver.FindElement(By.Name("username"));nameElement .SendKeys(username);var passElement = driver.FindElement(By.Name("password"));passElement .SendKeys(password);var submitButton = driver.FindElement(By.Name("submit"));
submitButton.Click();}
}
Login Test Case
var loginPage = new LoginPage();loginPage.Login("user","pass");
Verify your assumptions….
Are you where you think you are?Verify page elements on transitionsClicked linksForm submission
Be Generous with your logging
Overlogging is better than underloggingEasier to examine output files to see where
failures are occurringEspecially true for remote executionUse logging to get a trail on most events
Things I like to log
URL of the pageTimestampValues used on assertionsValues used on comparatorsValues used on loops
IE Considerations
Sometimes click appears to do “nothing”
Use SendKeys instead of Click
https://www.google.com/webhp?#safe=off&q=ie+click+selenium
SendKeys Code
Instead ofbutton.Click();
Usebutton.SendKeys(Keys.Enter);
Handling Frames
Be sure to set the focus to the frame hosting your elements.
IWebElement mainFrame =driver.FindElement(By.Name("MainFrame"));
driver.SwitchTo().Frame(mainFrame);
Handling Dialogs
Javascript alertsJavascript confirm Javascript prompts
Example code
try {driver.SwitchTo().Alert();return true;}catch (NoAlertPresentException) {// Modal dialog not displayedreturn false;}
Handling Popup windowsvar windowHandles = driver.WindowHandles;
// if handle 0 is the main window then handle 1 is the popup, otherwise the popup is handle 0
var popUp = (windowHandles[0] == mainWindowHandle ? windowHandles[1] : windowHandles[0]);
driver.SwitchTo().Window(popUp);<do stuff>driver.SwitchTo().Window(mainWindowHandle );
Not the only answer...
Sometimes Selenium can’t do the job.AutoIt can be used as a fall-back.
https://www.autoitscript.com/site/autoit/
Browser login prompts
Advanced Topics
Use with Continuous Integration tools
Remote control of tests
Selenium Grid
Summary
Instrument your framework correctly and Selenium tests can be very good for you
Don’t be discouraged. Try different things.
Investigate to see what Selenium can do for you
Contact Info
Email: [email protected]: https://www.linkedin.com/in/arkie
Come visit: http://bscwest.techwell.com/
Post your questions on Twitter and we'll answer them @XBOSoft
Join us to keep updated on all our webinars, reports and whitepapers:
facebook.com/xbosoft
+xbosoft
linkedin.com/company/xbosoft
We post regularly on our blog – check us out! http://xbosoft.com/software-quality-blog/
Why not download our free Whitepapers, available here: http://xbosoft.com/knowledge-center/
You will receive an email with information on slides and recording. Any further queries regarding our services or ideas for
future webinars please email us! [email protected]
Q+A
www.xbosoft.com