selenium overview
TRANSCRIPT
TEST AUTOMATION TODAY
Web UI Test AutomationSelenium, Watir, PhantomJS, CasperJS, Sahi, QTP, SauceLabs
Native Desktop UI AutomationAutoIt, Sikuli, QTP, TestComplete
Mobile App Test AutomationMonkeyTalk, Calabash, Selendroid, Appium, EggPlant
Performance Test AutomationJMeter, Load Runner, NeoLoad, HttPerf
SAY HELLO TO SELENIUM!Who am I?
A set of open source tools to interact with browser application in an automated way.
What are my tools?
Selenium IDE, Selenium 1 (RC), Selenium 2 (WebDriver)
Who are my creators?
Selenium 1 - Jason Huggins, Selenium 2 - Simon Stewart
SELENIUM ROCKS! SAY HOW?
Limitless possibilities of using Selenium
Possible interesting uses:Test Automation, ofcourse!
Web data scrapingPerforming CRUDs on a webpage repetitively (Bulk data
creation from the UI)As a content downloader!
Automated reply to Facebook posts?
HOW WAS SELENIUM EARLIER??
Called as Selenium RC (Remote Control), had a server whichlaunches, stops and interacts with the browser.Selenium Commands from code -> RC Server -> Javascriptcommands to browserClient libraries used HTTP GET / POST to interact with theselenium serverSelenium Server used Selenium Core which was a set of JSprograms that were injected into the browser for executing thecommands sent.Client libraries were in Python, Ruby, Java, .NET, PHP, Perl
WHY EVERYBODY MOVING TOSELENIUM 2???
Problems with Selenium RC
Explicitly start the server
No good organized API
Used Javascript Injection
Slower execution
No new enhancements. Deprecation!!
Same origin policy problem
WEBDRIVER
Binds natively to the browser. No Javascript injection in browser's JS Sandbox
Better object oriented API
Bindings for Java, Python, C#, Ruby, Perl, Php, JS
Significantly fewer calls than Selenium RC
Variants: ChromeDriver, FireFoxDriver, InternetExplorerDriver, OperaDriver, AndroidDriver,
IPhoneDriver, HtmlUnitDriver
Overcomes same origin policy problem
Self-contained library. No server to start explicitly.
LEARNING THESE WILL MAKE USINGSELENIUM EASIER!
HTML, CSS and Javascript from
and Codeacademy HTMLDog
THE DOM!!
Cross-platform, language-independent convention for representing and interacting with
objects in HTML, XHTML and XML documents.
HTML is structured according to DOM and DOM of the webpage is accessed by Selenium
DRIVING THE DRIVER
Interface - IWebDriver; Implements - ISearchContext, IDisposable Functionality - Controls the browser, Finds elements, Helps in debuggingUsage - Creating instance of a flavor of webdriver class that implements this interfaceCommonly used methods - FindElement, FindElements, Manage, Navigate, Quit, CloseCommonly used properties - Title, URL
WEBELEMENT
Interface - IWebElement; Implements - ISearchContextFunctionality - Provides methods to identify and interact with DOMelements on web page. Instance of it represents a HTML element.
Usage: Created using FindElement / FindElements method on driverinstance or another webelement object.
Commonly used methods: Click, SendKeys, Clear, Submit,GetAttribute, GetCssValue
Commonly used properties: Displayed, Text, Enabled, Selected,TagName
ELEMENT LOCATINGUse By class to locate elements
By class provides following static member methods:
CssSelector
ClassName
Id
XPath
TagName
LinkText
PartialLinkText
Name
Subclass the By class to build your own locating mechanism. Eg: ByIdOrName
LOCATING BY ID
Id is the King!W3C standards state Ids should be unique.
Independent from the type of element.Developers usually give ids to elements which contain dynamic
elements. Benefit!If your developer doesn't add ids, convince him to do so!
LOCATING BY CSS / XPATH SELECTORS
css prefered over xpath.xpath engines different for different browsers.xpath tend to become complex and difficult to read.xpath useful when you want to fetch parent element orfind element by text.
USEFUL LINKS FOR XPATH AND CSSSELECTORS
Xpath to Css Cheat Sheet
Elemental Selenium Blog on performance benchmarks forCSS and XPATH selectors
A stackoverflow post
"A good way to start a CSS or Xpath locator is to start with anelement that you know is not likely to change much and use itas an ‘anchor’ in your locator. It may have an ID or stable location
but not be the element you need to locate but is a reliable position tosearch from. Your anchoring element can be above or below the
current element in the HTML tree, but most often it’s above."
How do I find the <li> ??
INDEX AND DESCENDANT LOCATORS
Index locatorsnth-child(), nth-of-type()
Use when finding element in a list, by indexNth-child example: p:nth-child(2)
Nth-of-type: p:nth-of-type(2)
Descendant LocatorsUse when finding direct child elements of parentCSS example: div > div > ul > li > span
Xpath example: //div/div/ul/li/span
COMPONENT OBJECT PATTERNCreate a single class for interacting with a widget/complex
control present frequently on different pages of the website
YOU NEED TO WAIT FOR MR. AJAX!
AJAX (Asynchronous Javascript & XML)
Dynamic content rendered and updated with the help ofAJAX and Javascript
This calls for the need to test dynamic UI which has timeuncertainty
WebDriver handles this using Implict and Explict waiting mechanism
Link: WebDriver Wait Commands
COMMON EXCEPTIONS
ElementNotVisibleException : Use Javascript to make element visible
IllegalLocatorException: when By.className() is used with a compound class name.NoSuchElementException: Use waits OR verify your locator
StaleElementReferenceException: Happens if a DOM operation happening on the page is
temporarily causing the element to be inaccessible.
public boolean retryFindClick(By by) { boolean result = false; int attempts = 0; while(attempts < 2) { try { driver.findElement(by).click(); result = true; break; } catch(StaleElementException e) { } attempts++; } return result;
IJAVASCRIPTEXECUTOR
Used to execute javascript on webpage.
Can be used for finding elements
Performing actions like click or selecting control
Syntax:
IJavaScriptExecutor jsExec = (IJavaScriptExecutor)_driver;
_searchTextField = (IWebElement)jsExec.ExecuteScript("return document.getElementById('txtSearch')"
_searchTextField.SendKeys(searchString);