XBOSoft, Inc. All Rights Reserved. 1 Web Testing with Selenium
XBOSoft Founded in 2006 Dedicated to software quality Software QA Consulting Software Testing Offices in San Francisco, Beijing and Amsterdam XBOSoft, Inc. All Rights Reserved. 2
House Rules Everyone 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 experience Over 8 years with Watir About a year with Selenium
Agenda Intro on Selenium Tips and Tricks Pitfalls 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 data Reading of records on the browser Updating of content Deletion of records
What version of Selenium? Don’t use Selenium 1.0 - Selenium IDE Recorder is deprecated Javascript Injection to drive a browser Selenium 2 uses WebDriver http://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! Java C# python ruby others supported as well
Pick your browser/driver Firefox Chrome IE Safari many more!
How do I interact with the browser? Dev tools are built-in to browsers Inspect the HTML to glean the locators to use var inputElement = driver.FindElement(By.Name("myButton")); inputElement.Click();
Built-in locators Use these if you can driver.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'] CSS table[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 seconds button.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/d otnet/html/AllMembers_T_OpenQA_Seleniu m_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 Object class 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 transitions Clicked links Form submission
Be Generous with your logging Overlogging is better than underlogging Easier to examine output files to see where failures are occurring Especially true for remote execution Use logging to get a trail on most events
Things I like to log URL of the page Timestamp Values used on assertions Values used on comparators Values used on loops
IE Considerations Sometimes click appears to do “nothing” Use SendKeys instead of Click https://www.google.com/webhp?#safe=off&q=i e+click+selenium
SendKeys Code Instead of button.Click(); Use button.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 alerts Javascript confirm Javascript prompts
Example code try { driver.SwitchTo().Alert(); return true; } catch (NoAlertPresentException) { // Modal dialog not displayed return false; }
Handling Popup windows var 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: aark@eidpassport.com LinkedIn: 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! Services@xbosoft.com Q+A www.xbosoft.com

Web testing with Selenium

  • 1.
    XBOSoft, Inc. AllRights Reserved. 1 Web Testing with Selenium
  • 2.
    XBOSoft Founded in 2006 Dedicatedto software quality Software QA Consulting Software Testing Offices in San Francisco, Beijing and Amsterdam XBOSoft, Inc. All Rights Reserved. 2
  • 3.
    House Rules Everyone exceptthe 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
  • 4.
    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.
  • 5.
    Who is AlanArk? Sr. Software Developer in Test at Eid Passport in Hillsboro, Oregon, USA Over 20 years of automated testing experience Over 8 years with Watir About a year with Selenium
  • 6.
    Agenda Intro on Selenium Tipsand Tricks Pitfalls to avoid Ask questions as we go!
  • 7.
    Watir? Web Application Testingin Ruby A different open source project that drives browsers for test automation
  • 8.
    What is Selenium? Atool to automate browsers! Quick regression testing across many browsers Automate web based admin tasks
  • 9.
    Why Selenium overWatir? Choice More widely supported More bindings available
  • 10.
    Regression testing! Repetitive testefforts Reproducible tests across many browsers Time consuming
  • 11.
    Automation of webbased admin tasks! Creation of data Reading of records on the browser Updating of content Deletion of records
  • 12.
    What version ofSelenium? Don’t use Selenium 1.0 - Selenium IDE Recorder is deprecated Javascript Injection to drive a browser Selenium 2 uses WebDriver http://docs.seleniumhq.org/projects/webdriver/
  • 13.
    WebDriver? A platform andlanguage-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/
  • 14.
    How do Istart? Pick a language! Java C# python ruby others supported as well
  • 15.
  • 16.
    How do Iinteract with the browser? Dev tools are built-in to browsers Inspect the HTML to glean the locators to use var inputElement = driver.FindElement(By.Name("myButton")); inputElement.Click();
  • 17.
    Built-in locators Use theseif you can driver.FindElement(By.Name("myName")); driver.FindElement(By.Id("myId")); driver.FindElement(By.ClassName("myClass")); others as well
  • 18.
    XPath vs. CSS XPath //div[.='Some Text of the Div'] CSS table[id='tblBadgeInfo'] thead td Speed considerations?
  • 19.
    Tips to avoidheadaches…. GUI based tests sometimes thought of as fragile, brittle or unreliable How to prevent your Selenium automation from becoming shelfware
  • 20.
    Use unique locators Verydifficult if locators are not unique Avoid using index numbers Ask for some name/id/class on UI elements from the development team
  • 21.
    Do not usehard coded sleeps Makes test scripts brittle when run on different environments. Thread.Sleep(5000); // Sleep for 5 seconds button.Click();
  • 22.
    Use a pollingwait Be flexible and return as soon as possible but ignore exceptions
  • 23.
    Use WebDriverWait WebDriverWait wait= new WebDriverWait(driver, TimeSpan.FromSeconds(10)); IWebElement myDynamicElement = wait.Until<IWebElement>((d) => { return d.FindElement(By.Id("myButtonId")); });
  • 24.
    Use ExpectedConditions Convenience methodson things that are checked often. Use these with WebDriverWait. http://selenium.googlecode.com/git/docs/api/d otnet/html/AllMembers_T_OpenQA_Seleniu m_Support_UI_ExpectedConditions.htm
  • 25.
    Use Page Objects IsolateUI 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
  • 26.
  • 27.
    Login Page Object classLoginPage : 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(); } }
  • 28.
    Login Test Case varloginPage = new LoginPage(); loginPage.Login("user","pass");
  • 29.
    Verify your assumptions…. Areyou where you think you are? Verify page elements on transitions Clicked links Form submission
  • 30.
    Be Generous withyour logging Overlogging is better than underlogging Easier to examine output files to see where failures are occurring Especially true for remote execution Use logging to get a trail on most events
  • 31.
    Things I liketo log URL of the page Timestamp Values used on assertions Values used on comparators Values used on loops
  • 32.
    IE Considerations Sometimes clickappears to do “nothing” Use SendKeys instead of Click https://www.google.com/webhp?#safe=off&q=i e+click+selenium
  • 33.
  • 34.
    Handling Frames Be sureto set the focus to the frame hosting your elements. IWebElement mainFrame = driver.FindElement(By.Name("MainFrame")); driver.SwitchTo().Frame(mainFrame);
  • 35.
  • 36.
    Example code try { driver.SwitchTo().Alert(); returntrue; } catch (NoAlertPresentException) { // Modal dialog not displayed return false; }
  • 37.
    Handling Popup windows varwindowHandles = 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 );
  • 38.
    Not the onlyanswer... Sometimes Selenium can’t do the job. AutoIt can be used as a fall-back. https://www.autoitscript.com/site/autoit/
  • 39.
  • 40.
    Advanced Topics Use withContinuous Integration tools Remote control of tests Selenium Grid
  • 41.
    Summary Instrument your frameworkcorrectly 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
  • 42.
    Contact Info Email: aark@eidpassport.com LinkedIn:https://www.linkedin.com/in/arkie Come visit: http://bscwest.techwell.com/
  • 43.
    Post your questionson 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! Services@xbosoft.com Q+A www.xbosoft.com