Execute individual Selenium test via CLI in Play Framework using headless browser

Hey everyone,

I’m trying to figure out how to run just one Selenium test through the command line in Play Framework. I know I can do it through the browser UI, but I’m looking for a way to do it headless.

Right now, when I use play auto-test, it runs all my Selenium and JUnit tests together. But what if I only want to check a specific Selenium test? Is there a command or option to tell Play to run just that one test using a headless browser?

I’m hoping there’s a simple way to do this without having to set up a whole new configuration. Any ideas or tips would be super helpful! Thanks in advance.

As someone who’s worked extensively with Play Framework and Selenium, I can share a workaround I’ve used for this exact scenario. While Play doesn’t have a built-in CLI command for running individual Selenium tests headlessly, you can achieve this with a bit of configuration.

First, you’ll need to modify your build.sbt file to include a custom task. Add something like:

lazy val runSingleTest = inputKey[Unit]("Run a single Selenium test")

runSingleTest := {
  val args: Seq[String] = spaceDelimited("<arg>").parsed
  (Test / testOnly).toTask(s" ${args.mkString(" ")}").value
}

Then, you can run a specific test using:

sbt "runSingleTest *YourTestClassName -- -Dwebdriver.chrome.driver=/path/to/chromedriver -Dheadless=true"

This approach lets you run individual tests headlessly without changing your entire setup. It’s been a game-changer for my CI/CD pipeline efficiency.

hey, i’ve got a quick tip for ya. try using sbt’s test:testOnly command. it lets u run specific tests. like this:

sbt “test:testOnly *YourTestName – -Dwebdriver.chrome.driver=/path/to/driver -Dheadless=true”

just make sure ur test reads that headless flag. works great for me when i need to run just one test headlessly.

Having faced similar challenges, I can share a technique that’s worked well for me. Instead of relying solely on Play’s built-in commands, I’ve found success by leveraging SBT’s test filters combined with system properties for headless execution.

Here’s the approach:

sbt ‘testOnly *YourSpecificTestClass – -Dwebdriver.chrome.driver=/path/to/chromedriver -Dheadless=true’

This command allows you to run a single Selenium test headlessly. You’ll need to ensure your test class is set up to read the ‘headless’ system property and configure the WebDriver accordingly.

In your test setup, you can add logic like:

if (System.getProperty(“headless”) != null) {
ChromeOptions options = new ChromeOptions();
options.addArguments(“–headless”);
// Set up your WebDriver with these options
}

This method has significantly streamlined my testing process, especially for CI/CD pipelines.