내가 쓰려고 만드는 내맘대로 Playwright Scope
class PlaywrightScope(
internal val createOptions: Palywright.CreateOptions? = null,
internal val launchOptions: BrowserType.LaunchOptions? = null
)
fun playwright(block: PlaywrightScope.() -> Unit) {
PalywrightScope().block()
}
fun <T>palywright(block: PlaywrightScope.() -> T): T {
return PlaywrightScope().block()
}
fun PlaywrightScope.createOptions(block: PlaywrightScope.() -> Playwright.CreateOptions) {
createOptions = block()
}
fun PlaywrightScope.launchOptions(block: PlaywrightScope.() -> BrowserType.LaunchOptions) {
lauchOptions = block()
}
fun PlaywrightScope.executablePath(executablePath: String) {
if (launchOptions == null) {
launchOptions {
BrowserType.LaunchOptions().setExecutablePath(Path(executablePath))
}
} else {
launchOptions!!.setExecutablePath(executablePath)
}
fun PlaywrightScope.launch(block: PlaywrightScope.(browser: Browser) -> Unit) {
Playwright.create(createOptions).use { playwright ->
palywright.chromium().launch(launchOptions).use { browser ->
block(browser)
}
}
}
}
fun <T>PlaywrightScope.launch(block: PlaywrightScope.(browser: Browser) -> T): T {
return Playwright.create(createOptions).use { playwright ->
palywright.chromium().launch(launchOptions).use { browser ->
block(browser)
}
}
}
}
대충 사용 방법
playwright {
executablePath("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
launch {
page.navigate("https://google.com")
println(page.content())
}
}
val content = playwright<String> {
executablePath("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome")
launch {
page.navigate("https://google.com")
return@playwright page.content()
}
}