SlideShare a Scribd company logo
SCALATEST SELENIUM DSL
Scala days 2014 - Berlin
MATTHEW FARWELL
• Over 20 years development experience
• Project Lead on Scalastyle, style checker for Scala http://www.scalastyle.org
• Contributor to various open source projects, notably Scalatest, JUnit, Scala-
IDE
• Co-author with Josh Suereth of "sbt in Action" http://manning.com/suereth2/
• https://github.com/matthewfarwell/scaladays-2014-selenium
GOALS
• Present the Selenium DSL
• Show how it simplifies the writing of selenium tests.
• Show how it can be used to test a javascript heavy application
• Show how it can be integrated with other tests to make more useful
integration tests
• The future, well what I would like anyway
SELENIUM
• Selenium automates browsers.
• Firefox, Chrome, Internet Explorer, Safari, Opera, HtmlUnit
• Selenium has interfaces for Java, Python, Ruby, etc.
• We'll be looking at from the point of view of testing
• But we could use it for other stuff – automation of administration for example.
EXAMPLE (JAVA)
final WebDriver driver = new FirefoxDriver();
Wait<WebDriver> wait = new WebDriverWait(driver, 30);
driver.get("http://www.google.ch/");
driver.findElement(By.name("q")).sendKeys("Matthew Farwell");
driver.findElement(By.name("btnG")).click();
wait.until(new ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver webDriver) {
return webDriver.findElement(By.id("resultStats")) != null;
}});
boolean b = driver.findElement(By.tagName("body"))
.getText().contains("farwell.co.uk");
System.out.println((found ? "You are famous" : "Not famous enough, sorry"));
EXAMPLE (PYTHON)
driver = webdriver.Firefox()
driver.get("http://www.google.ch")
elem = driver.find_element_by_name("q")
elem.send_keys("Farwell")
elem.send_keys(Keys.RETURN)
assert "farwell.co.uk" in driver.find_by_element_id("body")
EXAMPLE (SCALA)
go to "http://www.google.ch/"
textField("q").value = "Farwell"
click on "btnG"
eventually {
val t = find(tagName("body")).get.text
}
t should include("farwell.co.uk")
println("Yay, you are famous")
- AmIFamousScala.scala
OUR APPLICATION
• https://github.com/matthewfarwell/scaladays-2014-selenium
$ sbt
> re-start
- Run tests from Eclipse
$ sbt
> re-start
> test
A REAL TEST
val baseUrl = "http://localhost:9100/"
"slash" should "redirect to admin index.html" in {
go to baseUrl
currentUrl should be (baseUrl + "admin/index.html#/users")
find("pageTitle").get.text should be("Users")
textField("userSearch").value should be('empty)
find("userUsername_2").get.text should be("matthew")
}
- UserSpec1.scala
ADDING EVENTUALLY
eventually {
find("userUsername_2").get.text should be("matthew")
}
- UserSpec2.scala
ADDING EVENTUALLY (2)
eventually {
find("pageTitle").get.text should be("Users")
textField("userSearch").value should be('empty)
find("userUsername_2").get.text should be("matthew")
}
- UserSpec2.scala
GETTING / SETTING VALUES
go to (baseUrl + "admin/index.html#/users")
eventually {
click on "userEdit_2"
}
eventually {
val tf = textField("userFullname")
tf.value should be("Matthew Farwell")
tf.isEnabled should be (true)
}
textField("userFullname").value = "Matthew Farwell is great"
- UserSpec3.scala
GETTING / SETTING VALUES
pwdField("password").value = "hello world"
pwdField("confirmPassword").value = "hello world"
find("error").get.text should be("Passwords do not match")
- UserSpec3.scala
DEBUGGING
capture to "foo.png"
println(currentUrl)
println(pageSource)
withScreenshot {
// some stuff
// if ModifiableMessage is thrown, screenshot is taken
}
DEBUGGING - REPORTERS
event match {
case e: TestFailed => {
val name = e.testName
web.captureTo(s"${name}.png")
println(web.pageSource)
}
}
- Common.scala
OTHER INPUT TYPES
• textField
• pwdField
• checkBox
• radioButton
• singleSel, multiSel
• HTML5 - emailField, colorField, dateField, dateTimeField, dateTimeLocalField,
monthField, numberField, rangeField, searchField, telField, timeField, urlField,
weekField
XPATH LOOKUPS
"list" should "default to 10 items" in {
go to (baseUrl + "admin/index.html#/users")
eventually {
val xp = "//tr//td[starts-with(@id, 'userUsername_')]"
val tds = findAll(xpath(xp)).map(_.text).toList
tds.size should be(10)
}
}
- UserSpec5.scala
PAGE OBJECT PATTERN
class HomePage extends Page {
val url = "localhost:9100/index.html"
}
val homePage = new HomePage
go to homePage
COOKIES
add cookie ("cookie_name", "cookie_value")
cookie("cookie_name").value should be ("cookie_value")
delete cookie "cookie_name"
ALERTS, FRAMES, WINDOWS
switch to alert
switch to frame(0) // switch by index
switch to frame("name") // switch by name
switch to window(windowHandle)
switch to activeElement
switch to defaultContent
NAVIGATION
goBack()
goForward()
reloadPage()
JAVASCRIPT
go to (host + "index.html")
val result1 = executeScript("return document.title;")
result1 should be ("Test Title")
val result2 = executeScript("return 'Hello ' + arguments[0]", "ScalaTest")
result2 should be ("Hello ScalaTest")
INTEGRATION TESTING
// create a new user
click on "newUser"
textField("username").value = "user"
// etc.
click on "submit"
// how do we test that the user has been created?
- UserSpec4.scala
INTEGRATION
• A lot of the time, we would like to assert something which isn't available
through the HTML front end
INTEGRATION TESTING
eventually {
val after = Services.listUsers().unmarshall
after.size should be(before.size + 1)
val u = after.find(_.username == "user").get
u.fullName should be(Some("Mr. User"))
u.id should not be (None)
}
- UserSpec4.scala
MULTI-BROWSER TESTING
abstract class UserSpecBase with Driver {
// tests here
}
class UserSpecChrome extends UserSpecBase with Chrome
class UserSpecFirefox extends UserSpecBase with Firefox
class UserSpecSafari extends UserSpecBase with Safari
- UserSpec6.scala
OUR CONTEXT
• Lots of legacy code, partially tested with selenium in Java / Python.
• Lots of new code, which has been tested with Scalatest selenium DSL
• A simple click on a web page can have effects way beyond CRUD
• Selenium used for testing pure HTML, GWT, Angular JS applications
OUR EXPERIENCE
• DSL is good for walking through a test – clearer and more readable than the
Java equivalent
• We have tests written by "developers" and "testers"
• Consistent tests – we use Scalatest for unit tests, UI tests and integration tests.
• Selenium DSL is integrated into Scalatest
• Always have ids on elements
• Our integration tests work with heterogeneous actions in the same test:
• we can perform an action in the Browser,
• then check call rest service,
• then check the database,
• call a command via ssh on a remote machine.
WHAT WE'D LIKE TO IMPROVE
IMPLICIT EVENTUALLY
eventually {
click on "userEdit_2"
}
eventually {
textField("foo").get.value should be ("bar")
}
eventually {
click on "btn"
}
eventually {
textField("baz").get.value should be ("flobble")
}
WHAT WE'D LIKE TO IMPROVE
SLOWING DOWN
eventually {
click on "userEdit_2"
}
eventually {
textField("foo").get.value should be ("bar")
}
eventually {
click on "userEdit_2"
}
eventually {
textField("foo").get.value should be ("bar")
}
ME AGAIN
• Matthew Farwell
• Twitter: @matthewfarwell
• Blog: http://randomallsorts.blogspot.ch/
• Scalastyle: http://www.scalastyle.org
• sbt in Action: http://manning.com/suereth2

More Related Content

What's hot

Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
Knoldus Inc.
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
Yakov Fain
 
Intoduction to Play Framework
Intoduction to Play FrameworkIntoduction to Play Framework
Intoduction to Play Framework
Knoldus Inc.
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats Bryntse
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
Peter Drinnan
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
Yakov Fain
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
Tagged Social
 
AngularJS Unit Test
AngularJS Unit TestAngularJS Unit Test
AngularJS Unit Test
Chiew Carol
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
Michał Pierzchała
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
Troy Miles
 
Apache Ant
Apache AntApache Ant
Apache AntAli Bahu
 
Codegeneration With Xtend
Codegeneration With XtendCodegeneration With Xtend
Codegeneration With Xtend
Sven Efftinge
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
Timothy Oxley
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
Christopher Bartling
 
Oleksandr Tolstykh
Oleksandr TolstykhOleksandr Tolstykh
Oleksandr Tolstykh
CodeFest
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven TomacJavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
Morris Singer
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
Christopher Bartling
 
Php unit for drupal 8
Php unit for drupal 8Php unit for drupal 8
Php unit for drupal 8
valuebound
 

What's hot (19)

Jquery- One slide completing all JQuery
Jquery- One slide completing all JQueryJquery- One slide completing all JQuery
Jquery- One slide completing all JQuery
 
Dart for Java Developers
Dart for Java DevelopersDart for Java Developers
Dart for Java Developers
 
Intoduction to Play Framework
Intoduction to Play FrameworkIntoduction to Play Framework
Intoduction to Play Framework
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 
Intro to JavaScript
Intro to JavaScriptIntro to JavaScript
Intro to JavaScript
 
PHP Unit Testing
PHP Unit TestingPHP Unit Testing
PHP Unit Testing
 
AngularJS Unit Test
AngularJS Unit TestAngularJS Unit Test
AngularJS Unit Test
 
Painless JavaScript Testing with Jest
Painless JavaScript Testing with JestPainless JavaScript Testing with Jest
Painless JavaScript Testing with Jest
 
Angular Application Testing
Angular Application TestingAngular Application Testing
Angular Application Testing
 
Apache Ant
Apache AntApache Ant
Apache Ant
 
Codegeneration With Xtend
Codegeneration With XtendCodegeneration With Xtend
Codegeneration With Xtend
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 
JavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and KarmaJavaScript TDD with Jasmine and Karma
JavaScript TDD with Jasmine and Karma
 
Oleksandr Tolstykh
Oleksandr TolstykhOleksandr Tolstykh
Oleksandr Tolstykh
 
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven TomacJavaCro'14 - Unit testing in AngularJS – Slaven Tomac
JavaCro'14 - Unit testing in AngularJS – Slaven Tomac
 
Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015Unit Testing Express and Koa Middleware in ES2015
Unit Testing Express and Koa Middleware in ES2015
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
Php unit for drupal 8
Php unit for drupal 8Php unit for drupal 8
Php unit for drupal 8
 

Similar to Scaladays 2014 introduction to scalatest selenium dsl

WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
Kazuhiro Sera
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
scalaconfjp
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
Balint Erdi
 
Aurelia intro
Aurelia introAurelia intro
Aurelia intro
Ats Uiboupin
 
Scala Frustrations
Scala FrustrationsScala Frustrations
Scala Frustrations
takezoe
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Ondřej Machulda
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with SolrErik Hatcher
 
BP-6 Repository Customization Best Practices
BP-6 Repository Customization Best PracticesBP-6 Repository Customization Best Practices
BP-6 Repository Customization Best Practices
Alfresco Software
 
Android networking-2
Android networking-2Android networking-2
Android networking-2
Aravindharamanan S
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
ciklum_ods
 
Session on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh GundechaSession on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh Gundecha
Agile Testing Alliance
 
Antons Kranga Building Agile Infrastructures
Antons Kranga   Building Agile InfrastructuresAntons Kranga   Building Agile Infrastructures
Antons Kranga Building Agile Infrastructures
Antons Kranga
 
Testing in Scala by Adform research
Testing in Scala by Adform researchTesting in Scala by Adform research
Testing in Scala by Adform research
Vasil Remeniuk
 
Testing in Scala. Adform Research
Testing in Scala. Adform ResearchTesting in Scala. Adform Research
Testing in Scala. Adform Research
Vasil Remeniuk
 
Knolx session
Knolx sessionKnolx session
Knolx session
Knoldus Inc.
 
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングXitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
scalaconfjp
 
Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014
Ngoc Dao
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
Mike Pfaff
 

Similar to Scaladays 2014 introduction to scalatest selenium dsl (20)

WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
Solid And Sustainable Development in Scala
Solid And Sustainable Development in ScalaSolid And Sustainable Development in Scala
Solid And Sustainable Development in Scala
 
Solid and Sustainable Development in Scala
Solid and Sustainable Development in ScalaSolid and Sustainable Development in Scala
Solid and Sustainable Development in Scala
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
Ruby On Rails
Ruby On RailsRuby On Rails
Ruby On Rails
 
Aurelia intro
Aurelia introAurelia intro
Aurelia intro
 
Scala Frustrations
Scala FrustrationsScala Frustrations
Scala Frustrations
 
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
Selenium & PHPUnit made easy with Steward (Berlin, April 2017)
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
 
BP-6 Repository Customization Best Practices
BP-6 Repository Customization Best PracticesBP-6 Repository Customization Best Practices
BP-6 Repository Customization Best Practices
 
Android networking-2
Android networking-2Android networking-2
Android networking-2
 
Bring the fun back to java
Bring the fun back to javaBring the fun back to java
Bring the fun back to java
 
Session on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh GundechaSession on Selenium Powertools by Unmesh Gundecha
Session on Selenium Powertools by Unmesh Gundecha
 
Antons Kranga Building Agile Infrastructures
Antons Kranga   Building Agile InfrastructuresAntons Kranga   Building Agile Infrastructures
Antons Kranga Building Agile Infrastructures
 
Testing in Scala by Adform research
Testing in Scala by Adform researchTesting in Scala by Adform research
Testing in Scala by Adform research
 
Testing in Scala. Adform Research
Testing in Scala. Adform ResearchTesting in Scala. Adform Research
Testing in Scala. Adform Research
 
Knolx session
Knolx sessionKnolx session
Knolx session
 
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディングXitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
Xitrum Web Framework Live Coding Demos / Xitrum Web Framework ライブコーディング
 
Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014Xitrum @ Scala Matsuri Tokyo 2014
Xitrum @ Scala Matsuri Tokyo 2014
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 

Recently uploaded

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
abdulrafaychaudhry
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Neo4j
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
Pro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp BookPro Unity Game Development with C-sharp Book
Pro Unity Game Development with C-sharp Book
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Atelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissancesAtelier - Innover avec l’IA Générative et les graphes de connaissances
Atelier - Innover avec l’IA Générative et les graphes de connaissances
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 

Scaladays 2014 introduction to scalatest selenium dsl

  • 1. SCALATEST SELENIUM DSL Scala days 2014 - Berlin
  • 2. MATTHEW FARWELL • Over 20 years development experience • Project Lead on Scalastyle, style checker for Scala http://www.scalastyle.org • Contributor to various open source projects, notably Scalatest, JUnit, Scala- IDE • Co-author with Josh Suereth of "sbt in Action" http://manning.com/suereth2/ • https://github.com/matthewfarwell/scaladays-2014-selenium
  • 3. GOALS • Present the Selenium DSL • Show how it simplifies the writing of selenium tests. • Show how it can be used to test a javascript heavy application • Show how it can be integrated with other tests to make more useful integration tests • The future, well what I would like anyway
  • 4. SELENIUM • Selenium automates browsers. • Firefox, Chrome, Internet Explorer, Safari, Opera, HtmlUnit • Selenium has interfaces for Java, Python, Ruby, etc. • We'll be looking at from the point of view of testing • But we could use it for other stuff – automation of administration for example.
  • 5. EXAMPLE (JAVA) final WebDriver driver = new FirefoxDriver(); Wait<WebDriver> wait = new WebDriverWait(driver, 30); driver.get("http://www.google.ch/"); driver.findElement(By.name("q")).sendKeys("Matthew Farwell"); driver.findElement(By.name("btnG")).click(); wait.until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver webDriver) { return webDriver.findElement(By.id("resultStats")) != null; }}); boolean b = driver.findElement(By.tagName("body")) .getText().contains("farwell.co.uk"); System.out.println((found ? "You are famous" : "Not famous enough, sorry"));
  • 6. EXAMPLE (PYTHON) driver = webdriver.Firefox() driver.get("http://www.google.ch") elem = driver.find_element_by_name("q") elem.send_keys("Farwell") elem.send_keys(Keys.RETURN) assert "farwell.co.uk" in driver.find_by_element_id("body")
  • 7. EXAMPLE (SCALA) go to "http://www.google.ch/" textField("q").value = "Farwell" click on "btnG" eventually { val t = find(tagName("body")).get.text } t should include("farwell.co.uk") println("Yay, you are famous") - AmIFamousScala.scala
  • 8. OUR APPLICATION • https://github.com/matthewfarwell/scaladays-2014-selenium $ sbt > re-start - Run tests from Eclipse $ sbt > re-start > test
  • 9. A REAL TEST val baseUrl = "http://localhost:9100/" "slash" should "redirect to admin index.html" in { go to baseUrl currentUrl should be (baseUrl + "admin/index.html#/users") find("pageTitle").get.text should be("Users") textField("userSearch").value should be('empty) find("userUsername_2").get.text should be("matthew") } - UserSpec1.scala
  • 10. ADDING EVENTUALLY eventually { find("userUsername_2").get.text should be("matthew") } - UserSpec2.scala
  • 11. ADDING EVENTUALLY (2) eventually { find("pageTitle").get.text should be("Users") textField("userSearch").value should be('empty) find("userUsername_2").get.text should be("matthew") } - UserSpec2.scala
  • 12. GETTING / SETTING VALUES go to (baseUrl + "admin/index.html#/users") eventually { click on "userEdit_2" } eventually { val tf = textField("userFullname") tf.value should be("Matthew Farwell") tf.isEnabled should be (true) } textField("userFullname").value = "Matthew Farwell is great" - UserSpec3.scala
  • 13. GETTING / SETTING VALUES pwdField("password").value = "hello world" pwdField("confirmPassword").value = "hello world" find("error").get.text should be("Passwords do not match") - UserSpec3.scala
  • 14. DEBUGGING capture to "foo.png" println(currentUrl) println(pageSource) withScreenshot { // some stuff // if ModifiableMessage is thrown, screenshot is taken }
  • 15. DEBUGGING - REPORTERS event match { case e: TestFailed => { val name = e.testName web.captureTo(s"${name}.png") println(web.pageSource) } } - Common.scala
  • 16. OTHER INPUT TYPES • textField • pwdField • checkBox • radioButton • singleSel, multiSel • HTML5 - emailField, colorField, dateField, dateTimeField, dateTimeLocalField, monthField, numberField, rangeField, searchField, telField, timeField, urlField, weekField
  • 17. XPATH LOOKUPS "list" should "default to 10 items" in { go to (baseUrl + "admin/index.html#/users") eventually { val xp = "//tr//td[starts-with(@id, 'userUsername_')]" val tds = findAll(xpath(xp)).map(_.text).toList tds.size should be(10) } } - UserSpec5.scala
  • 18. PAGE OBJECT PATTERN class HomePage extends Page { val url = "localhost:9100/index.html" } val homePage = new HomePage go to homePage
  • 19. COOKIES add cookie ("cookie_name", "cookie_value") cookie("cookie_name").value should be ("cookie_value") delete cookie "cookie_name"
  • 20. ALERTS, FRAMES, WINDOWS switch to alert switch to frame(0) // switch by index switch to frame("name") // switch by name switch to window(windowHandle) switch to activeElement switch to defaultContent
  • 22. JAVASCRIPT go to (host + "index.html") val result1 = executeScript("return document.title;") result1 should be ("Test Title") val result2 = executeScript("return 'Hello ' + arguments[0]", "ScalaTest") result2 should be ("Hello ScalaTest")
  • 23. INTEGRATION TESTING // create a new user click on "newUser" textField("username").value = "user" // etc. click on "submit" // how do we test that the user has been created? - UserSpec4.scala
  • 24. INTEGRATION • A lot of the time, we would like to assert something which isn't available through the HTML front end
  • 25. INTEGRATION TESTING eventually { val after = Services.listUsers().unmarshall after.size should be(before.size + 1) val u = after.find(_.username == "user").get u.fullName should be(Some("Mr. User")) u.id should not be (None) } - UserSpec4.scala
  • 26. MULTI-BROWSER TESTING abstract class UserSpecBase with Driver { // tests here } class UserSpecChrome extends UserSpecBase with Chrome class UserSpecFirefox extends UserSpecBase with Firefox class UserSpecSafari extends UserSpecBase with Safari - UserSpec6.scala
  • 27. OUR CONTEXT • Lots of legacy code, partially tested with selenium in Java / Python. • Lots of new code, which has been tested with Scalatest selenium DSL • A simple click on a web page can have effects way beyond CRUD • Selenium used for testing pure HTML, GWT, Angular JS applications
  • 28. OUR EXPERIENCE • DSL is good for walking through a test – clearer and more readable than the Java equivalent • We have tests written by "developers" and "testers" • Consistent tests – we use Scalatest for unit tests, UI tests and integration tests. • Selenium DSL is integrated into Scalatest • Always have ids on elements • Our integration tests work with heterogeneous actions in the same test: • we can perform an action in the Browser, • then check call rest service, • then check the database, • call a command via ssh on a remote machine.
  • 29. WHAT WE'D LIKE TO IMPROVE IMPLICIT EVENTUALLY eventually { click on "userEdit_2" } eventually { textField("foo").get.value should be ("bar") } eventually { click on "btn" } eventually { textField("baz").get.value should be ("flobble") }
  • 30. WHAT WE'D LIKE TO IMPROVE SLOWING DOWN eventually { click on "userEdit_2" } eventually { textField("foo").get.value should be ("bar") } eventually { click on "userEdit_2" } eventually { textField("foo").get.value should be ("bar") }
  • 31. ME AGAIN • Matthew Farwell • Twitter: @matthewfarwell • Blog: http://randomallsorts.blogspot.ch/ • Scalastyle: http://www.scalastyle.org • sbt in Action: http://manning.com/suereth2

Editor's Notes

  1. No loop – just an example
  2. Find returns an Option