Testing web applications Selenium with Scala 3 and ScalaTest
I needed to test a web application recently, so I started using Selenium again. This time I decided to use Selenium with Scala, and Scala 3 and ScalaTest in particular.
Because Scala can generally use Java libraries very easily, this was a quick and easy setup. I just configured sbt as usual, and then started creating my Selenium tests with Scala 3 and ScalaTest.
As an example, this is the first Selenium ScalaTest I created just to get everything up and running again:
class SeleniumTestSuite1 extends AnyFunSuite with BeforeAndAfter:
var driver: WebDriver = null
var wdWait: WebDriverWait = null
before {
driver = FirefoxDriver()
wdWait = WebDriverWait(driver, 5)
}
after {
driver.quit
}
test("Test that we get the desired url") {
val url = "https://alvinalexander.com/"
driver.get(url)
// if the given css class can’t be found, this fails the test
wdWait.until(ExpectedConditions.visibilityOfElementLocated(
By.ByClassName("footer-col-last")
))
assert(driver.getCurrentUrl == url)
}
end SeleniumTestSuite1
As you can see, the great thing about using Scala with Selenium is how clean the code looks. There are no unnecessary parentheses, braces, semi-colons, or other visual clutter, so your Selenium tests are easy to read. In fact, they’re so easy to read, you can show them to non-technical people, and they will probably be able to grok what you’re doing.
Of course there are a million other things you can do with Selenium to test web applications, but if you’re interested in getting started with Selenium, Scala 3, ScalaTest, and sbt, I hope this information is helpful.
Reporting live from Longmont, Colorado,
Alvin Alexander
Recent blog posts
- Free: Introduction To Functional Programming video training course
- The #1 functional programming (and computer programming) book
- The User Story Mapping Workshop process
- Alvin Alexander, Certified Scrum Product Owner (CSPO)
- Alvin Alexander is now a Certified ScrumMaster (CSM)
- Our “Back To Then” app (for iOS and Android)
- A Docker cheat sheet
- Pushing a Scala 3 JAR/Docker file to Google Cloud Run
- Reading a CSV File Into a Spark RDD (Scala Cookbook recipe)
- Reading a File Into a Spark RDD (Scala Cookbook recipe)