내맘대로 만들어보는 Playwright Scope

aosamesan·2022년 12월 12일
0

내가 쓰려고 만드는 내맘대로 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()
  }
}
profile
재미로 개발 하는 사람

0개의 댓글