This example shows how to navigate to a http://play.golang.org page, input a short program, run it, and inspect its output. If you want to actually run this example: 1. Ensure the file paths at the top of the function are correct. 2. Remove the word "Example" from the comment at the bottom of the
()
| 20 | // 3. Run: |
| 21 | // go test -test.run=Example$ github.com/tebeka/selenium |
| 22 | func Example() { |
| 23 | // Start a Selenium WebDriver server instance (if one is not already |
| 24 | // running). |
| 25 | const ( |
| 26 | // These paths will be different on your system. |
| 27 | seleniumPath = "vendor/selenium-server-standalone-3.4.jar" |
| 28 | geckoDriverPath = "vendor/geckodriver-v0.18.0-linux64" |
| 29 | port = 8080 |
| 30 | ) |
| 31 | opts := []selenium.ServiceOption{ |
| 32 | selenium.StartFrameBuffer(), // Start an X frame buffer for the browser to run in. |
| 33 | selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox. |
| 34 | selenium.Output(os.Stderr), // Output debug information to STDERR. |
| 35 | } |
| 36 | selenium.SetDebug(true) |
| 37 | service, err := selenium.NewSeleniumService(seleniumPath, port, opts...) |
| 38 | if err != nil { |
| 39 | panic(err) // panic is used only as an example and is not otherwise recommended. |
| 40 | } |
| 41 | defer service.Stop() |
| 42 | |
| 43 | // Connect to the WebDriver instance running locally. |
| 44 | caps := selenium.Capabilities{"browserName": "firefox"} |
| 45 | wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port)) |
| 46 | if err != nil { |
| 47 | panic(err) |
| 48 | } |
| 49 | defer wd.Quit() |
| 50 | |
| 51 | // Navigate to the simple playground interface. |
| 52 | if err := wd.Get("http://play.golang.org/?simple=1"); err != nil { |
| 53 | panic(err) |
| 54 | } |
| 55 | |
| 56 | // Get a reference to the text box containing code. |
| 57 | elem, err := wd.FindElement(selenium.ByCSSSelector, "#code") |
| 58 | if err != nil { |
| 59 | panic(err) |
| 60 | } |
| 61 | // Remove the boilerplate code already in the text box. |
| 62 | if err := elem.Clear(); err != nil { |
| 63 | panic(err) |
| 64 | } |
| 65 | |
| 66 | // Enter some new code in text box. |
| 67 | err = elem.SendKeys(` |
| 68 | package main |
| 69 | import "fmt" |
| 70 | |
| 71 | func main() { |
| 72 | fmt.Println("Hello WebDriver!\n") |
| 73 | } |
| 74 | `) |
| 75 | if err != nil { |
| 76 | panic(err) |
| 77 | } |
| 78 | |
| 79 | // Click the run button. |
nothing calls this directly
no test coverage detected