SlideShare uma empresa Scribd logo
1 de 31
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

Mais conteúdo relacionado

Mais procurados

Apache Ant
Apache AntApache Ant
Apache Ant
Ali Bahu
 

Mais procurados (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
 

Semelhante a 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
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 
Rapid Prototyping with Solr
Rapid Prototyping with SolrRapid Prototyping with Solr
Rapid Prototyping with Solr
Erik Hatcher
 

Semelhante a 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. Adform Research
Testing in Scala. Adform ResearchTesting in Scala. Adform Research
Testing in Scala. Adform Research
 
Testing in Scala by Adform research
Testing in Scala by Adform researchTesting in Scala by Adform research
Testing in Scala by 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
 

Último

%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
masabamasaba
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 

Último (20)

WSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaSWSO2CON 2024 Slides - Open Source to SaaS
WSO2CON 2024 Slides - Open Source to SaaS
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni%in Benoni+277-882-255-28 abortion pills for sale in Benoni
%in Benoni+277-882-255-28 abortion pills for sale in Benoni
 

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

Notas do Editor

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