@@ -118,14 +118,139 @@ object JsEnvConfig {
118
118
}
119
119
}
120
120
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
+ }
131
256
}
0 commit comments