-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add xk6-browser as an experimental module (#2884)
* Add xk6-browser as an experimental module * Update to release version of xk6-browser * Update to the experimental import * Add vendored xk6-browser dependency * Update version of xk6-browser from main This version of xk6-browser deps now match k6. * Add the vendor of the deps * Add wrapper RootModule for browser tests This wrapper RootModule will allow browser tests to run if the correct K6_BROWSER_ENABLE_RUN env var is set. * Add tests for the browser wrapper RootModule * Refactor to use VU to get env vars in RootModule * Apply suggestions from code review Co-authored-by: na-- <n@andreev.sh> * fixup! Apply suggestions from code review * fixup! Add wrapper RootModule for browser tests * Apply suggestions from code review Co-authored-by: Ivan Mirić <ivan.miric@grafana.com> --------- Co-authored-by: na-- <n@andreev.sh> Co-authored-by: Ivan Mirić <ivan.miric@grafana.com>
- Loading branch information
1 parent
134ca65
commit 1da5da3
Showing
267 changed files
with
163,038 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Package browser contains a RootModule wrapper | ||
// that wraps around the experimental browser | ||
// RootModule. | ||
package browser | ||
|
||
import ( | ||
"errors" | ||
"strconv" | ||
|
||
xk6browser "github.com/grafana/xk6-browser/browser" | ||
"go.k6.io/k6/js/common" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
type ( | ||
// RootModule is a wrapper around the experimental | ||
// browser RootModule. It will prevent browser test | ||
// runs unless K6_BROWSER_ENABLED env var is set. | ||
RootModule struct { | ||
rm *xk6browser.RootModule | ||
} | ||
) | ||
|
||
// New creates an experimental browser RootModule | ||
// and wraps it around this internal RootModule. | ||
func New() *RootModule { | ||
return &RootModule{ | ||
rm: xk6browser.New(), | ||
} | ||
} | ||
|
||
// NewModuleInstance will check to see if | ||
// K6_BROWSER_ENABLED is set before allowing | ||
// test runs to continue. | ||
func (r *RootModule) NewModuleInstance(vu modules.VU) modules.Instance { | ||
env := vu.InitEnv() | ||
|
||
throwError := func() { | ||
msg := "To run browser tests set env var K6_BROWSER_ENABLED=true" | ||
if m, ok := env.LookupEnv("K6_BROWSER_ENABLED_MSG"); ok && m != "" { | ||
msg = m | ||
} | ||
|
||
common.Throw(vu.Runtime(), errors.New(msg)) | ||
} | ||
|
||
vs, ok := env.LookupEnv("K6_BROWSER_ENABLED") | ||
if !ok { | ||
throwError() | ||
} | ||
|
||
v, err := strconv.ParseBool(vs) | ||
if err != nil || !v { | ||
throwError() | ||
} | ||
|
||
return r.rm.NewModuleInstance(vu) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { check } from 'k6'; | ||
import { chromium } from 'k6/experimental/browser'; | ||
|
||
export const options = { | ||
thresholds: { | ||
checks: ["rate==1.0"] | ||
} | ||
} | ||
|
||
export default async function() { | ||
const browser = chromium.launch({ | ||
headless: __ENV.XK6_HEADLESS ? true : false, | ||
}); | ||
const context = browser.newContext(); | ||
const page = context.newPage(); | ||
|
||
try { | ||
// Goto front page, find login link and click it | ||
await page.goto('https://test.k6.io/', { waitUntil: 'networkidle' }); | ||
await Promise.all([ | ||
page.waitForNavigation(), | ||
page.locator('a[href="/my_messages.php"]').click(), | ||
]); | ||
// Enter login credentials and login | ||
page.locator('input[name="login"]').type('admin'); | ||
page.locator('input[name="password"]').type('123'); | ||
// We expect the form submission to trigger a navigation, so to prevent a | ||
// race condition, setup a waiter concurrently while waiting for the click | ||
// to resolve. | ||
await Promise.all([ | ||
page.waitForNavigation(), | ||
page.locator('input[type="submit"]').click(), | ||
]); | ||
check(page, { | ||
'header': page.locator('h2').textContent() == 'Welcome, admin!', | ||
}); | ||
} finally { | ||
page.close(); | ||
browser.close(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.