Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Headless mode for Web #2239

Merged
merged 1 commit into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions maestro-cli/src/main/java/maestro/cli/command/TestCommand.kt
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ class TestCommand : Callable<Int> {
)
private var excludeTags: List<String> = emptyList()

@Option(
names = ["--headless"],
description = ["(Web only) Run the tests in headless mode"],
)
private var headless: Boolean = false

@CommandLine.Spec
lateinit var commandSpec: CommandLine.Model.CommandSpec

Expand Down Expand Up @@ -300,6 +306,7 @@ class TestCommand : Callable<Int> {
driverHostPort = driverHostPort,
deviceId = deviceId,
platform = parent?.platform,
isHeadless = headless,
) { session ->
val maestro = session.maestro
val device = session.device
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ object MaestroSessionManager {
deviceId: String?,
platform: String? = null,
isStudio: Boolean = false,
isHeadless: Boolean = isStudio,
block: (MaestroSession) -> T,
): T {
val selectedDevice = selectDevice(
Expand Down Expand Up @@ -89,6 +90,7 @@ object MaestroSessionManager {
selectedDevice.platform
),
isStudio = isStudio,
isHeadless = isHeadless,
driverHostPort = driverHostPort,
)
Runtime.getRuntime().addShutdownHook(thread(start = false) {
Expand Down Expand Up @@ -146,6 +148,7 @@ object MaestroSessionManager {
selectedDevice: SelectedDevice,
connectToExistingSession: Boolean,
isStudio: Boolean,
isHeadless: Boolean,
driverHostPort: Int?,
): MaestroSession {
return when {
Expand All @@ -163,7 +166,7 @@ object MaestroSessionManager {
driverHostPort,
)

Platform.WEB -> pickWebDevice(isStudio)
Platform.WEB -> pickWebDevice(isStudio, isHeadless)
},
device = selectedDevice.device,
)
Expand All @@ -188,7 +191,7 @@ object MaestroSessionManager {
)

selectedDevice.platform == Platform.WEB -> MaestroSession(
maestro = pickWebDevice(isStudio),
maestro = pickWebDevice(isStudio, isHeadless),
device = null
)

Expand Down Expand Up @@ -316,8 +319,8 @@ object MaestroSessionManager {
)
}

private fun pickWebDevice(isStudio: Boolean): Maestro {
return Maestro.web(isStudio)
private fun pickWebDevice(isStudio: Boolean, isHeadless: Boolean): Maestro {
return Maestro.web(isStudio, isHeadless)
}

private data class SelectedDevice(
Expand Down
10 changes: 8 additions & 2 deletions maestro-client/src/main/java/maestro/Maestro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -634,7 +634,10 @@ class Maestro(
return Maestro(driver)
}

fun web(isStudio: Boolean): Maestro {
fun web(
isStudio: Boolean,
isHeadless: Boolean,
): Maestro {
// Check that JRE is at least 11
val version = System.getProperty("java.version")
if (version.startsWith("1.")) {
Expand All @@ -646,7 +649,10 @@ class Maestro(
}
}

val driver = WebDriver(isStudio)
val driver = WebDriver(
isStudio = isStudio,
isHeadless = isHeadless,
)
driver.open()
return Maestro(driver)
}
Expand Down
5 changes: 3 additions & 2 deletions maestro-client/src/main/java/maestro/drivers/WebDriver.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import java.util.*

class WebDriver(
val isStudio: Boolean,
private val seleniumFactory: SeleniumFactory = ChromeSeleniumFactory()
isHeadless: Boolean = isStudio,
private val seleniumFactory: SeleniumFactory = ChromeSeleniumFactory(isHeadless = isHeadless)
) : Driver {

private var seleniumDriver: org.openqa.selenium.WebDriver? = null
Expand All @@ -60,7 +61,7 @@ class WebDriver(
}

override fun open() {
seleniumDriver = seleniumFactory.create(isStudio)
seleniumDriver = seleniumFactory.create()

try {
seleniumDriver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import org.openqa.selenium.chromium.ChromiumDriverLogLevel
import java.util.logging.Level
import java.util.logging.Logger

class ChromeSeleniumFactory : SeleniumFactory {
class ChromeSeleniumFactory(
private val isHeadless: Boolean
) : SeleniumFactory {

override fun create(
isStudio: Boolean
): WebDriver {
override fun create(): WebDriver {
System.setProperty("webdriver.chrome.silentOutput", "true")
System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true")
Logger.getLogger("org.openqa.selenium").level = Level.OFF
Expand All @@ -28,7 +28,7 @@ class ChromeSeleniumFactory : SeleniumFactory {
addArguments("--remote-allow-origins=*")
addArguments("--disable-search-engine-choice-screen")
addArguments("--lang=en")
if (isStudio) {
if (isHeadless) {
addArguments("--headless=new")
addArguments("--window-size=1024,768")
setExperimentalOption("detach", true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import org.openqa.selenium.WebDriver

interface SeleniumFactory {

fun create(
isStudio: Boolean = false
): WebDriver
fun create(): WebDriver

}
Loading