Scalatest + Selenium Integration Test Tutorial

Source: Internet
Author: User
Tags assert subdomain seleniumhq

The Selenium Integration Test tool is built into play 1 and 2, where you try to test it individually with Scalatest + Selenium for simple integration testing. Selenium can support a built-in browser with no interface Java implementation, and can also use external browsers such as Safari, Firefox, Chrome, IE, Opera, or mobile device browsers. Use different external browsing methods or plug-ins (Safari/firefox, etc.) or Chromium to navigate the Chrome browser.

And the Scalatest 2.2.6 we're going to use here are the following qualities in the package Org.scalatest.selenium Chrome, Driver, Firefox, Htmlunit, InternetExplorer, Page, Safa RI and WebBrowser. This allows you to see the scalatest default supported browsers. If the Selenium itself, it can be powerful temporarily beyond my imagination, you can build the server, select the browser distribution test task. We know that the benefit of using Selenium is not only to assert the static text of the page as usual, but also to execute Javascript scripts, so you can assert dynamic content.

I developed the environment for MAC OS, can successfully let Selenium test run in the built-in Java browser, and external Safari, Chrome Browser, Firefox plug-in is not installed successfully.

Let the facts speak, still let one of the simplest examples speak for themselves, create a SBT project, try to simplify the directory, only BUILD.SBT files and Test/integrationtest.scala test code, the content is

Build.sbt

Librarydependencies ++= Seq (
"Org.scalatest"% "scalatest"% "2.2.6"% "test",
"Org.seleniumhq.selenium"% "Selenium-java"% "2.35.0"% "test"
)

Scalasource in Test: = Basedirectory.value/"Test"
It introduces the necessary scalatest and Selenium-java packages, and only cares about Scala test code in the test directory, and other directories randomly.

Test/integrationtest.scala


Import Java.util.concurrent.TimeUnit

Import Org.openqa.selenium.By
Import Org.scalatest.selenium._
Import Org.scalatest. {beforeandafterall, Flatspec, shouldmatchers}

Class Integrationtest extends Flatspec with Shouldmatchers with Chrome with Beforeandafterall {

Override Def afterall () {
Webdriver.quit ()
}

"Short Google url" should ' redirect to www subdomain ' in {
Go to "http://google.com"
Currenturl should Startwith ("https://www.google.com/")
}

"My Blog pages" should "have different titles" in {
Go to "http://unmi.cc"
Webdriver.gettitle should equal ("Yehuang Unmi Blog-Software Programming Practice")
Webdriver.manage (). Timeouts (). implicitlywait (5, Timeunit.seconds)
Webdriver.findelement (By.partiallinktext ("Same directory Layout")). Click ()
Webdriver.gettitle should include ("Create a Play 2 framework directory Layout")
}

It should "contain archive dropdown list" in {
Go to "http://unmi.cc"
Find (XPath ("//select[@name = ' Archive-dropdown ']"). IsDefined shouldbe True
}
}
Above with what the chrome part wants to use what browser with what, certainly is from Scalatest already supported Firefox, InternetExplorer, Safari, Htmlunit Select. Htmlunit's function is very weak, is to use it to run the above code is problematic, because my blog is still a lot of Javascript, as well as Ajax requests.

We're here to test the external Chrome and Safari

Chrome Run Selenium Test

If you run the test SBT directly, you will receive a message

[INFO] Exception encountered when attempting to run a suite with class Name:org.scalatest.DeferredAbortedSuite * * * aborted * * *
[INFO] java.lang.IllegalStateException:The path to the driver executable must is set by the Webdriver.chrome.driver sys Tem property; For more information, Http://code.google.com/p/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://code.google.com/p/chromedriver/downloads/list

It tells us to download Chromedriver and specify Chromedriver with Webdriver.chrome.driver system properties. From this page https://sites.google.com/a/chromium.org/chromedriver/find the latest version of the current Chromedriver_mac32.zip download, incredibly still only 32 bits, download after the assumption of decompression in /opt/chromedriver directory, you can run the above test with Sbt-dwebdriver.chrome.driver=/opt/chromedriver test.


Executive effect here, show it in a video, for the convenience of more domestic students to watch or put the video on the Youku, if you are in the flesh outside you can directly access the Youtube Chrome selenium video.


The command line information is as follows

➜✗sbt-dwebdriver.chrome.driver=/users/uqiu/downloads/chromedriver
[INFO] Set current project to Test_selenium (in Build file:/users/uqiu/workspaces/test_selenium/)
> Test
Starting Chromedriver 2.21.371459 (36D3D07F660FF2BC1BF28A75D1CDABED0983E7C4) on port 15045
Only the local connections are allowed.
[INFO] Integrationtest:
[INFO] Short Google URL
[INFO]-should redirect to www subdomain
[INFO] My Blog pages
[INFO]-should have different titles
[INFO]-should contain archive dropdown list
[INFO] Run completed in seconds, 253 milliseconds.
[INFO] Total number of tests Run:3
[INFO] Suites:completed 1, aborted 0
[INFO] Tests:succeeded 3, failed 0, canceled 0, ignored 0, pending 0
[INFO] All tests passed.
[Success] Total time:13 S, completed APR, 2016 1:41:32 AM

Safari on Run Selenium test

Change the With Chrome section of the previous code to with Safari to try to run the test, and if Safari has not installed the appropriate extensions, you can see it opens a Safari browser but cannot communicate

Because you need to install Webdriver extensions for Safari, you can also find the current version of the download link from this page http://www.seleniumhq.org/download/http:// Selenium-release.storage.googleapis.com/2.48/safaridriver.safariextz, the download opens after the connection is installed with Safari. And suggest that the extension is not official and whether it trusts

Here may be a point of skill, if the direct use of the mouse to click the Trust button may not happen, you can try to use the Tab to focus on the Trust button, and then by the space to be trusted, installed like my Safari expansion will show the Webdriver.

Execute the next SBT test again and you'll see the shutdown after you open Safari testing like chrome, and the results may not be the same as using chrome because the browser behaves differently and why we need to choose a variety of browsers for integration testing.

Another attempt to install Firefox's Selenium IDE plugin did not succeed in Firefox as an unauthenticated plugin that was forcibly disabled. I don't know why this type of Firefox plugin can't be posted to Firefox's official plugin library. should be able to try to open Firefox

The other is the use of Selenium API skills, how to manipulate events, input, positioning elements. And Selenium also has the function of screen-cutting, or save access data, even can record the entire test process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.