diff --git a/build.sbt b/build.sbt index c5b5d41..5aba038 100644 --- a/build.sbt +++ b/build.sbt @@ -1,6 +1,6 @@ name := "robobrowser" organization := "com.outr" -version := "1.7.2" +version := "1.7.3-SNAPSHOT" val scala213: String = "2.13.15" @@ -58,7 +58,7 @@ libraryDependencies ++= Seq( "org.seleniumhq.selenium" % "selenium-remote-driver" % seleniumVersion, "org.seleniumhq.selenium" % "htmlunit-driver" % "4.13.0", "org.seleniumhq.selenium" % "selenium-support" % seleniumVersion, - "org.seleniumhq.selenium" % "selenium-devtools-v120" % seleniumVersion, + "org.seleniumhq.selenium" % "selenium-devtools-v130" % seleniumVersion, "com.lihaoyi" %% "sourcecode" % sourcecodeVersion, "com.fifesoft" % "rsyntaxtextarea" % rsyntaxtextareaVersion, "org.scalatest" %% "scalatest" % scalatestVersion % "test" diff --git a/src/main/scala/com/outr/robobrowser/RoboBrowser.scala b/src/main/scala/com/outr/robobrowser/RoboBrowser.scala index 4daae03..21e6f1e 100644 --- a/src/main/scala/com/outr/robobrowser/RoboBrowser.scala +++ b/src/main/scala/com/outr/robobrowser/RoboBrowser.scala @@ -26,8 +26,8 @@ import fabric.define.DefType import fabric.io.{JsonFormatter, JsonParser} import fabric.rw._ import org.openqa.selenium.devtools.{DevTools, HasDevTools} -import org.openqa.selenium.devtools.v120.network.Network -import org.openqa.selenium.devtools.v120.network.model.{RequestWillBeSent, ResponseReceived} +import org.openqa.selenium.devtools.v130.network.Network +import org.openqa.selenium.devtools.v130.network.model.{RequestWillBeSent, ResponseReceived} import spice.http.cookie.SameSite import spice.http.cookie.{Cookie => SpiceCookie} import spice.net.URL @@ -46,6 +46,7 @@ abstract class RoboBrowser(val capabilities: Capabilities) extends AbstractEleme // TODO: Figure out a way to support this with Appium // protected final lazy val _driver: Driver = new EventFiringDecorator[Driver](listener).decorate(createDriver()) protected final lazy val _driver: Driver = createDriver() + protected final lazy val webStorage: WebStorageUtil = WebStorageUtil(_driver) private lazy val mainContext: Context = scs(scs => { scs @@ -491,6 +492,7 @@ abstract class RoboBrowser(val capabilities: Capabilities) extends AbstractEleme } } + // TODO: Migrate to using WebStorageUtil object localStorage { private def ls[Return](f: LocalStorage => Return): Return = withDriver { driver => f(driver.asInstanceOf[WebStorage].getLocalStorage) diff --git a/src/main/scala/com/outr/robobrowser/WebStorageUtil.scala b/src/main/scala/com/outr/robobrowser/WebStorageUtil.scala new file mode 100644 index 0000000..afea8f6 --- /dev/null +++ b/src/main/scala/com/outr/robobrowser/WebStorageUtil.scala @@ -0,0 +1,64 @@ +package com.outr.robobrowser + +import org.openqa.selenium.{JavascriptExecutor, WebDriver} +import scala.jdk.CollectionConverters._ + +case class WebStorageUtil(driver: WebDriver) { + private val jsExecutor = driver.asInstanceOf[JavascriptExecutor] + + // Set an item in localStorage + def setLocalStorageItem(key: String, value: String): Unit = { + jsExecutor.executeScript(s"window.localStorage.setItem('$key','$value');") + } + + // Get an item from localStorage + def getLocalStorageItem(key: String): Option[String] = { + Option(jsExecutor.executeScript(s"return window.localStorage.getItem('$key');").asInstanceOf[String]) + } + + // Remove an item from localStorage + def removeLocalStorageItem(key: String): Unit = { + jsExecutor.executeScript(s"window.localStorage.removeItem('$key');") + } + + // Clear all items from localStorage + def clearLocalStorage(): Unit = { + jsExecutor.executeScript("window.localStorage.clear();") + } + + // Get all keys from localStorage + def localStorageKeys: Set[String] = { + val keys = jsExecutor.executeScript( + "return Object.keys(window.localStorage);" + ).asInstanceOf[java.util.List[String]] + keys.asScala.toSet + } + + // Set an item in sessionStorage + def setSessionStorageItem(key: String, value: String): Unit = { + jsExecutor.executeScript(s"window.sessionStorage.setItem('$key','$value');") + } + + // Get an item from sessionStorage + def getSessionStorageItem(key: String): Option[String] = { + Option(jsExecutor.executeScript(s"return window.sessionStorage.getItem('$key');").asInstanceOf[String]) + } + + // Remove an item from sessionStorage + def removeSessionStorageItem(key: String): Unit = { + jsExecutor.executeScript(s"window.sessionStorage.removeItem('$key');") + } + + // Clear all items from sessionStorage + def clearSessionStorage(): Unit = { + jsExecutor.executeScript("window.sessionStorage.clear();") + } + + // Get all keys from sessionStorage + def sessionStorageKeys: Set[String] = { + val keys = jsExecutor.executeScript( + "return Object.keys(window.sessionStorage);" + ).asInstanceOf[java.util.List[String]] + keys.asScala.toSet + } +} \ No newline at end of file diff --git a/src/main/scala/com/outr/robobrowser/browser/chrome/Chrome.scala b/src/main/scala/com/outr/robobrowser/browser/chrome/Chrome.scala index 9ef7687..f74212f 100644 --- a/src/main/scala/com/outr/robobrowser/browser/chrome/Chrome.scala +++ b/src/main/scala/com/outr/robobrowser/browser/chrome/Chrome.scala @@ -2,8 +2,8 @@ package com.outr.robobrowser.browser.chrome import com.outr.robobrowser.RoboBrowser import org.openqa.selenium.chrome.{ChromeDriver, ChromeOptions => SeleniumChromeOptions} -import org.openqa.selenium.devtools.v120.network.Network -import org.openqa.selenium.devtools.v120.network.model.Headers +import org.openqa.selenium.devtools.v130.network.Network +import org.openqa.selenium.devtools.v130.network.model.Headers import java.io.{File, FileNotFoundException} import java.util