1. Getting started with
Selenium 2
Sebastiano Armeli-Battana
@sebarmeli
https://github.com/sebarmeli
November 17 , 2011 Open Source Developers Conference, Canberra
2. What is Selenium?
Open Source Web Application Testing System
Automated UI Testing
Functional type of tests
Multiple browsers, multiple languages
3. History
2004 2007 2009 2011
Selenium WebDriver
Selenium 2
14. Locator Strategies
• Id
webDriver.findElement(By.id("logo"));
• Name
webDriver.findElement(By.name("q"));
• Tag Name
webDriver.findElement(By.tagName("H1"));
• Class name
webDriver.findElements(By.className("sponsor_logos"));
• CSS Selector
webDriver.findElement(By.cssSelector("section#sponsor>p"));
• XPath
webDriver.findElement(By.xpath("//section[@id=‘miniconfs’]/a[2]"));
• Link Text
webDriver.findElements(By.linkText("About"));
• Partial Link Text
webDriver.findElement(By.partialLinkText("visitcanberra"));
16. AJAX applications
DOM Elements loaded asynchronously
a) Polling the DOM for n seconds
webDriver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); (Java)
webDriver.manage.timeouts.implicit_wait = 30 (Ruby)
webDriver.implicit_wait(30) (Python)
b) Wait until condition occurs
ExpectedCondition / WebDriverWait
17. Testing style and executing JS
Testing CSS properties
webElement.getCssValue(“height”);
webElement.getCssValue(“background-image”);
Javascript execution
JavascriptExecutor js = (JavascriptExecutor) webDriver;
Long value = (Long) js.executeScript("return window.scrollY");
18. TargetLocator and Navigation Interfaces
TargetLocator Frames and Popup Dialogs
TargetLocator target = webDriver.switchTo();
WebElement element = target.frame(“name”)
Alert alert = target.alert();
Navigation Browser buttons emulation
Navigation nav = webDriver.navigate();
nav.back(); / nav.forward(); nav.to(“url”);
19. Migrating from Selenium 1
Selenium selenium = new WebDriverBackedSelenium(webDriver,
“http://osdc.com.au”);
selenium.open("http://osdc.com.au");
selenium.click("id=follow_twitter");
selenium.waitForPageToLoad("10000");
WebDriver webDriver = ((WebDriverBackedSelenium)
selenium).getUnderlyingWebDriver();
20. Page Object Pattern / PageFactory
Pages as Objects
Separation between DOM and services in a Page
WebDriver API not exposed
PageFactory class to easily instantiate a Page Object