jeudi 2 novembre 2017

How to interface Google Go with Chrome?

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, place test into the global namespace
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 calls
console.log(test.cheesePrice("swiss")); //-> 6.49

Given the above, how can I within a Go script, invoke test.cheesePrice("swiss"), and receive the return value? Something like this (pseudocoded);

package main
import "fmt"
// 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() {
    fmt.getJsCheesePrice("cheddar"); //-> 9.99
}

Aucun commentaire:

Enregistrer un commentaire