20
6/2/15 1 ©2015 Eid Passport, Inc. All rights reserved. How to Prevent Test Automation Shelfware: A Selenium-WebDriver Case Study Alan Ark [email protected] http://www.linkedin.com/in/arkie ©2015 Eid Passport, Inc. All rights reserved. Goals 2 Introduction to Selenium Tips and Tricks Pitfalls to Avoid Ask questions as we go!

Prevent Test Automation Shelfware: A Selenium-WebDriver Case Study

Embed Size (px)

Citation preview

6/2/15  

1  

©2015 Eid Passport, Inc. All rights reserved. ©2015 Eid Passport, Inc. All rights reserved.

How to Prevent Test Automation Shelfware: A Selenium-WebDriver Case Study

Alan Ark [email protected] http://www.linkedin.com/in/arkie

©2015 Eid Passport, Inc. All rights reserved.

Goals

2

Introduction to Selenium Tips and Tricks

Pitfalls to Avoid Ask questions as we go!

6/2/15  

2  

©2015 Eid Passport, Inc. All rights reserved.

Watir?

Web Application Testing in Ruby A different open source project that drives browsers for test automation

©2015 Eid Passport, Inc. All rights reserved.

What is Selenium?

A tool to automate browsers! Quick regression testing across many browsers Automate web based admin tasks

6/2/15  

3  

©2015 Eid Passport, Inc. All rights reserved.

Why Selenium over Watir?

Choice More widely supported More bindings available

©2015 Eid Passport, Inc. All rights reserved.

Regression testing!

Repetitive test efforts Time consuming Reproducible tests across many browsers

6/2/15  

4  

©2015 Eid Passport, Inc. All rights reserved.

Automation of web based admin tasks!

Creation of data Reading of records on the browser Updating of content Deletion of records

©2015 Eid Passport, Inc. All rights reserved.

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/

6/2/15  

5  

©2015 Eid Passport, Inc. All rights reserved.

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/

©2015 Eid Passport, Inc. All rights reserved.

How do I start?

Pick a language! Java C# python ruby others supported as well

6/2/15  

6  

©2015 Eid Passport, Inc. All rights reserved.

Pick your browser/driver

Firefox Chrome IE Safari many more!

©2015 Eid Passport, Inc. All rights reserved.

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();  

6/2/15  

7  

©2015 Eid Passport, Inc. All rights reserved.

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    

©2015 Eid Passport, Inc. All rights reserved.

XPath vs. CSS

XPath //div[.  ='Some  Text  of  the  Div']  

 CSS  table[id='tblBadgeInfo']  thead  td  

 Speed considerations?

6/2/15  

8  

©2015 Eid Passport, Inc. All rights reserved.

Tips to avoid headaches….

GUI based tests sometimes thought of as fragile, brittle or unreliable How to prevent your Selenium automation from becoming shelfware

©2015 Eid Passport, Inc. All rights reserved.

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

6/2/15  

9  

©2015 Eid Passport, Inc. All rights reserved.

Do not use hard coded sleeps

Makes test scripts brittle when run on different environments.  //  Sleep  for  5  seconds  Thread.Sleep(5000);        button.Click();  

©2015 Eid Passport, Inc. All rights reserved.

Use a polling wait

Be flexible and return as soon as possible but ignore exceptions

6/2/15  

10  

©2015 Eid Passport, Inc. All rights reserved.

Use WebDriverWait

WebDriverWait  wait  =  new  WebDriverWait(driver,  TimeSpan.FromSeconds(30));    IWebElement  myDynamicElement  =  wait.Until<IWebElement>((d)  =>          {                  return  d.FindElement(By.Id("myButtonId"));          });  

©2015 Eid Passport, Inc. All rights reserved.

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

6/2/15  

11  

©2015 Eid Passport, Inc. All rights reserved.

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

©2015 Eid Passport, Inc. All rights reserved.

Login Page Example

6/2/15  

12  

©2015 Eid Passport, Inc. All rights reserved.

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();    }  }      

©2015 Eid Passport, Inc. All rights reserved.

Login Test Case

 var  loginPage  =  new  LoginPage();    loginPage.Login("user","pass");  

   

6/2/15  

13  

©2015 Eid Passport, Inc. All rights reserved.

Verify your assumptions….

Are you where you think you are?

Verify page elements on transitions Clicked links Form submission AJAX transitions

©2015 Eid Passport, Inc. All rights reserved.

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

6/2/15  

14  

©2015 Eid Passport, Inc. All rights reserved.

Things I like to log

URL of the page Timestamp Values used on assertions Values used on comparators Values used on loops

©2015 Eid Passport, Inc. All rights reserved.

IE Considerations

Sometimes click appears to do “nothing” Use SendKeys instead of Click https://www.google.com/webhp?q=ie+click+selenium

6/2/15  

15  

©2015 Eid Passport, Inc. All rights reserved.

SendKeys Code

Instead of button.Click();  

 Use

button.SendKeys(Keys.Enter);  

©2015 Eid Passport, Inc. All rights reserved.

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);  

6/2/15  

16  

©2015 Eid Passport, Inc. All rights reserved.

Handling Dialogs

Javascript alerts Javascript confirm Javascript prompts

 

©2015 Eid Passport, Inc. All rights reserved.

Example code

try  {      driver.SwitchTo().Alert();      return  true;    }    catch  (NoAlertPresentException)  {      //  Modal  dialog  is  not  displayed      return  false;    }  

   

6/2/15  

17  

©2015 Eid Passport, Inc. All rights reserved.

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);        

©2015 Eid Passport, Inc. All rights reserved.

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/

6/2/15  

18  

©2015 Eid Passport, Inc. All rights reserved.

Browser login prompts

©2015 Eid Passport, Inc. All rights reserved.

Advanced Topics

Use with Continuous Integration tools Remote control of tests Selenium Grid

6/2/15  

19  

©2015 Eid Passport, Inc. All rights reserved.

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

©2015 Eid Passport, Inc. All rights reserved.

The End

38

Enjoy the journey and

Have fun!

6/2/15  

20  

©2015 Eid Passport, Inc. All rights reserved.

Thank you!

39

Questions/Comments? Contact Info: Alan Ark [email protected] http://www.linkedin.com/in/arkie