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
- Automated GUI Testing (AGT) software, version 0.1.0
- Best practices of Automated GUI testing tools
- Seven benefits of automated GUI testing
- Business Analyst: How to write accurate software requirements
- Business Analyst: A simple secret to running a great meeting
- One thing a business analyst should ask about any requirement
- Business Analysts and Use Case quality: Questions to ask yourself when writing a Use Case
- The three things a Business Analyst should think about during meetings
- Testing web applications Selenium with Scala 3 and ScalaTest
- Scala Cookbook 2021: A best-selling new release in OOP and FP