forked from scala-js/scala-js-env-selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix scala-js#115: Add ESModule support
- Loading branch information
Showing
5 changed files
with
129 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 83 additions & 75 deletions
158
seleniumJSEnv/src/main/scala/org/scalajs/jsenv/selenium/JSSetup.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,80 +1,88 @@ | ||
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 = { | ||
s""" | ||
|(function() { | ||
| // Buffers for console.log / console.error | ||
| var consoleLog = []; | ||
| var consoleError = []; | ||
| | ||
| // Buffer for errors. | ||
| var errors = []; | ||
| | ||
| // Buffer for outgoing messages. | ||
| var outMessages = []; | ||
| | ||
| // Buffer for incoming messages (used if onMessage not initalized). | ||
| var inMessages = []; | ||
| | ||
| // Callback for incoming messages. | ||
| var onMessage = null; | ||
| | ||
| function captureConsole(fun, buf) { | ||
| if (!fun) return fun; | ||
| return function() { | ||
| var strs = [] | ||
| for (var i = 0; i < arguments.length; ++i) | ||
| strs.push(String(arguments[i])); | ||
| | ||
| buf.push(strs.join(" ")); | ||
| return fun.apply(this, arguments); | ||
| } | ||
| } | ||
| | ||
| console.log = captureConsole(console.log, consoleLog); | ||
| console.error = captureConsole(console.error, consoleError); | ||
| | ||
| window.addEventListener('error', function(e) { | ||
| errors.push(e.message) | ||
| }); | ||
| | ||
| if ($enableCom) { | ||
| this.scalajsCom = { | ||
| init: function(onMsg) { | ||
| onMessage = onMsg; | ||
| window.setTimeout(function() { | ||
| for (var m in inMessages) | ||
| onMessage(inMessages[m]); | ||
| inMessages = null; | ||
| }); | ||
| }, | ||
| send: function(msg) { outMessages.push(msg); } | ||
| } | ||
| } | ||
| | ||
| this.scalajsSeleniumInternalInterface = { | ||
| fetch: function() { | ||
| var res = { | ||
| consoleLog: consoleLog.slice(), | ||
| consoleError: consoleError.slice(), | ||
| errors: errors.slice(), | ||
| msgs: outMessages.slice() | ||
| } | ||
| | ||
| consoleLog.length = 0; | ||
| consoleError.length = 0; | ||
| errors.length = 0; | ||
| outMessages.length = 0; | ||
| | ||
| return res; | ||
| }, | ||
| send: function(msg) { | ||
| if (inMessages !== null) inMessages.push(msg); | ||
| else onMessage(msg); | ||
| } | ||
| }; | ||
|}).call(this) | ||
""".stripMargin | ||
def setupFile(enableCom: Boolean): Path = { | ||
Files.write( | ||
Jimfs.newFileSystem().getPath("setup.js"), | ||
s""" | ||
|(function() { | ||
| // Buffers for console.log / console.error | ||
| var consoleLog = []; | ||
| var consoleError = []; | ||
| | ||
| // Buffer for errors. | ||
| var errors = []; | ||
| | ||
| // Buffer for outgoing messages. | ||
| var outMessages = []; | ||
| | ||
| // Buffer for incoming messages (used if onMessage not initalized). | ||
| var inMessages = []; | ||
| | ||
| // Callback for incoming messages. | ||
| var onMessage = null; | ||
| | ||
| function captureConsole(fun, buf) { | ||
| if (!fun) return fun; | ||
| return function() { | ||
| var strs = [] | ||
| for (var i = 0; i < arguments.length; ++i) | ||
| strs.push(String(arguments[i])); | ||
| | ||
| buf.push(strs.join(" ")); | ||
| return fun.apply(this, arguments); | ||
| } | ||
| } | ||
| | ||
| console.log = captureConsole(console.log, consoleLog); | ||
| console.error = captureConsole(console.error, consoleError); | ||
| | ||
| window.addEventListener('error', function(e) { | ||
| errors.push(e.message) | ||
| }); | ||
| | ||
| if ($enableCom) { | ||
| this.scalajsCom = { | ||
| init: function(onMsg) { | ||
| onMessage = onMsg; | ||
| window.setTimeout(function() { | ||
| for (var m in inMessages) | ||
| onMessage(inMessages[m]); | ||
| inMessages = null; | ||
| }); | ||
| }, | ||
| send: function(msg) { outMessages.push(msg); } | ||
| } | ||
| } | ||
| | ||
| this.scalajsSeleniumInternalInterface = { | ||
| fetch: function() { | ||
| var res = { | ||
| consoleLog: consoleLog.slice(), | ||
| consoleError: consoleError.slice(), | ||
| errors: errors.slice(), | ||
| msgs: outMessages.slice() | ||
| } | ||
| | ||
| consoleLog.length = 0; | ||
| consoleError.length = 0; | ||
| errors.length = 0; | ||
| outMessages.length = 0; | ||
| | ||
| return res; | ||
| }, | ||
| send: function(msg) { | ||
| if (inMessages !== null) inMessages.push(msg); | ||
| else onMessage(msg); | ||
| } | ||
| }; | ||
|}).call(this) | ||
""".stripMargin.getBytes(StandardCharsets.UTF_8) | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
seleniumJSHttpEnvTest/src/main/scala/org/scalajs/jsenv/selenium/CamelCase.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
10 changes: 10 additions & 0 deletions
10
seleniumJSHttpEnvTest/src/test/scala/org/scalajs/jsenv/selenium/ModuleSupportTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) | ||
} | ||
} |