Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Selenium 2
Marc Philipp
May 09, 2011
Programming
3
220
Selenium 2
Marc Philipp
May 09, 2011
Tweet
Share
More Decks by Marc Philipp
See All by Marc Philipp
Produktive Tests (German)
marcphilipp
1
200
Customizing and Refactoring Gradle Builds
marcphilipp
1
91
Schnelle und zuverlässige Builds mit Gradle und Maven
marcphilipp
1
79
Neues von JUnit 5.x: From Revolution to Continuous Evolution
marcphilipp
1
92
News from JUnit 5.x: From Revolution to Continuous Evolution
marcphilipp
0
230
Fast and reliable builds with Gradle and Maven
marcphilipp
2
220
News from JUnit 5.x: From Revolution to Continuous Evolution
marcphilipp
0
210
Gradle – Build Happiness (German)
marcphilipp
1
48
JUnit 5: More than just a Testing Framework for Java
marcphilipp
0
88
Other Decks in Programming
See All in Programming
Jetpack Compose, 어디까지 알고 있을까?
jisungbin
0
120
Independently together: better developer experience & App performance
bcinarli
0
180
From Java through Scala to Clojure
lagenorhynque
0
230
Gitlab CIでMRを自動生成する
forcia_dev_pr
0
120
Deep Dive Into Google Zanzibar and its Concepts for Authorization Scenarios
dschenkelman
1
140
設計ナイト2022 トランザクションスクリプト
shinpeim
11
2.1k
Improving Developer Experience Through Tools and Techniques 2022
krzysztofzablocki
0
1.2k
Licences open source : entre guerre de clochers et radicalité
pylapp
2
510
データ分析やAIの "運用" について考える
mmorito
0
150
Springin‘でみんなもクリエイターに!
ueponx
0
220
Beyond Micro Frontends: Frontend Moduliths for the Enterprise @enterjs2022
manfredsteyer
PRO
0
190
Imperative is dead, long live Declarative! | Appdevcon
prof18
0
110
Featured
See All Featured
Pencils Down: Stop Designing & Start Developing
hursman
112
9.8k
Design by the Numbers
sachag
271
17k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
29
4.3k
GraphQLの誤解/rethinking-graphql
sonatard
28
6.6k
Designing for Performance
lara
597
63k
How STYLIGHT went responsive
nonsquared
85
3.9k
Robots, Beer and Maslow
schacon
152
7.1k
Faster Mobile Websites
deanohume
294
28k
Intergalactic Javascript Robots from Outer Space
tanoku
261
25k
YesSQL, Process and Tooling at Scale
rocio
157
12k
Writing Fast Ruby
sferik
612
57k
A designer walks into a library…
pauljervisheath
196
16k
Transcript
Selenium
What is Selenium? Testing framework for web applications, 3 variants:
Selenium IDE Firefox add-on Record and playback user actions on a web site Not recommend for stable, long staying test suites © 2011 andrena objects ag. All rights reserved. 2
What is Selenium? (cont.) Selenium RC (Remote Control, Selenium 1)
Java Server that controls various browsers Executes scripted user actions from a programming environment, typically JUnit Has a “string-based” programming API Supports a variety of browsers (Internet Explorer 6, 7, 8, Firefox 2, 3, Safari, Chrome, Opera) Selenium WebDriver (Selenium 2) Java Library, which controls the browser Has a object-oriented programming API Supports Internet Explorer, Firefox, Chrome and HtmlUnit as drivers We recommend using the WebDriver API! © 2011 andrena objects ag. All rights reserved. 3
Selenium WebDriver API An instance implementing the WebDriver interface allows
access to the browser and to retrieve WebElements (input fields, buttons, etc.): get(String url) Loads new web page by URL. findElement(By locator) Finds WebElement or throws exception. findElements(By locator) Finds list of WebElements, possibly empty. . . . © 2011 andrena objects ag. All rights reserved. 4
Locating Elements on a Page Locators: instances of the By
class identifying an element in the HTML page. By.id("okay-button") Selects an element by its HTML id attribute. By.name("okayButton") Selects an element by its HTML name attribute. By.xpath("//div[@class=’navi’]//a[text()=’click’]") Treats the HTML as XML and uses an XPath expression to select an element. © 2011 andrena objects ag. All rights reserved. 5
Locating Elements on a Page (cont.) By.className("mainMenu") Uses a CSS
class name to select an element. By.cssSelector(".mainMenu a") Uses CSS selector syntax to select an element. By.linkText("click here") Finds a link by its text shown on the page. © 2011 andrena objects ag. All rights reserved. 6
Interacting With Elements on a Page element.click() Clicks on an
element. element.sendKeys("some text") Enters text into text field. element.clear() Clears the contents of a text field. element.setSelected() Selects an option element. . . . © 2011 andrena objects ag. All rights reserved. 7
Coding Dojo I Task Write Selenium tests driven by JUnit
that log in and out of a web application Dojo Rules 1. Two people are coding: 1 driver (has keyboard), 1 navigator. 2. They think aloud and comment their actions so the audience understands what’s going on. 3. After 3 minutes: driver leaves, navigator becomes driver. 4. Someone else joins as the new navigator. 5. Repeat. © 2011 andrena objects ag. All rights reserved. 8
Page Objects A page object is a Java class representing
a web page holds a WebDriver instance knows about locators public class LoginPage extends PageObject { public void setUsername(String username) { WebElement usernameField = driver.findElement(By.id(" j_username")); usernameField.clear(); usernameField.sendKeys(username); } ... } © 2011 andrena objects ag. All rights reserved. 9
Web Element Annotations Web elements can additionally be found using
annotations: public class LoginPage extends PageObject { @FindBy(id = "j_username") private WebElement usernameField; public void setUsername(String username) { usernameField.clear(); usernameField.sendKeys(username); } } This will instantiate the LoginPage class and create a proxy for each WebElement by evaluating the @FindBy annotation: PageFactory.initElements(driver, LoginPage.class) © 2011 andrena objects ag. All rights reserved. 10
Page Objects Actions A method that changes to another page
returns a new page object public class LoginPage extends PageObject { @FindBy(id = "registration-link") private WebElement registrationLink; public LoginPage(WebDriver driver) { super(driver); } public RegistrationPage clickRegistrationLink() { registrationLink.click(); return PageFactory.initElements(driver, RegistrationPage. class); } } © 2011 andrena objects ag. All rights reserved. 11
Coding Dojo II Task Write Selenium tests driven by JUnit
that log in and out of a web application using Page Objects! Dojo Rules 1. Two people are coding: 1 driver (has keyboard), 1 navigator. 2. They think aloud and comment their actions so the audience understands what’s going on. 3. After 3 minutes: driver leaves, navigator becomes driver. 4. Someone else joins as the new navigator. 5. Repeat. © 2011 andrena objects ag. All rights reserved. 12
Page Objects Benefits Page objects allow test code be separated
from details about page structures Tests are written on a higher level of abstraction Structural knowledge is reused Reduces overall code size and development effort on page structure changes In summary, Page Objects help write Selenium tests which are More readable More maintainable Less fragile © 2011 andrena objects ag. All rights reserved. 13
Thank you! References and Further Reading: http://www.seleniumhq.org/ http://code.google.com/p/selenium/wiki/PageObjects Marc Philipp
E-Mail marc@andrena.de Twitter @marcphilipp andrena objects ag Albert-Nestler-Straße 11 D-76131 Karlsruhe Tel. +49 (0) 721 6105-122 Büro Frankfurt Clemensstraße 8 D-60487 Frankfurt Tel. +49 (0) 69 977 860 38 © 2011 andrena objects ag. All rights reserved. 14