From 4ac4518a406ecb5ee9d966c90bcf582b275ae742 Mon Sep 17 00:00:00 2001 From: mushtaq Date: Sun, 27 Sep 2020 12:47:23 +0530 Subject: [PATCH 1/2] Upgrade versions As mentioned in the [ScalaJS 1.1.1 release](http://www.scala-js.org/news/2020/07/02/announcing-scalajs-1.1.1), ScalaJS version in `scalajs-js-envs` should be harcoded instead of deriving it from `scalaJSVersion`. `scalajs-js-envs` is extracted out of the ScalaJS repository and will follow its own publishing cadence. --- build.sbt | 4 ++-- project/build.sbt | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build.sbt b/build.sbt index f723cbf..5795d8f 100644 --- a/build.sbt +++ b/build.sbt @@ -78,8 +78,8 @@ lazy val seleniumJSEnv: Project = project. * guava stuff which in turn makes selenium fail. */ "org.seleniumhq.selenium" % "selenium-server" % "3.141.59", - "org.scala-js" %% "scalajs-js-envs" % scalaJSVersion, - "org.scala-js" %% "scalajs-js-envs-test-kit" % scalaJSVersion % "test", + "org.scala-js" %% "scalajs-js-envs" % "1.1.1", + "org.scala-js" %% "scalajs-js-envs-test-kit" % "1.1.1" % "test", "com.novocode" % "junit-interface" % "0.11" % "test" ), diff --git a/project/build.sbt b/project/build.sbt index 5f14175..147d851 100644 --- a/project/build.sbt +++ b/project/build.sbt @@ -1,4 +1,4 @@ -addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.0.0") +addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.2.0") addSbtPlugin("com.typesafe" % "sbt-mima-plugin" % "0.1.18") From f9645253edf918330fd93cc779d7fd484ea64381 Mon Sep 17 00:00:00 2001 From: mushtaq Date: Sun, 27 Sep 2020 13:02:19 +0530 Subject: [PATCH 2/2] Fix #115: Add ESModule support --- build.sbt | 4 ++- .../org/scalajs/jsenv/selenium/JSSetup.scala | 14 +++++++- .../scalajs/jsenv/selenium/SeleniumRun.scala | 34 ++++++++++--------- .../scalajs/jsenv/selenium/CamelCase.scala | 12 +++++++ .../jsenv/selenium/ModuleSupportTest.scala | 10 ++++++ 5 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 seleniumJSHttpEnvTest/src/main/scala/org/scalajs/jsenv/selenium/CamelCase.scala create mode 100644 seleniumJSHttpEnvTest/src/test/scala/org/scalajs/jsenv/selenium/ModuleSupportTest.scala diff --git a/build.sbt b/build.sbt index 5795d8f..5ccc997 100644 --- a/build.sbt +++ b/build.sbt @@ -79,6 +79,7 @@ lazy val seleniumJSEnv: Project = project. */ "org.seleniumhq.selenium" % "selenium-server" % "3.141.59", "org.scala-js" %% "scalajs-js-envs" % "1.1.1", + "com.google.jimfs" % "jimfs" % "1.1", "org.scala-js" %% "scalajs-js-envs-test-kit" % "1.1.1" % "test", "com.novocode" % "junit-interface" % "0.11" % "test" ), @@ -135,5 +136,6 @@ lazy val seleniumJSHttpEnvTest: Project = project. SeleniumJSEnv.Config() .withMaterializeInServer("tmp", "http://localhost:8080/tmp/") ) - } + }, + scalaJSLinkerConfig ~= { _.withModuleKind(ModuleKind.ESModule) } ) diff --git a/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/JSSetup.scala b/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/JSSetup.scala index 0b30772..512a982 100644 --- a/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/JSSetup.scala +++ b/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/JSSetup.scala @@ -1,7 +1,18 @@ package org.scalajs.jsenv.selenium +import java.nio.charset.StandardCharsets +import java.nio.file.{Files, Path} + +import com.google.common.jimfs.Jimfs + private[selenium] object JSSetup { - def setupCode(enableCom: Boolean): String = { + def setupFile(enableCom: Boolean): Path = { + val path = Jimfs.newFileSystem().getPath("setup.js") + val contents = setupCode(enableCom).getBytes(StandardCharsets.UTF_8) + Files.write(path, contents) + } + + private def setupCode(enableCom: Boolean): String = { s""" |(function() { | // Buffers for console.log / console.error @@ -77,4 +88,5 @@ private[selenium] object JSSetup { |}).call(this) """.stripMargin } + } diff --git a/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/SeleniumRun.scala b/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/SeleniumRun.scala index 3c08ce5..6b39076 100644 --- a/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/SeleniumRun.scala +++ b/seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/SeleniumRun.scala @@ -10,6 +10,8 @@ import scala.util.control.NonFatal import java.util.concurrent.{ConcurrentLinkedQueue, Executors} import java.util.function.Consumer +import java.nio.file.Path +import java.net.URL private sealed class SeleniumRun( driver: WebDriver with JavascriptExecutor, @@ -129,20 +131,11 @@ private[selenium] object SeleniumRun { newRun: Ctor[T], failed: Throwable => T): T = { validator.validate(runConfig) - val scripts = input.map { - case Input.Script(s) => s - case _ => throw new UnsupportedInputException(input) - } - try { withCleanup(FileMaterializer(config.materialization))(_.close()) { m => - val allScriptURLs = ( - m.materialize("setup.js", JSSetup.setupCode(enableCom)) +: - scripts.map(m.materialize) - ) - - val page = m.materialize("scalajsRun.html", htmlPage(allScriptURLs)) - + val setupJsScript = Input.Script(JSSetup.setupFile(enableCom)) + val fullInput = setupJsScript +: input + val page = m.materialize("scalajsRun.html", htmlPage(fullInput, m)) withCleanup(newDriver())(maybeCleanupDriver(_, config)) { driver => driver.navigate().to(page) @@ -171,18 +164,27 @@ private[selenium] object SeleniumRun { private def maybeCleanupDriver(d: WebDriver, config: SeleniumJSEnv.Config) = if (!config.keepAlive) d.close() - private def htmlPage(scripts: Seq[java.net.URL]): String = { - val scriptTags = - scripts.map(path => s"") + private def htmlPage(fullInput: Seq[Input], materializer: FileMaterializer): String = { + val tags = fullInput.map { + case Input.Script(path) => makeTag(path, "text/javascript", materializer) + case Input.ESModule(path) => makeTag(path, "module", materializer) + case _ => throw new UnsupportedInputException(fullInput) + } + s""" | | - | ${scriptTags.mkString("\n ")} + | ${tags.mkString("\n ")} | | """.stripMargin } + private def makeTag(path: Path, tpe: String, materializer: FileMaterializer): String = { + val url = materializer.materialize(path) + s"" + } + private class WindowOnErrorException(errs: List[String]) extends Exception(s"JS error: $errs") private def consumer[A](f: A => Unit): Consumer[A] = new Consumer[A] { diff --git a/seleniumJSHttpEnvTest/src/main/scala/org/scalajs/jsenv/selenium/CamelCase.scala b/seleniumJSHttpEnvTest/src/main/scala/org/scalajs/jsenv/selenium/CamelCase.scala new file mode 100644 index 0000000..1020844 --- /dev/null +++ b/seleniumJSHttpEnvTest/src/main/scala/org/scalajs/jsenv/selenium/CamelCase.scala @@ -0,0 +1,12 @@ +package org.scalajs.jsenv.selenium + +import scala.scalajs.js +import scala.scalajs.js.annotation.JSImport + +object CamelCase { + def hello(input: String): String = s"Hello ${camelCase(input)}!" + + @JSImport("https://cdn.skypack.dev/camelcase@^6.0.0", JSImport.Default) + @js.native + def camelCase(input: String): String = js.native +} diff --git a/seleniumJSHttpEnvTest/src/test/scala/org/scalajs/jsenv/selenium/ModuleSupportTest.scala b/seleniumJSHttpEnvTest/src/test/scala/org/scalajs/jsenv/selenium/ModuleSupportTest.scala new file mode 100644 index 0000000..86630f8 --- /dev/null +++ b/seleniumJSHttpEnvTest/src/test/scala/org/scalajs/jsenv/selenium/ModuleSupportTest.scala @@ -0,0 +1,10 @@ +package org.scalajs.jsenv.selenium + +import org.junit.Assert._ +import org.junit.Test + +class ModuleSupportTest { + @Test def testBasicImport(): Unit = { + assertEquals("Hello scalaJsSelenium!", CamelCase.hello("scala js selenium")) + } +}