diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..55d85a5 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,18 @@ +name: Test +on: + workflow_dispatch: + push: + tags: + - "v*.*.*" + pull_request: + branches: + - "main" +jobs: + test: + runs-on: ubuntu-latest + defaults: + run: + working-directory: testdata + steps: + - uses: actions/checkout@v3 + - run: docker run --rm $(docker build -q .) diff --git a/README.md b/README.md index 6607379..592cb55 100644 --- a/README.md +++ b/README.md @@ -2,15 +2,16 @@ # Instruction -To use this base image, please specify `DriverDirectory` to `/root/.cache`. -Also, we are recommend to specify `SkipInstallBrowsers` to `true`. +To use this base image, please specify followng arguments of `BrowserTypeLaunchOptions`. ```go -pw, err := playwright.Run(&playwright.RunOptions{ - DriverDirectory: "/root/.cache", - SkipInstallBrowsers: true, +pw, _ := playwright.Run() + +browser, _ := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Args: []string{ + "--disable-dev-shm-usage", + "--single-process", + "--no-sandbox", + }, }) -if err != nil { - return nil, fmt.Errorf("failed to start playwright: %w", err) -} ``` diff --git a/testdata/Dockerfile b/testdata/Dockerfile new file mode 100644 index 0000000..e1830fd --- /dev/null +++ b/testdata/Dockerfile @@ -0,0 +1,15 @@ +FROM golang:1.20 as builder + +WORKDIR /go/src/app + +COPY . . + +RUN CGO_ENABLED=0 GOOS=linux go build -mod=readonly -v -o pw ./main.go + +FROM ghcr.io/mokmok-dev/playwright-go:latest as runner + +WORKDIR / + +COPY --from=builder /go/src/app/pw /pw + +ENTRYPOINT ["/pw"] diff --git a/testdata/go.mod b/testdata/go.mod new file mode 100644 index 0000000..50abe43 --- /dev/null +++ b/testdata/go.mod @@ -0,0 +1,20 @@ +module github.com/mokmok-dev/playwright-go/testdata + +go 1.20 + +require ( + github.com/playwright-community/playwright-go v0.2000.1 + github.com/volatiletech/null/v8 v8.1.2 +) + +require ( + github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 // indirect + github.com/friendsofgo/errors v0.9.2 // indirect + github.com/go-stack/stack v1.8.1 // indirect + github.com/gofrs/uuid v3.2.0+incompatible // indirect + github.com/volatiletech/inflect v0.0.1 // indirect + github.com/volatiletech/randomize v0.0.1 // indirect + github.com/volatiletech/strmangle v0.0.1 // indirect + golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 // indirect + gopkg.in/square/go-jose.v2 v2.6.0 // indirect +) diff --git a/testdata/go.sum b/testdata/go.sum new file mode 100644 index 0000000..023ba9c --- /dev/null +++ b/testdata/go.sum @@ -0,0 +1,35 @@ +github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964 h1:y5HC9v93H5EPKqaS1UYVg1uYah5Xf51mBfIoWehClUQ= +github.com/danwakefield/fnmatch v0.0.0-20160403171240-cbb64ac3d964/go.mod h1:Xd9hchkHSWYkEqJwUGisez3G1QY8Ryz0sdWrLPMGjLk= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/friendsofgo/errors v0.9.2 h1:X6NYxef4efCBdwI7BgS820zFaN7Cphrmb+Pljdzjtgk= +github.com/friendsofgo/errors v0.9.2/go.mod h1:yCvFW5AkDIL9qn7suHVLiI/gH228n7PC4Pn44IGoTOI= +github.com/go-stack/stack v1.8.1 h1:ntEHSVwIt7PNXNpgPmVfMrNhLtgjlmnZha2kOpuRiDw= +github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP3XYfe4= +github.com/gofrs/uuid v3.2.0+incompatible h1:y12jRkkFxsd7GpqdSZ+/KCs/fJbqpEXSGd4+jfEaewE= +github.com/gofrs/uuid v3.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/h2non/filetype v1.1.1/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= +github.com/playwright-community/playwright-go v0.2000.1 h1:2JViSHpJQ/UL/PO1Gg6gXV5IcXAAsoBJ3KG9L3wKXto= +github.com/playwright-community/playwright-go v0.2000.1/go.mod h1:1y9cM9b9dVHnuRWzED1KLM7FtbwTJC8ibDjI6MNqewU= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/volatiletech/inflect v0.0.1 h1:2a6FcMQyhmPZcLa+uet3VJ8gLn/9svWhJxJYwvE8KsU= +github.com/volatiletech/inflect v0.0.1/go.mod h1:IBti31tG6phkHitLlr5j7shC5SOo//x0AjDzaJU1PLA= +github.com/volatiletech/null/v8 v8.1.2 h1:kiTiX1PpwvuugKwfvUNX/SU/5A2KGZMXfGD0DUHdKEI= +github.com/volatiletech/null/v8 v8.1.2/go.mod h1:98DbwNoKEpRrYtGjWFctievIfm4n4MxG0A6EBUcoS5g= +github.com/volatiletech/randomize v0.0.1 h1:eE5yajattWqTB2/eN8df4dw+8jwAzBtbdo5sbWC4nMk= +github.com/volatiletech/randomize v0.0.1/go.mod h1:GN3U0QYqfZ9FOJ67bzax1cqZ5q2xuj2mXrXBjWaRTlY= +github.com/volatiletech/strmangle v0.0.1 h1:UKQoHmY6be/R3tSvD2nQYrH41k43OJkidwEiC74KIzk= +github.com/volatiletech/strmangle v0.0.1/go.mod h1:F6RA6IkB5vq0yTG4GQ0UsbbRcl3ni9P76i+JrTBKFFg= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= +golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/square/go-jose.v2 v2.6.0 h1:NGk74WTnPKBNUhNzQX7PYcTLUjoq7mzKk2OKbvwk2iI= +gopkg.in/square/go-jose.v2 v2.6.0/go.mod h1:M9dMgbHiYLoDGQrXy7OpJDJWiKiU//h+vD76mk0e1AI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/testdata/main.go b/testdata/main.go new file mode 100644 index 0000000..e708228 --- /dev/null +++ b/testdata/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + + playwright "github.com/playwright-community/playwright-go" +) + +const url = "https://playwright.dev/" + +func main() { + pw, err := playwright.Run() + if err != nil { + panic(err) + } + + browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{ + Args: []string{ + "--disable-dev-shm-usage", + "--single-process", + "--no-sandbox", + }, + }) + if err != nil { + panic(err) + } + + bCtx, err := browser.NewContext() + if err != nil { + panic(err) + } + + page, err := bCtx.NewPage() + if err != nil { + panic(err) + } + + if _, err := page.Goto(url); err != nil { + panic(err) + } + + elm, err := page.Locator("header > div > h1 > span") + if err != nil { + panic(err) + } + + txt, err := elm.InnerText() + if err != nil { + panic(err) + } + + fmt.Println(txt) +}