Skip to content

Commit 032158d

Browse files
committed
rework api Playwright JsEnvConfig
1 parent fab181a commit 032158d

File tree

5 files changed

+241
-40
lines changed

5 files changed

+241
-40
lines changed

libs/scalajslib/api/src/mill/scalajslib/worker/api/JsEnvConfig.scala

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,65 @@ private[scalajslib] object JsEnvConfig {
4040
}
4141

4242
final case class Playwright(
43-
browserName: String = "chromium",
44-
headless: Boolean = true,
45-
showLogs: Boolean = false,
46-
debug: Boolean = false,
47-
// pwConfig: Config = Config(),
48-
runConfigEnv: Map[String, String] = Map.empty,
49-
launchOptions: List[String] = Nil,
50-
additionalLaunchOptions: List[String] = Nil
43+
capabilities: Playwright.Capabilities
5144
) extends JsEnvConfig
45+
object Playwright {
46+
sealed trait Capabilities
47+
case class ChromeOptions(
48+
headless: Boolean = true,
49+
showLogs: Boolean = false,
50+
debug: Boolean = false,
51+
launchOptions: List[String] = List(
52+
"--disable-extensions",
53+
"--disable-web-security",
54+
"--allow-running-insecure-content",
55+
"--disable-site-isolation-trials",
56+
"--allow-file-access-from-files",
57+
"--disable-gpu"
58+
)
59+
) extends Capabilities:
60+
61+
def addLaunchOptions(options: List[String]): ChromeOptions =
62+
copy(launchOptions = (launchOptions ++ options).distinct)
63+
64+
object ChromeOptions:
65+
val default = ChromeOptions()
66+
67+
case class FirefoxOptions(
68+
headless: Boolean = true,
69+
showLogs: Boolean = false,
70+
debug: Boolean = false,
71+
firefoxUserPrefs: Map[String, String | Double | Boolean] = Map(
72+
"security.mixed_content.block_active_content" -> false,
73+
"security.mixed_content.upgrade_display_content" -> false,
74+
"security.file_uri.strict_origin_policy" -> false
75+
)
76+
) extends Capabilities:
77+
78+
def addFirefoxUserPrefs(options: Map[String, String | Double | Boolean])
79+
: FirefoxOptions =
80+
copy(firefoxUserPrefs = (firefoxUserPrefs ++ options))
81+
82+
object FirefoxOptions:
83+
val default = FirefoxOptions()
84+
85+
case class WebkitOptions(
86+
headless: Boolean = true,
87+
showLogs: Boolean = false,
88+
debug: Boolean = false,
89+
launchOptions: List[String] = List(
90+
"--disable-extensions",
91+
"--disable-web-security",
92+
"--allow-running-insecure-content",
93+
"--disable-site-isolation-trials",
94+
"--allow-file-access-from-files"
95+
)
96+
) extends Capabilities:
97+
98+
def addLaunchOptions(options: List[String]): WebkitOptions =
99+
copy(launchOptions = (launchOptions ++ options).distinct)
100+
101+
object WebkitOptions:
102+
val default = WebkitOptions()
103+
}
52104
}

libs/scalajslib/src/mill/scalajslib/api/JsEnvConfig.scala

Lines changed: 135 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,139 @@ object JsEnvConfig {
118118
}
119119
}
120120

121-
final case class Playwright(
122-
browserName: String = "chromium",
123-
headless: Boolean = true,
124-
showLogs: Boolean = false,
125-
debug: Boolean = false,
126-
// pwConfig: Config = Config(),
127-
runConfigEnv: Map[String, String] = Map.empty,
128-
launchOptions: List[String] = Nil,
129-
additionalLaunchOptions: List[String] = Nil
130-
) extends JsEnvConfig
121+
final class Playwright private (val capabilities: Playwright.Capabilities) extends JsEnvConfig
122+
object Playwright {
123+
implicit def rwCapabilities: RW[Capabilities] = macroRW
124+
125+
private given Root_Capabilities: Mirrors.Root[Capabilities] =
126+
Mirrors.autoRoot[Capabilities]
127+
128+
def apply(capabilities: Capabilities): Playwright =
129+
new Playwright(capabilities = capabilities)
130+
131+
sealed trait Capabilities
132+
133+
val defaultChromeLaunchOptions = List(
134+
"--disable-extensions",
135+
"--disable-web-security",
136+
"--allow-running-insecure-content",
137+
"--disable-site-isolation-trials",
138+
"--allow-file-access-from-files",
139+
"--disable-gpu"
140+
)
141+
142+
def chrome(
143+
headless: Boolean = true,
144+
showLogs: Boolean = false,
145+
debug: Boolean = false,
146+
launchOptions: List[String] = defaultChromeLaunchOptions
147+
): Playwright =
148+
val options = ChromeOptions(
149+
headless = headless,
150+
showLogs = showLogs,
151+
debug = debug,
152+
launchOptions = launchOptions
153+
)
154+
new Playwright(options)
155+
156+
case class ChromeOptions(
157+
headless: Boolean = true,
158+
showLogs: Boolean = false,
159+
debug: Boolean = false,
160+
launchOptions: List[String] = defaultChromeLaunchOptions
161+
) extends Capabilities {
162+
def withHeadless(value: Boolean): ChromeOptions = copy(headless = value)
163+
def withShowLogs(value: Boolean): ChromeOptions = copy(showLogs = value)
164+
def withDebug(value: Boolean): ChromeOptions = copy(debug = value)
165+
def withLaunchOptions(value: List[String]): ChromeOptions = copy(launchOptions = value)
166+
}
167+
object ChromeOptions:
168+
implicit def rw: RW[ChromeOptions] = macroRW
169+
170+
val defaultFirefoxUserPrefs: Map[String, String | Double | Boolean] =
171+
Map(
172+
"security.mixed_content.block_active_content" -> false,
173+
"security.mixed_content.upgrade_display_content" -> false,
174+
"security.file_uri.strict_origin_policy" -> false
175+
)
176+
177+
def firefox(
178+
headless: Boolean = true,
179+
showLogs: Boolean = false,
180+
debug: Boolean = false,
181+
firefoxUserPrefs: Map[String, String | Double | Boolean] = defaultFirefoxUserPrefs
182+
): Playwright =
183+
val options = FirefoxOptions(
184+
headless = headless,
185+
showLogs = showLogs,
186+
debug = debug,
187+
firefoxUserPrefs = firefoxUserPrefs
188+
)
189+
new Playwright(options)
190+
case class FirefoxOptions(
191+
headless: Boolean = true,
192+
showLogs: Boolean = false,
193+
debug: Boolean = false,
194+
firefoxUserPrefs: Map[String, String | Double | Boolean] = defaultFirefoxUserPrefs
195+
) extends Capabilities {
196+
def withHeadless(value: Boolean): FirefoxOptions = copy(headless = value)
197+
def withShowLogs(value: Boolean): FirefoxOptions = copy(showLogs = value)
198+
def withDebug(value: Boolean): FirefoxOptions = copy(debug = value)
199+
def withFirefoxUserPrefs(value: Map[String, String | Double | Boolean]): FirefoxOptions =
200+
copy(firefoxUserPrefs = value)
201+
}
202+
object FirefoxOptions:
203+
given upickle.default.ReadWriter[String | Double | Boolean] =
204+
upickle.default.readwriter[ujson.Value].bimap[String | Double | Boolean](
205+
{
206+
case v: Boolean => upickle.default.writeJs(v)
207+
case v: Double => upickle.default.writeJs(v)
208+
case v: String => upickle.default.writeJs(v)
209+
},
210+
json =>
211+
json.boolOpt
212+
.orElse(
213+
json.numOpt
214+
).orElse(
215+
json.strOpt.map(_.toString)
216+
).getOrElse(throw new Exception("Invalid value"))
217+
)
218+
given rw: RW[FirefoxOptions] = macroRW
219+
220+
val defaultWebkitLaunchOptions = List(
221+
"--disable-extensions",
222+
"--disable-web-security",
223+
"--allow-running-insecure-content",
224+
"--disable-site-isolation-trials",
225+
"--allow-file-access-from-files"
226+
)
227+
228+
def webkit(
229+
headless: Boolean = true,
230+
showLogs: Boolean = false,
231+
debug: Boolean = false,
232+
launchOptions: List[String] = defaultWebkitLaunchOptions
233+
): Playwright =
234+
val options = WebkitOptions(
235+
headless = headless,
236+
showLogs = showLogs,
237+
debug = debug,
238+
launchOptions = launchOptions
239+
)
240+
new Playwright(options)
241+
242+
case class WebkitOptions(
243+
headless: Boolean = true,
244+
showLogs: Boolean = false,
245+
debug: Boolean = false,
246+
launchOptions: List[String] = defaultWebkitLaunchOptions
247+
) extends Capabilities {
248+
def withHeadless(value: Boolean): WebkitOptions = copy(headless = value)
249+
def withShowLogs(value: Boolean): WebkitOptions = copy(showLogs = value)
250+
def withDebug(value: Boolean): WebkitOptions = copy(debug = value)
251+
def withLaunchOptions(value: List[String]): WebkitOptions = copy(launchOptions = value)
252+
}
253+
object WebkitOptions:
254+
implicit def rw: RW[WebkitOptions] = macroRW
255+
}
131256
}

libs/scalajslib/src/mill/scalajslib/worker/ScalaJSWorker.scala

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,29 @@ private[scalajslib] class ScalaJSWorker(jobs: Int)
110110
}
111111
)
112112
case config: api.JsEnvConfig.Playwright =>
113-
workerApi.JsEnvConfig.Playwright(
114-
browserName = config.browserName,
115-
headless = config.headless,
116-
showLogs = config.showLogs,
117-
debug = config.debug,
118-
// pwConfig = config.pwConfig,
119-
runConfigEnv = config.runConfigEnv,
120-
launchOptions = config.launchOptions,
121-
additionalLaunchOptions = config.additionalLaunchOptions
122-
)
113+
val options = config.capabilities match
114+
case options: api.JsEnvConfig.Playwright.ChromeOptions =>
115+
workerApi.JsEnvConfig.Playwright.ChromeOptions(
116+
headless = options.headless,
117+
showLogs = options.showLogs,
118+
debug = options.debug,
119+
launchOptions = options.launchOptions
120+
)
121+
case options: api.JsEnvConfig.Playwright.FirefoxOptions =>
122+
workerApi.JsEnvConfig.Playwright.FirefoxOptions(
123+
headless = options.headless,
124+
showLogs = options.showLogs,
125+
debug = options.debug,
126+
firefoxUserPrefs = options.firefoxUserPrefs
127+
)
128+
case options: api.JsEnvConfig.Playwright.WebkitOptions =>
129+
workerApi.JsEnvConfig.Playwright.WebkitOptions(
130+
headless = options.headless,
131+
showLogs = options.showLogs,
132+
debug = options.debug,
133+
launchOptions = options.launchOptions
134+
)
135+
workerApi.JsEnvConfig.Playwright(options)
123136
}
124137
}
125138

libs/scalajslib/worker/1/src/mill/scalajslib/worker/jsenv/Playwright.scala

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,26 @@ package mill.scalajslib.worker.jsenv
33
import mill.scalajslib.worker.api._
44

55
object Playwright {
6-
def apply(config: JsEnvConfig.Playwright) =
7-
new io.github.thijsbroersen.jsenv.playwright.PWEnv(
8-
browserName = config.browserName,
9-
headless = config.headless,
10-
showLogs = config.showLogs,
11-
debug = config.debug,
12-
// pwConfig = config.pwConfig,
13-
runConfigEnv = config.runConfigEnv,
14-
launchOptions = config.launchOptions,
15-
additionalLaunchOptions = config.additionalLaunchOptions
16-
)
6+
def apply(config: JsEnvConfig.Playwright) = config.capabilities match
7+
case options: JsEnvConfig.Playwright.ChromeOptions =>
8+
io.github.thijsbroersen.jsenv.playwright.PlaywrightJSEnv.chrome(
9+
headless = options.headless,
10+
showLogs = options.showLogs,
11+
debug = options.debug,
12+
launchOptions = options.launchOptions
13+
)
14+
case options: JsEnvConfig.Playwright.FirefoxOptions =>
15+
io.github.thijsbroersen.jsenv.playwright.PlaywrightJSEnv.firefox(
16+
headless = options.headless,
17+
showLogs = options.showLogs,
18+
debug = options.debug,
19+
firefoxUserPrefs = options.firefoxUserPrefs
20+
)
21+
case options: JsEnvConfig.Playwright.WebkitOptions =>
22+
io.github.thijsbroersen.jsenv.playwright.PlaywrightJSEnv.webkit(
23+
headless = options.headless,
24+
showLogs = options.showLogs,
25+
debug = options.debug,
26+
launchOptions = options.launchOptions
27+
)
1728
}

mill-build/src/millbuild/Deps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ object Deps {
3333
val scalajsEnvSelenium =
3434
mvn"org.scala-js::scalajs-env-selenium:1.1.1".withDottyCompat(scalaVersion)
3535
val scalajsEnvPlaywright =
36-
mvn"io.github.thijsbroersen::scala-js-env-playwright:0.2.2"
36+
mvn"io.github.thijsbroersen::scala-js-env-playwright:0.2.3"
3737
val scalajsSbtTestAdapter =
3838
mvn"org.scala-js::scalajs-sbt-test-adapter:${scalaJsVersion}".withDottyCompat(scalaVersion)
3939
val scalajsLinker =

0 commit comments

Comments
 (0)