Skip to content
This repository has been archived by the owner on Jan 30, 2025. It is now read-only.

Commit

Permalink
Change approach in tests to avoid data race
Browse files Browse the repository at this point in the history
  • Loading branch information
olegbespalov committed Dec 10, 2024
1 parent 4cfa2e0 commit 2223948
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 86 deletions.
136 changes: 50 additions & 86 deletions tests/locator_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package tests

import (
"bytes"
"fmt"
"net/http"
"testing"
"time"

Expand All @@ -12,10 +9,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/grafana/xk6-browser/common"

"go.k6.io/k6/cmd"
k6tests "go.k6.io/k6/cmd/tests"
k6httpmultibin "go.k6.io/k6/lib/testutils/httpmultibin"
)

// Strict mode:
Expand Down Expand Up @@ -676,85 +669,56 @@ func TestLocatorShadowDOM(t *testing.T) {
func TestSelectOption(t *testing.T) {
t.Parallel()

tb := k6httpmultibin.NewHTTPMultiBin(t)
ts := k6tests.NewGlobalTestState(t)
tb.Mux.HandleFunc("/favicon.ico", func(w http.ResponseWriter, _ *http.Request) {
_, _ = fmt.Fprint(w, "")
})
tb.Mux.HandleFunc("/selectOption", func(w http.ResponseWriter, _ *http.Request) {
_, err := fmt.Fprint(w, `
<html>
<body>
<select name="numbers" id="numbers-options" onchange="selectOnChange(this)" multiple>
<option value="zero">Zero</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
</body>
</html>
`)
assert.NoError(t, err)
})

ts.CmdArgs = []string{
"k6", "run", "-",
}
ts.Stdin = bytes.NewBufferString(tb.Replacer.Replace(`
import { browser } from 'k6/browser';
export const options = {
scenarios: {
browser: {
executor: 'shared-iterations',
options: {
browser: {
type: 'chromium',
},
},
},
},
};
export default async function () {
const page = await browser.newPage();
await page.goto('HTTPBIN_IP_URL/selectOption');
const options = page.locator('#numbers-options');
await options.selectOption({label:'Five'});
let selectedValue = await options.inputValue();
if (selectedValue !== 'five') {
throw new Error('Expected "five" but got ' + selectedValue);
}
await options.selectOption({index:5});
selectedValue = await options.inputValue();
if (selectedValue !== 'five') {
throw new Error('Expected "five" but got ' + selectedValue);
}
await options.selectOption({value:'four'});
selectedValue = await options.inputValue();
if (selectedValue !== 'four') {
throw new Error('Expected "four" but got ' + selectedValue);
}
await options.selectOption([{label:'One'}]);
selectedValue = await options.inputValue();
if (selectedValue !== 'one') {
throw new Error('Expected "one" but got ' + selectedValue);
}
await options.selectOption(['two']); // Value
selectedValue = await options.inputValue();
if (selectedValue !== 'two') {
throw new Error('Expected "two" but got ' + selectedValue);
}
await options.selectOption('five'); // Value
selectedValue = await options.inputValue();
if (selectedValue !== 'five') {
throw new Error('Expected "five" but got ' + selectedValue);
}
tb := newTestBrowser(t,
withFileServer(),
)
defer tb.Browser.Close()

vu, _, _, cleanUp := startIteration(t)
defer cleanUp()

got := vu.RunPromise(t, `
const page = await browser.newPage();
await page.goto('%s');
const options = page.locator('#numbers-options');
await options.selectOption({label:'Five'});
let selectedValue = await options.inputValue();
if (selectedValue !== 'five') {
throw new Error('Expected "five" but got ' + selectedValue);
}
await options.selectOption({index:5});
selectedValue = await options.inputValue();
if (selectedValue !== 'five') {
throw new Error('Expected "five" but got ' + selectedValue);
}
await options.selectOption({value:'four'});
selectedValue = await options.inputValue();
if (selectedValue !== 'four') {
throw new Error('Expected "four" but got ' + selectedValue);
}
`))
cmd.ExecuteWithGlobalState(ts.GlobalState)
assert.Empty(t, ts.Stderr.String())
await options.selectOption([{label:'One'}]);
selectedValue = await options.inputValue();
if (selectedValue !== 'one') {
throw new Error('Expected "one" but got ' + selectedValue);
}
await options.selectOption(['two']); // Value
selectedValue = await options.inputValue();
if (selectedValue !== 'two') {
throw new Error('Expected "two" but got ' + selectedValue);
}
await options.selectOption('five'); // Value
selectedValue = await options.inputValue();
if (selectedValue !== 'five') {
throw new Error('Expected "five" but got ' + selectedValue);
}
`, tb.staticURL("select_options.html"))
assert.Equal(t, sobek.Undefined(), got.Result())
}
12 changes: 12 additions & 0 deletions tests/static/select_options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html>
<body>
<select name="numbers" id="numbers-options" onchange="selectOnChange(this)" multiple>
<option value="zero">Zero</option>
<option value="one">One</option>
<option value="two">Two</option>
<option value="three">Three</option>
<option value="four">Four</option>
<option value="five">Five</option>
</select>
</body>
</html>

0 comments on commit 2223948

Please sign in to comment.