Skip to content

Commit

Permalink
feat: added reload, remove custom browser launch option, tweaked laun…
Browse files Browse the repository at this point in the history
…ch input otions to full spectrum
  • Loading branch information
nicholasvuono committed Mar 10, 2022
1 parent bbe89ca commit 21a03da
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<p>Special thanks to all the contributors over at <a href="https://github.com/grafana/k6/graphs/contributors" target="_blank">k6</a> and <a href="https://github.com/mxschmitt/playwright-go/graphs/contributors" target="_blank">playwright-go</a>
<p>Here's to open source!</p>

<a href="https://github.com/nicholasvuono/xk6-playwright/releases"><img alt="GitHub license" src="https://img.shields.io/badge/release-v0.2.0-blue"></a>
<a href="https://github.com/nicholasvuono/xk6-playwright/releases"><img alt="GitHub license" src="https://img.shields.io/badge/release-v0.2.3-blue"></a>
<a href="https://goreportcard.com/report/github.com/wosp-io/xk6-playwright"><img src="https://goreportcard.com/badge/github.com/wosp-io/xk6-playwright?dummy=unused" alt="Go Report Card"></a>
<a href="https://github.com/wosp-io/xk6-playwright/blob/main/LICENSE"><img alt="GitHub license" src="https://img.shields.io/github/license/wosp-io/xk6-playwright?color=red"></a>
</div>
Expand Down Expand Up @@ -87,6 +87,7 @@ export default function () {
| fill() | [`Fill()`](https://pkg.go.dev/github.com/mxschmitt/playwright-go#Page.Fill) | fills an 'input' element on the page based on the provided selector and string to be entered |
| dragAndDrop() | [`DragAndDrop()`](https://pkg.go.dev/github.com/mxschmitt/playwright-go#Page.DragAndDrop) | drag an item from one place to another based on two selectors |
| evaluate() | [`Evaluate()`](https://pkg.go.dev/github.com/mxschmitt/playwright-go#Page.Evaluate) | evaluate an expresion or function and get the return value |
| reload() | [`Reload()`](https://pkg.go.dev/github.com/mxschmitt/playwright-go#Page.Reload) | reloads the current page |


NOTE: the above 'Encompassed Playwright Function(s)' will link to the [playwright-go package documentation](https://pkg.go.dev/github.com/mxschmitt/playwright-go#section-readme) to give an in-depth overview of how these functions will behave from a low-level perspective.
Expand Down
33 changes: 8 additions & 25 deletions playwright.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,12 @@ type Playwright struct {
}

//Launch starts the playwright client and launches a browser
func (p *Playwright) Launch(args []string) {
func (p *Playwright) Launch(args playwright.BrowserTypeLaunchOptions) {
pw, err := playwright.Run()
if err != nil {
log.Fatalf("could not start playwright: %v", err)
}
browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Args: args,
})
if err != nil {
log.Fatalf("could not launch browser: %v", err)
}
p.Self = pw
p.Browser = browser
}

func (p *Playwright) LaunchCustomBrowser(args []string, browserPath string, driver string) {
runOptions := playwright.RunOptions{
DriverDirectory: driver,
SkipInstallBrowsers: true,
Browsers: []string {browserPath},
}
pw, err := playwright.Run(&runOptions)
if err != nil {
log.Fatalf("could not start playwright: %v", err)
}
browser, err := pw.Chromium.Launch(playwright.BrowserTypeLaunchOptions{
Args: args,
ExecutablePath: &browserPath,
})
browser, err := pw.Chromium.Launch(args)
if err != nil {
log.Fatalf("could not launch browser: %v", err)
}
Expand Down Expand Up @@ -163,4 +140,10 @@ func (p *Playwright) Evaluate(expression string, opts playwright.PageEvaluateOpt
log.Fatalf("error evaluating the expression: %v", err)
}
return returnedValue;
}

func (p *Playwright) Reload() {
if _, err := p.Page.Reload(); err != nil {
log.Fatalf("error with reloading the page: %v", err)
}
}
19 changes: 4 additions & 15 deletions playwright_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (

var tests = []func(t *testing.T){
TestPlaywright,
TestPlaywrightCustomBrowser,
}

func TestPlaywright(t *testing.T) {
var pw Playwright
var opts []string
headless := true
opts := playwright.BrowserTypeLaunchOptions {
Headless: &headless,
}
var opts2 playwright.PageGotoOptions
var opts3 playwright.PageWaitForSelectorOptions

Expand All @@ -25,19 +27,6 @@ func TestPlaywright(t *testing.T) {
pw.Kill()
}

func TestPlaywrightCustomBrowser(t *testing.T) {
var pw Playwright
var opts []string
var opts2 playwright.PageGotoOptions
var opts3 playwright.PageWaitForSelectorOptions

pw.LaunchCustomBrowser(opts, "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe", "")
pw.NewPage()
pw.Goto("https://www.google.com", opts2)
pw.WaitForSelector("//html/body/div[1]/div[2]", opts3)
pw.Kill()
}

func TestAll(t *testing.T) {
for i, test := range tests {
t.Run(strconv.Itoa(i), test)
Expand Down

0 comments on commit 21a03da

Please sign in to comment.