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

Valley Programming is currently a one-person business, owned and operated by Alvin Alexander. If you’re interested in anything you read here, feel free to contact me at “al” at (“@”) this website name (“valleyprogramming.com”), or at the phone number shown below. I’m just getting back to business here in November, 2021, and eventually I’ll get a contact form set up here, but until then, I hope that works.