I'm trying to write a JavaScript test suite in Go.
I envision creating unit tests with Go, and running them against a Web application loaded in browser.
I've put a test
JavaScript object into the global namespace for this purpose; so, I have a "hook" into the different components that I want to test.
Is it possible to load this script in Chrome, and expose the interface to test
such that I can access in-browser JavaScript functions from Go?
Example:
// JavaScript function
// Intentionally, and cautiously, emplace "test" into the global namespace
// In practice I would make this a singleton :^D
test = (typeof test == "undefined") ? {} : test;
// Employ the method invocation pattern to append "test" with function "cheesePrice()"
// ES6 fat arrow function
test.cheesePrice = (type) => {
let p = 0;
switch (type) {
case "cheddar" : { p = 9.99; break; };
case "swiss" : { p = 6.49; break; };
case "gouda" : { p = 7.49; break; };
default : { // throw; }
}
return p;
}
// Example call
test.cheesePrice("swiss"); //-> 6.49
Given the above, how can I within a Go script, invoke test.cheesePrice(), and receive the return value? Something like this (pseudocoded);
// Imagine here browserHook is an interface into Chrome
// Enabling us to invoke the method above
func getJsCheesePrice(cheeseType string) float32 {
return browserHook.test.getPrice(cheeseType);
}
func main() {
assert.Equal(getJsCheesePrice("cheddar"), 9.99);
}
From my understanding there isn't a way to interface with javascript functions within a compiled Go program. There is however a Selenium webdriver client for Go here.
Note that this won't necessarily allow you to call javascript functions directly, but will give you an interface to a browser running the JS.