playwright-go/examples/javascript/main.go
Can Stand d1bed759cd
Automatically generate Go interfaces code, etc. (#367)
BREAKING CHANGE: 
Due to the rewrite of the go code generation scripts, it brings the following **breaking** changes:

- Optimized generated comments. Links and **Deprecated** tags in Go doc comments now work.
  - May cause many **Deprecated** lint errors, please update the call or use `//nolint:staticcheck` to ignore.
- Added event interface methods. For example `Page.OnDialog()` etc.
- The signatures of some interface methods have changed, for example:
  - Unified optional parameter naming. For example, previously all `Click` used `PageClickOptions`, now
    ```go
    Locator.Click(options ...LocatorClickOptions)
    Page.Click(selector string, options ...PageClickOptions)
    Frame.Click(selector string, options ...FrameClickOptions)
    ```
  - Some parameters are generated as new types or names, e.g. `Page.AddInitScript(script Script)`
- Removed several methods that were not documented upstream. These methods can be safely converted to other existing methods.  For example
  - `Page.ExpectedDialog` can use `Page.OnDialog` instead.
  - All `xxxAssertions.NotTo...`, use `.Not().xxx` pls.
2023-08-24 12:44:28 +08:00

50 lines
1.2 KiB
Go

//go:build ignore
// +build ignore
package main
import (
"fmt"
"log"
"github.com/playwright-community/playwright-go"
)
func main() {
pw, err := playwright.Run()
if err != nil {
log.Fatal(err)
}
browser, err := pw.Chromium.Launch()
if err != nil {
log.Fatalf("could not launch browser: %v\n", err)
}
page, err := browser.NewPage()
if err != nil {
log.Fatalf("could not create page: %v\n", err)
}
if _, err = page.Goto("https://en.wikipedia.org/wiki/JavaScript"); err != nil {
log.Fatalf("could not goto: %v\n", err)
}
// mw.config.values is the JS object where Wikipedia stores wiki metadata
handle, err := page.EvaluateHandle("mw.config.values", struct{}{})
if err != nil {
log.Fatalf("could not acquire JSHandle: %v\n", err)
}
// mw.config.values.wgPageName is the name of the current page
pageName, err := handle.GetProperty("wgPageName")
if err != nil {
log.Fatalf("could not get Wikipedia page name: %v\n", err)
}
fmt.Printf("Lots of type casting, brought to you by %s\n", pageName)
if err := browser.Close(); err != nil {
log.Fatalf("could not close browser: %v\n", err)
}
if err := pw.Stop(); err != nil {
log.Fatalf("could not stop Playwright: %v\n", err)
}
}