Skip to content

Commit

Permalink
feat: web api service can now use both Chrome and Firefox based on av…
Browse files Browse the repository at this point in the history
…ailability
  • Loading branch information
AlmasB committed Feb 23, 2024
1 parent 0a76bfe commit 96aaafd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import javafx.beans.property.ReadOnlyBooleanWrapper
import org.openqa.selenium.WebDriver
import org.openqa.selenium.chrome.ChromeDriver
import org.openqa.selenium.chrome.ChromeOptions
import org.openqa.selenium.firefox.FirefoxDriver
import org.openqa.selenium.firefox.FirefoxOptions

/**
* Provides access to JS-driven implementation.
Expand Down Expand Up @@ -65,21 +67,50 @@ abstract class WebAPIService(server: LocalWebSocketServer, private val apiURL: S
stop()
}

val options = ChromeOptions()
options.addArguments("--headless=new")
options.addArguments("--use-fake-ui-for-media-stream")

webDriver = ChromeDriver(options)
webDriver!!.get(apiURL)
webDriver = loadWebDriverAndPage(apiURL)

onWebDriverLoaded(webDriver!!)
} catch (e: Exception) {
log.warning("Failed to start Chrome web driver. Ensure Chrome is installed in default location")
log.warning("Failed to start web driver.")
log.warning("Error data", e)
}
}
}

private fun loadWebDriverAndPage(url: String): WebDriver {
val driverSuppliers = listOf(
{ loadChromeDriver() },
{ loadFirefoxDriver() }
)

driverSuppliers.forEach { supplier ->
try {
val driver = supplier()
driver.get(url)
return driver
} catch (e: Exception) {
log.warning("Failed to load web driver/page. Ensure Chrome or Firefox is installed in default location", e)
}
}

throw RuntimeException("No valid driver was able to load: $url")
}

private fun loadFirefoxDriver(): WebDriver {
val options = FirefoxOptions()
options.addArguments("--headless")

return FirefoxDriver(options)
}

private fun loadChromeDriver(): WebDriver {
val options = ChromeOptions()
options.addArguments("--headless=new")
options.addArguments("--use-fake-ui-for-media-stream")

return ChromeDriver(options)
}

/**
* Stops this service.
* No-op if it has not started via start() before.
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
<attach.version>4.0.17</attach.version>
<jackson.version>2.14.2</jackson.version>
<websocket.version>1.5.5</websocket.version>
<selenium.version>4.17.0</selenium.version>
<selenium.version>4.18.1</selenium.version>
<slf4j.version>2.0.6</slf4j.version>

<!-- test dependencies -->
Expand Down

0 comments on commit 96aaafd

Please sign in to comment.