Skip to content

Commit

Permalink
Updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
darkfrog26 committed Nov 15, 2024
1 parent 8bdabf3 commit 78b2917
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "robobrowser"
organization := "com.outr"
version := "1.7.2"
version := "1.7.3-SNAPSHOT"

val scala213: String = "2.13.15"

Expand Down Expand Up @@ -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"
Expand Down
6 changes: 4 additions & 2 deletions src/main/scala/com/outr/robobrowser/RoboBrowser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down
64 changes: 64 additions & 0 deletions src/main/scala/com/outr/robobrowser/WebStorageUtil.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 78b2917

Please sign in to comment.