Skip to content

Commit 4f6b001

Browse files
feat: allow for custom launchOptions on PWEnv (#13)
1 parent f9fd433 commit 4f6b001

File tree

9 files changed

+109
-74
lines changed

9 files changed

+109
-74
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ project/plugins/project/
1919
.idea
2020
.metals
2121
.lh
22+
.bloop
2223
.bsp
2324
.vscode
2425
.DS_Store

src/main/scala/jsenv/playwright/CEComRun.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class CEComRun(
1616
override val pwConfig: Config,
1717
override val runConfig: RunConfig,
1818
override val input: Seq[Input],
19+
override val launchOptions: List[String],
20+
override val additionalLaunchOptions: List[String],
1921
onMessage: String => Unit
2022
) extends JSComRun
2123
with Runner {
@@ -27,7 +29,7 @@ class CEComRun(
2729
override protected def receivedMessage(msg: String): Unit = onMessage(msg)
2830

2931
lazy val future: Future[Unit] =
30-
jsRunPrg(browserName, headless, isComEnabled = true, None)
32+
jsRunPrg(browserName, headless, isComEnabled = true, pwLaunchOptions)
3133
.use(_ => IO.unit)
3234
.unsafeToFuture()
3335

src/main/scala/jsenv/playwright/CERun.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ class CERun(
1414
override val headless: Boolean,
1515
override val pwConfig: Config,
1616
override val runConfig: RunConfig,
17-
override val input: Seq[Input]
17+
override val input: Seq[Input],
18+
override val launchOptions: List[String],
19+
override val additionalLaunchOptions: List[String]
1820
) extends JSRun
1921
with Runner {
2022
scribe.debug(s"Creating CERun for $browserName")
2123
lazy val future: Future[Unit] =
22-
jsRunPrg(browserName, headless, isComEnabled = false, None)
24+
jsRunPrg(browserName, headless, isComEnabled = false, pwLaunchOptions)
2325
.use(_ => IO.unit)
2426
.unsafeToFuture()
2527

src/main/scala/jsenv/playwright/PWEnv.scala

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,32 @@ import java.nio.file.Path
99
import java.nio.file.Paths
1010
import scala.util.control.NonFatal
1111

12+
/**
13+
* Playwright JS environment
14+
*
15+
* @param browserName
16+
* browser name, options are "chromium", "chrome", "firefox", "webkit", default is "chromium"
17+
* @param headless
18+
* headless mode, default is true
19+
* @param showLogs
20+
* show logs, default is false
21+
* @param debug
22+
* debug mode, default is false
23+
* @param pwConfig
24+
* Playwright configuration
25+
* @param launchOptions
26+
* override launch options, if not provided default launch options are used
27+
* @param additionalLaunchOptions
28+
* additional launch options (added to (default) launch options)
29+
*/
1230
class PWEnv(
1331
browserName: String = "chromium",
1432
headless: Boolean = true,
1533
showLogs: Boolean = false,
1634
debug: Boolean = false,
17-
pwConfig: Config = Config()
35+
pwConfig: Config = Config(),
36+
launchOptions: List[String] = Nil,
37+
additionalLaunchOptions: List[String] = Nil
1838
) extends JSEnv {
1939

2040
private lazy val validator = {
@@ -27,7 +47,14 @@ class PWEnv(
2747
override def start(input: Seq[Input], runConfig: RunConfig): JSRun = {
2848
try {
2949
validator.validate(runConfig)
30-
new CERun(browserName, headless, pwConfig, runConfig, input)
50+
new CERun(
51+
browserName,
52+
headless,
53+
pwConfig,
54+
runConfig,
55+
input,
56+
launchOptions,
57+
additionalLaunchOptions)
3158
} catch {
3259
case ve: java.lang.IllegalArgumentException =>
3360
scribe.error(s"CEEnv.startWithCom failed with throw ve $ve")
@@ -51,6 +78,8 @@ class PWEnv(
5178
pwConfig,
5279
runConfig,
5380
input,
81+
launchOptions,
82+
additionalLaunchOptions,
5483
onMessage
5584
)
5685
} catch {
@@ -66,13 +95,11 @@ class PWEnv(
6695
}
6796

6897
object PWEnv {
69-
final class Config private (val materialization: Config.Materialization) {
98+
case class Config(
99+
materialization: Config.Materialization = Config.Materialization.Temp
100+
) {
70101
import Config.Materialization
71102

72-
private def this() = this(
73-
materialization = Config.Materialization.Temp
74-
)
75-
76103
/**
77104
* Materializes purely virtual files into a temp directory.
78105
*
@@ -131,16 +158,9 @@ object PWEnv {
131158

132159
def withMaterialization(materialization: Materialization): Config =
133160
copy(materialization = materialization)
134-
135-
private def copy(
136-
materialization: Config.Materialization = materialization
137-
): Config = {
138-
new Config(materialization)
139-
}
140161
}
141162

142163
object Config {
143-
def apply(): Config = new Config()
144164

145165
abstract class Materialization private ()
146166
object Materialization {
@@ -153,4 +173,23 @@ object PWEnv {
153173
}
154174
}
155175
}
176+
177+
val chromeLaunchOptions = List(
178+
"--disable-extensions",
179+
"--disable-web-security",
180+
"--allow-running-insecure-content",
181+
"--disable-site-isolation-trials",
182+
"--allow-file-access-from-files",
183+
"--disable-gpu"
184+
)
185+
186+
val firefoxLaunchOptions = List("--disable-web-security")
187+
188+
val webkitLaunchOptions = List(
189+
"--disable-extensions",
190+
"--disable-web-security",
191+
"--allow-running-insecure-content",
192+
"--disable-site-isolation-trials",
193+
"--allow-file-access-from-files"
194+
)
156195
}

src/main/scala/jsenv/playwright/PageFactory.scala

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.microsoft.playwright.BrowserType.LaunchOptions
88
import com.microsoft.playwright.Page
99
import com.microsoft.playwright.Playwright
1010

11-
import scala.jdk.CollectionConverters.seqAsJavaListConverter
1211
object PageFactory {
1312
def pageBuilder(browser: Browser): Resource[IO, Page] = {
1413
Resource.make(IO {
@@ -22,7 +21,7 @@ object PageFactory {
2221
playwright: Playwright,
2322
browserName: String,
2423
headless: Boolean,
25-
launchOptions: Option[LaunchOptions] = None
24+
launchOptions: LaunchOptions
2625
): Resource[IO, Browser] =
2726
Resource.make(IO {
2827

@@ -35,47 +34,11 @@ object PageFactory {
3534
playwright.webkit()
3635
case _ => throw new IllegalArgumentException("Invalid browser type")
3736
}
38-
launchOptions match {
39-
case Some(launchOptions) =>
40-
browserType.launch(launchOptions.setHeadless(headless))
41-
case None =>
42-
val launchOptions = browserName.toLowerCase match {
43-
case "chromium" | "chrome" =>
44-
new BrowserType.LaunchOptions().setArgs(
45-
List(
46-
"--disable-extensions",
47-
"--disable-web-security",
48-
"--allow-running-insecure-content",
49-
"--disable-site-isolation-trials",
50-
"--allow-file-access-from-files",
51-
"--disable-gpu"
52-
).asJava
53-
)
54-
case "firefox" =>
55-
new BrowserType.LaunchOptions().setArgs(
56-
List(
57-
"--disable-web-security"
58-
).asJava
59-
)
60-
case "webkit" =>
61-
new BrowserType.LaunchOptions().setArgs(
62-
List(
63-
"--disable-extensions",
64-
"--disable-web-security",
65-
"--allow-running-insecure-content",
66-
"--disable-site-isolation-trials",
67-
"--allow-file-access-from-files"
68-
).asJava
69-
)
70-
case _ => throw new IllegalArgumentException("Invalid browser type")
71-
}
72-
val browser = browserType.launch(launchOptions.setHeadless(headless))
73-
scribe.info(
74-
s"Creating browser ${browser.browserType().name()} version ${browser
75-
.version()} with ${browser.hashCode()}"
76-
)
77-
browser
78-
}
37+
val browser = browserType.launch(launchOptions.setHeadless(headless))
38+
scribe.info(
39+
s"Creating browser ${browser.browserType().name()} version ${browser.version()} with ${browser.hashCode()}"
40+
)
41+
browser
7942
})(browser =>
8043
IO {
8144
scribe.debug(s"Closing browser with ${browser.hashCode()}")
@@ -95,7 +58,7 @@ object PageFactory {
9558
def createPage(
9659
browserName: String,
9760
headless: Boolean,
98-
launchOptions: Option[LaunchOptions]
61+
launchOptions: LaunchOptions
9962
): Resource[IO, Page] =
10063
for {
10164
playwright <- playWrightBuilder

src/main/scala/jsenv/playwright/Runner.scala

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package jsenv.playwright
22

33
import cats.effect.IO
44
import cats.effect.Resource
5+
import com.microsoft.playwright.BrowserType
56
import com.microsoft.playwright.BrowserType.LaunchOptions
67
import jsenv.playwright.PWEnv.Config
78
import jsenv.playwright.PageFactory._
@@ -12,20 +13,23 @@ import org.scalajs.jsenv.RunConfig
1213
import java.util.concurrent.ConcurrentLinkedQueue
1314
import java.util.concurrent.atomic.AtomicBoolean
1415
import scala.concurrent.duration.DurationInt
16+
import scala.jdk.CollectionConverters.seqAsJavaListConverter
1517

1618
trait Runner {
1719
val browserName: String = "" // or provide actual values
1820
val headless: Boolean = false // or provide actual values
1921
val pwConfig: Config = Config() // or provide actual values
2022
val runConfig: RunConfig = RunConfig() // or provide actual values
2123
val input: Seq[Input] = Seq.empty // or provide actual values
24+
val launchOptions: List[String] = Nil
25+
val additionalLaunchOptions: List[String] = Nil
2226

2327
// enableCom is false for CERun and true for CEComRun
2428
protected val enableCom = false
2529
protected val intf = "this.scalajsPlayWrightInternalInterface"
2630
protected val sendQueue = new ConcurrentLinkedQueue[String]
2731
// receivedMessage is called only from JSComRun. Hence its implementation is empty in CERun
28-
protected def receivedMessage(msg: String): Unit
32+
protected def receivedMessage(msg: String): Unit
2933
var wantToClose = new AtomicBoolean(false)
3034
// List of programs
3135
// 1. isInterfaceUp()
@@ -47,7 +51,7 @@ trait Runner {
4751
browserName: String,
4852
headless: Boolean,
4953
isComEnabled: Boolean,
50-
launchOptions: Option[LaunchOptions]
54+
launchOptions: LaunchOptions
5155
): Resource[IO, Unit] = for {
5256
_ <- Resource.pure(
5357
scribe.info(
@@ -123,6 +127,29 @@ trait Runner {
123127
}
124128
}
125129

130+
protected lazy val pwLaunchOptions =
131+
browserName.toLowerCase() match {
132+
case "chromium" | "chrome" =>
133+
new BrowserType.LaunchOptions().setArgs(
134+
if (launchOptions.isEmpty)
135+
(PWEnv.chromeLaunchOptions ++ additionalLaunchOptions).asJava
136+
else (launchOptions ++ additionalLaunchOptions).asJava
137+
)
138+
case "firefox" =>
139+
new BrowserType.LaunchOptions().setArgs(
140+
if (launchOptions.isEmpty)
141+
(PWEnv.firefoxLaunchOptions ++ additionalLaunchOptions).asJava
142+
else (launchOptions ++ additionalLaunchOptions).asJava
143+
)
144+
case "webkit" =>
145+
new BrowserType.LaunchOptions().setArgs(
146+
if (launchOptions.isEmpty)
147+
(PWEnv.webkitLaunchOptions ++ additionalLaunchOptions).asJava
148+
else (launchOptions ++ additionalLaunchOptions).asJava
149+
)
150+
case _ => throw new IllegalArgumentException("Invalid browser type")
151+
}
152+
126153
}
127154

128155
//private class WindowOnErrorException(errs: List[String])

src/test/scala/jsenv/playwright/PWSuiteChrome.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import org.scalajs.jsenv.test._
66
@RunWith(classOf[JSEnvSuiteRunner])
77
class PWSuiteChrome
88
extends JSEnvSuite(
9-
JSEnvSuiteConfig(new PWEnv("chrome", debug = true, headless = true))
9+
JSEnvSuiteConfig(new PWEnv("chrome", debug = false, headless = true))
1010
)

src/test/scala/jsenv/playwright/PWSuiteFirefox.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ import org.scalajs.jsenv.test._
66
@RunWith(classOf[JSEnvSuiteRunner])
77
class PWSuiteFirefox
88
extends JSEnvSuite(
9-
JSEnvSuiteConfig(new PWEnv("firefox", debug = true))
9+
JSEnvSuiteConfig(new PWEnv("firefox", debug = false, headless = true))
1010
)
Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
//package jsenv.playwright
2-
//
3-
//import org.junit.runner.RunWith
4-
//import org.scalajs.jsenv.test._
5-
//
6-
//@RunWith(classOf[JSEnvSuiteRunner])
7-
//class PWSuiteWebKit extends JSEnvSuite(
8-
// JSEnvSuiteConfig(new PWEnv("webkit", debug = true))
9-
//)
1+
// package jsenv.playwright
2+
3+
// import org.junit.runner.RunWith
4+
// import org.scalajs.jsenv.test._
5+
6+
// @RunWith(classOf[JSEnvSuiteRunner])
7+
// class PWSuiteWebKit
8+
// extends JSEnvSuite(
9+
// JSEnvSuiteConfig(new PWEnv("webkit", debug = false, headless = true))
10+
// )

0 commit comments

Comments
 (0)