package selenium
- Alphabetic
- Public
- All
Type Members
-
trait
Chrome extends WebBrowser with Driver with ScreenshotCapturer
WebBrowsersubtrait that defines an implicitWebDriverfor Chrome (anorg.openqa.selenium.chrome.ChromeDriver).WebBrowsersubtrait that defines an implicitWebDriverfor Chrome (anorg.openqa.selenium.chrome.ChromeDriver).- Annotations
- @deprecated
- Deprecated
Chrome has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
Driver extends AnyRef
Trait declaring a
webDriverfield that enables tests to be abstracted across different kinds ofWebDrivers.Trait declaring a
webDriverfield that enables tests to be abstracted across different kinds ofWebDrivers.This trait enables you to place tests that you want to run in multiple browsers in a trait with a self type of
WebBrowser with Driver, like this:trait MyBrowserTests { this: WebBrowser with Driver => // Your browser tests }
Then you can create concrete subclasses for each actual browser you want to run those tests in:
class MyBrowserTestsWithChrome extends MyBrowserTests with Chrome class MyBrowserTestsWithSafari extends MyBrowserTests with Safari class MyBrowserTestsWithInternetExplorer extends MyBrowserTests with InternetExplorer class MyBrowserTestsWithFirefox extends MyBrowserTests with Firefox
- Annotations
- @deprecated
- Deprecated
Driver has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
Firefox extends WebBrowser with Driver with ScreenshotCapturer
WebBrowsersubtrait that defines an implicitWebDriverfor Firefox (anorg.openqa.selenium.firefox.FirefoxDriver).WebBrowsersubtrait that defines an implicitWebDriverfor Firefox (anorg.openqa.selenium.firefox.FirefoxDriver).The
FirefoxDriveruses theFirefoxProfiledefined asfirefoxProfile. By default this is just anew FirefoxProfile. You can mutate this object to modify the profile, or overridefirefoxProfile.- Annotations
- @deprecated
- Deprecated
Firefox has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
HtmlUnit extends WebBrowser with Driver with ScreenshotCapturer
WebBrowsersubtrait that defines an implicitWebDriverfor HTMLUnit (anorg.openqa.selenium.htmlunit.HtmlUnitDriver), with JavaScript enabled by default.WebBrowsersubtrait that defines an implicitWebDriverfor HTMLUnit (anorg.openqa.selenium.htmlunit.HtmlUnitDriver), with JavaScript enabled by default.Note: You can disable JavaScript with:
webDriver.setJavascriptEnabled(false)
- Annotations
- @deprecated
- Deprecated
HtmlUnit has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
InternetExplorer extends WebBrowser with Driver with ScreenshotCapturer
WebBrowsersubtrait that defines an implicitWebDriverfor Internet Explorer (anorg.openqa.selenium.ie.InternetExplorerDriver).WebBrowsersubtrait that defines an implicitWebDriverfor Internet Explorer (anorg.openqa.selenium.ie.InternetExplorerDriver).- Annotations
- @deprecated
- Deprecated
InternetExplorer has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
Page extends AnyRef
Trait that facilitates using the page object pattern with the ScalaTest Selenium DSL.
Trait that facilitates using the page object pattern with the ScalaTest Selenium DSL.
If you use the page object pattern, mixing trait
Pageinto your page classes will allow you to use thego tosyntax with your page objects. Here's an example:class HomePage extends Page { val url = "localhost:9000/index.html" }
val homePage = new HomePage go to homePage- Annotations
- @deprecated
- Deprecated
Page has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
Safari extends WebBrowser with Driver with ScreenshotCapturer
WebBrowsersubtrait that defines an implicitWebDriverfor Safari (anorg.openqa.selenium.safari.SafariDriver).WebBrowsersubtrait that defines an implicitWebDriverfor Safari (anorg.openqa.selenium.safari.SafariDriver).- Annotations
- @deprecated
- Deprecated
Safari has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
trait
WebBrowser extends AnyRef
Trait that provides a domain specific language (DSL) for writing browser-based tests using Selenium.
Trait that provides a domain specific language (DSL) for writing browser-based tests using Selenium.
To use ScalaTest's Selenium DSL, mix trait
WebBrowserinto your test class. This trait provides the DSL in its entirety except for one missing piece: an implicitorg.openqa.selenium.WebDriver. One way to provide the missing implicit driver is to declare one as a member of your test class, like this:import org.scalatest._ import selenium._ import org.openqa.selenium._ import htmlunit._
class BlogSpec extends FlatSpec with Matchers with WebBrowser {
implicit val webDriver: WebDriver = new HtmlUnitDriver
val host = "http://localhost:9000/"
"The blog app home page" should "have the correct title" in { go to (host + "index.html") pageTitle should be ("Awesome Blog") } }For convenience, however, ScalaTest provides a
WebBrowsersubtrait containing an implicitWebDriverfor each driver provided by Selenium. Thus a simpler way to use theHtmlUnitdriver, for example, is to extend ScalaTest'sHtmlUnittrait, like this:import org.scalatest._ import selenium._
class BlogSpec extends FlatSpec with Matchers with HtmlUnit {
val host = "http://localhost:9000/"
"The blog app home page" should "have the correct title" in { go to (host + "index.html") pageTitle should be ("Awesome Blog") } }The web driver traits provided by ScalaTest are:
Driver WebBrowsersubtraitGoogle Chrome ChromeMozilla Firefox FirefoxHtmlUnit HtmlUnitMicrosoft Internet Explorer InternetExplorerApple Safari SafariNavigation
You can ask the browser to retrieve a page (go to a URL) like this:
go to "http://www.artima.com"Note: If you are using the page object pattern, you can also go to a page using the
Pageinstance, as illustrated in the section on page objects below.Once you have retrieved a page, you can fill in and submit forms, query for the values of page elements, and make assertions. In the following example, selenium will go to
http://www.google.com, fill in the text box withCheese!, press the submit button, and wait for result returned from an AJAX call:go to "http://www.google.com" click on "q" enter("Cheese!") submit() // Google's search is rendered dynamically with JavaScript. eventually { pageTitle should be ("Cheese! - Google Search") }
In the above example, the
"q"used in “click on "q"” can be either the id or name of an element. ScalaTest's Selenium DSL will try to lookup by id first. If it cannot find any element with an id equal to"q", it will then try lookup by name"q".Alternatively, you can be more specific:
click on id("q") // to lookup by id "q" click on name("q") // to lookup by name "q"
In addition to
idandname, you can use the following approaches to lookup elements, just as you can do with Selenium'sorg.openqa.selenium.Byclass:xpathclassNamecssSelectorlinkTextpartialLinkTexttagName
For example, you can select by link text with:
click on linkText("click here!")If an element is not found via any form of lookup, evaluation will complete abruptly with a
TestFailedException.Getting and setting input element values
ScalaTest's Selenium DSL provides a clear, simple syntax for accessing and updating the values of input elements such as text fields, radio buttons, checkboxes, selection lists, and the input types introduced in HTML5. If a requested element is not found, or if it is found but is not of the requested type, an exception will immediately result causing the test to fail.
The most common way to access field value is through the
valueproperty, which is supported by the following input types:Tag Name Input Type Lookup Method inputtexttextFieldtextarea-textAreainputpasswordpwdFieldinputemailemailFieldinputcolorcolorFieldinputdatedateFieldinputdatetimedateTimeFieldinputdatetime-localdateTimeLocalFieldinputmonthmonthFieldinputnumbernumberFieldinputrangerangeFieldinputsearchsearchFieldinputteltelFieldinputtimetimeFieldinputurlurlFieldinputweekweekFieldYou can change a input field's value by assigning it via the
=operator, like this:textField("q").value = "Cheese!"
And you can access a input field's value by simply invoking
valueon it:textField("q").value should be ("Cheese!")
If the text field is empty,
valuewill return an empty string ("").You can use the same syntax with other type of input fields by replacing
textFieldwithLookup Methodlisted in table above, for example to use text area:textArea("body").value = "I saw something cool today!" textArea("body").value should be ("I saw something cool today!")
or with a password field:
pwdField("secret").value = "Don't tell anybody!" pwdField("secret").value should be ("Don't tell anybody!")
Alternate Way for Data Entry
An alternate way to enter data into a input fields is to use
enterorpressKeys. Although both ofenterandpressKeyssend characters to the active element,pressKeyscan be used on any kind of element, whereasentercan only be used on text entry fields, which include:textFieldtextAreapwdFieldemailFieldsearchFieldtelFieldurlField
Another difference is that
enterwill clear the text field or area before sending the characters, effectively replacing any currently existing text with the new text passed toenter. By contrast,pressKeysdoes not do any clearing—it just appends more characters to any existing text. You can backup withpressKeys, however, by sending explicit backspace characters,"\u0008".To use these commands, you must first click on the input field you are interested in to give it the focus. Here's an example:
click on "q" enter("Cheese!")
Here's a (contrived) example of using
pressKeyswith backspace to fix a typo:click on "q" // q is the name or id of a text field or text area enter("Cheesey!") // Oops, meant to say Cheese! pressKeys("\u0008\u0008") // Send two backspaces; now the value is Cheese pressKeys("!") // Send the missing exclamation point; now the value is Cheese!
Radio buttons
Radio buttons work together in groups. For example, you could have a group of radio buttons, like this:
<input type="radio" id="opt1" name="group1" value="Option 1"> Option 1</input> <input type="radio" id="opt2" name="group1" value="Option 2"> Option 2</input> <input type="radio" id="opt3" name="group1" value="Option 3"> Option 3</input>
You can select an option in either of two ways:
radioButtonGroup("group1").value = "Option 2" radioButtonGroup("group1").selection = Some("Option 2")
Likewise, you can read the currently selected value of a group of radio buttons in two ways:
radioButtonGroup("group1").value should be ("Option 2") radioButtonGroup("group1").selection should be (Some("Option 2"))
If the radio button has no selection at all,
selectionwill returnNonewhereasvaluewill throw aTestFailedException. By usingvalue, you are indicating you expect a selection, and if there isn't a selection that should result in a failed test.If you would like to work with
RadioButtonelement directly, you can select it by callingradioButton:click on radioButton("opt1")you can check if an option is selected by calling
isSelected:radioButton("opt1").isSelected should be (true)
to get the value of radio button, you can call
value:radioButton("opt1").value should be ("Option 1")
Checkboxes
A checkbox in one of two states: selected or cleared. Here's how you select a checkbox:
checkbox("cbx1").select()And here's how you'd clear one:
checkbox("cbx1").clear()You can access the current state of a checkbox with
isSelected:checkbox("cbx1").isSelected should be (true)
Single-selection dropdown lists
Given the following single-selection dropdown list:
<select id="select1"> <option value="option1">Option 1</option> <option value="option2">Option 2</option> <option value="option3">Option 3</option> </select>
You could select
Option 2in either of two ways:singleSel("select1").value = "option2" singleSel("select1").selection = Some("option2")
To clear the selection, either invoke
clearor setselectiontoNone:singleSel("select1").clear() singleSel("select1").selection = None
You can read the currently selected value of a single-selection list in the same manner as radio buttons:
singleSel("select1").value should be ("option2") singleSel("select1").selection should be (Some("option2"))
If the single-selection list has no selection at all,
selectionwill returnNonewhereasvaluewill throw aTestFailedException. By usingvalue, you are indicating you expect a selection, and if there isn't a selection that should result in a failed test.Multiple-selection lists
Given the following multiple-selection list:
<select name="select2" multiple="multiple"> <option value="option4">Option 4</option> <option value="option5">Option 5</option> <option value="option6">Option 6</option> </select>
You could select
Option 5andOption 6like this:multiSel("select2").values = Seq("option5", "option6")
The previous command would essentially clear all selections first, then select
Option 5andOption 6. If instead you want to not clear any existing selection, just additionally selectOption 5andOption 6, you can use the+=operator, like this.multiSel("select2").values += "option5" multiSel("select2").values += "option6"
To clear a specific option, pass its name to
clear:multiSel("select2").clear("option5")
To clear all selections, call
clearAll:multiSel("select2").clearAll()You can access the current selections with
values, which returns an immutableIndexedSeq[String]:multiSel("select2").values should have size 2 multiSel("select2").values(0) should be ("option5") multiSel("select2").values(1) should be ("option6")
Clicking and submitting
You can click on any element with “
click on” as shown previously:click on "aButton" click on name("aTextField")
If the requested element is not found,
click onwill throw an exception, failing the test.Clicking on a input element will give it the focus. If current focus is in on an input element within a form, you can submit the form by calling
submit:submit()
Switching
You can switch to a popup alert bo using the following code:
switch to alertBox
to switch to a frame, you could:
switch to frame(0) // switch by index switch to frame("name") // switch by name
If you have reference to a window handle (can be obtained from calling windowHandle/windowHandles), you can switch to a particular window by:
switch to window(windowHandle)
You can also switch to active element and default content:
switch to activeElement switch to defaultContent
Navigation history
In real web browser, you can press the 'Back' button to go back to previous page. To emulate that action in your test, you can call
goBack:goBack()
To emulate the 'Forward' button, you can call:
goForward()
And to refresh or reload the current page, you can call:
reloadPage()
Cookies!
To create a new cookie, you'll say:
add cookie ("cookie_name", "cookie_value")
to read a cookie value, you do:
cookie("cookie_name").value should be ("cookie_value") // If value is undefined, throws TFE right then and there. Never returns null.
In addition to the common use of name-value cookie, you can pass these extra fields when creating the cookie, available ways are:
cookie(name: String, value: String) cookie(name: String, value: String, path: String) cookie(name: String, value: String, path: String, expiry: Date) cookie(name: String, value: String, path: String, expiry: Date, domain: String) cookie(name: String, value: String, path: String, expiry: Date, domain: String, secure: Boolean)
and to read those extra fields:
cookie("cookie_name").value // Read cookie's value cookie("cookie_name").path // Read cookie's path cookie("cookie_name").expiry // Read cookie's expiry cookie("cookie_name").domain // Read cookie's domain cookie("cookie_name").isSecure // Read cookie's isSecure flag
In order to delete a cookie, you could use the following code:
delete cookie "cookie_name"or to delete all cookies in the same domain:-
delete all cookies
To get the underlying Selenium cookie, you can use
underlying:cookie("cookie_name").underlying.validate() // call the validate() method on underlying Selenium cookie
Other useful element properties
All element types (
textField,textArea,radioButton,checkbox,singleSel,multiSel) support the following useful properties:Method Description locationThe XY location of the top-left corner of this Element.sizeThe width/height size of this Element.isDisplayedIndicates whether this Elementis displayed.isEnabledIndicates whether this Elementis enabled.isSelectedIndicates whether this Elementis selected.tagNameThe tag name of this element. underlyingThe underlying WebElementwrapped by thisElement.attribute(name: String)The attribute value of the given attribute name of this element, wrapped in a Some, orNoneif no such attribute exists on thisElement.textReturns the visible (i.e., not hidden by CSS) text of this element, including sub-elements, without any leading or trailing whitespace. Implicit wait
To set Selenium's implicit wait timeout, you can call the
implicitlyWaitmethod:implicitlyWait(Span(10, Seconds))
Invoking this method sets the amount of time the driver will wait when searching for an element that is not immediately present. For more information, see the documentation for method
implicitlyWait.Page source and current URL
It is possible to get the html source of currently loaded page, using:
pageSource
and if needed, get the current URL of currently loaded page:
currentUrl
Screen capture
You can capture screen using the following code:
val file = captureBy default, the captured image file will be saved in temporary folder (returned by java.io.tmpdir property), with random file name ends with .png extension. You can specify a fixed file name:
capture to "MyScreenShot.png"or
capture to "MyScreenShot"Both will result in a same file name
MyScreenShot.png.You can also change the target folder screenshot file is written to, by saying:
setCaptureDir("/home/your_name/screenshots")If you want to capture a screenshot when something goes wrong (e.g. test failed), you can use
withScreenshot:withScreenshot { assert("Gold" == "Silver", "Expected gold, but got silver") }In case the test code fails, you'll see the screenshot location appended to the error message, for example:
Expected gold but got silver; screenshot capture in /tmp/AbCdEfGhIj.png
Using the page object pattern
If you use the page object pattern, mixing trait
Pageinto your page classes will allow you to use thego tosyntax with your page objects. Here's an example:class HomePage extends Page { val url = "http://localhost:9000/index.html" }
val homePage = new HomePage go to homePageExecuting JavaScript
To execute arbitrary JavaScript, for example, to test some JavaScript functions on your page, pass it to
executeScript: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")
To execute an asynchronous bit of JavaScript, pass it to
executeAsyncScript. You can set the script timeout withsetScriptTimeout:val script = """ var callback = arguments[arguments.length - 1]; window.setTimeout(function() {callback('Hello ScalaTest')}, 500); """ setScriptTimeout(1 second) val result = executeAsyncScript(script) result should be ("Hello ScalaTest")
Querying for elements
You can query for arbitrary elements via
findandfindAll. Thefindmethod returns the first matching element, wrapped in aSome, orNoneif no element is found. ThefindAllmethod returns an immutableIndexedSeqof all matching elements. If no elements match the query,findAllreturns an emptyIndexedSeq. These methods allow you to perform rich queries usingforexpressions. Here are some examples:val ele: Option[Element] = find("q")
val eles: colection.immutable.IndexedSeq[Element] = findAll(className("small")) for (e <- eles; if e.tagName != "input") e should be ('displayed) val textFields = eles filter { tf.isInstanceOf[TextField] }Cleaning up
To close the current browser window, and exit the driver if the current window was the only one remaining, use
close:close()
To close all windows, and exit the driver, use
quit:quit()
Alternate forms
Although statements like “
delete all cookies” fit well with matcher statements like “title should be ("Cheese!")”, they do not fit as well with the simple method call form of assertions. If you prefer, you can avoid operator notation and instead use alternatives that take the form of plain-old method calls. Here's an example:goTo("http://www.google.com") clickOn("q") textField("q").value = "Cheese!" submit() // Google's search is rendered dynamically with JavaScript. eventually(assert(pageTitle === "Cheese! - Google Search"))
Here's a table showing the complete list of alternatives:
operator notation method call go to (host + "index.html")goTo(host + "index.html")click on "aButton"clickOn("aButton")switch to activeElementswitchTo(activeElement)add cookie ("cookie_name", "cookie_value")addCookie("cookie_name", "cookie_value")delete cookie "cookie_name"deleteCookie("cookie_name")delete all cookiesdeleteAllCookies()capture to "MyScreenShot"captureTo("MyScreenShot")- Annotations
- @deprecated
- Deprecated
WebBrowser has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
Deprecated Value Members
-
object
Chrome extends Chrome
Companion object that facilitates the importing of
Chromemembers as an alternative to mixing it in.Companion object that facilitates the importing of
Chromemembers as an alternative to mixing it in. One use case is to importChromemembers so you can use them in the Scala interpreter.- Annotations
- @deprecated
- Deprecated
Chrome has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
object
Firefox extends Firefox
Companion object that facilitates the importing of
Firefoxmembers as an alternative to mixing it in.Companion object that facilitates the importing of
Firefoxmembers as an alternative to mixing it in. One use case is to importFirefoxmembers so you can use them in the Scala interpreter.- Annotations
- @deprecated
- Deprecated
Firefox has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
object
HtmlUnit extends HtmlUnit
Companion object that facilitates the importing of
HtmlUnitmembers as an alternative to mixing it in.Companion object that facilitates the importing of
HtmlUnitmembers as an alternative to mixing it in. One use case is to importHtmlUnitmembers so you can use them in the Scala interpreter.- Annotations
- @deprecated
- Deprecated
HtmlUnit has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
object
InternetExplorer extends InternetExplorer
Companion object that facilitates the importing of
InternetExplorermembers as an alternative to mixing it in.Companion object that facilitates the importing of
InternetExplorermembers as an alternative to mixing it in. One use case is to importInternetExplorermembers so you can use them in the Scala interpreter.- Annotations
- @deprecated
- Deprecated
InternetExplorer has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
object
Safari extends Safari
Companion object that facilitates the importing of
Safarimembers as an alternative to mixing it in.Companion object that facilitates the importing of
Safarimembers as an alternative to mixing it in. One use case is to importSafarimembers so you can use them in the Scala interpreter.- Annotations
- @deprecated
- Deprecated
Safari has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.
-
object
WebBrowser extends WebBrowser
Companion object that facilitates the importing of
WebBrowsermembers as an alternative to mixing it in.Companion object that facilitates the importing of
WebBrowsermembers as an alternative to mixing it in. One use case is to importWebBrowsermembers so you can use them in the Scala interpreter.- Annotations
- @deprecated
- Deprecated
WebBrowser has been moved from org.scalatest.selenium to org.scalatestplus.selenium. Please update your imports, as this deprecated type alias will be removed in a future version of ScalaTest.