playwright-fluent
Version:
Fluent API around playwright
120 lines (119 loc) • 6.6 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const path = tslib_1.__importStar(require("path"));
const playwright_1 = require("playwright");
const SUT = tslib_1.__importStar(require("../index"));
const get_all_options_of_handle_1 = require("../../get-all-options-of-handle");
const select_options_in_handle_1 = require("../../select-options-in-handle");
describe('select options by value in handle', () => {
let browser = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach(() => { });
afterEach(async () => {
if (browser) {
await browser.close();
}
});
test('should throw when selector is not a select - chromium', async () => {
// Given
browser = await playwright_1.chromium.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'select-options-by-value-in-handle.common.test.html')}`;
await page.goto(url);
const selector = '#empty-input';
const handle = await page.$(selector);
// When
// Then
const expectedError = new Error("Cannot find any options in selector '#empty-input'");
await SUT.selectOptionsByValueInHandle(handle, selector, ['foobar'], page, select_options_in_handle_1.defaultSelectOptions).catch((error) => expect(error).toMatchObject(expectedError));
});
test('should throw when select is disabled - chromium', async () => {
// Given
browser = await playwright_1.chromium.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'select-options-by-value-in-handle.common.test.html')}`;
await page.goto(url);
const selector = '#disabled-select';
const handle = await page.$(selector);
// When
// Then
const expectedError = new Error("Cannot select options 'value 1' in '#disabled-select' because this selector is disabled");
await SUT.selectOptionsByValueInHandle(handle, selector, ['value 1'], page, {
...select_options_in_handle_1.defaultSelectOptions,
timeoutInMilliseconds: 2000,
}).catch((error) => expect(error).toMatchObject(expectedError));
});
test('should throw when option is unknown - chromium', async () => {
// Given
browser = await playwright_1.chromium.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'select-options-by-value-in-handle.common.test.html')}`;
await page.goto(url);
const selector = '#enabled-select';
const handle = await page.$(selector);
// When
// Then
const expectedError = new Error("Cannot select options 'foobar' in '#enabled-select' because this selector has only options ',value 1,value 2'");
await SUT.selectOptionsByValueInHandle(handle, selector, ['foobar'], page, {
...select_options_in_handle_1.defaultSelectOptions,
timeoutInMilliseconds: 2000,
}).catch((error) => expect(error).toMatchObject(expectedError));
});
test('should not throw when options are already selected in a disabled select - chromium', async () => {
// Given
browser = await playwright_1.chromium.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'select-options-by-value-in-handle.common.test.html')}`;
await page.goto(url);
// When
const selector = '#disabled-select';
const values = ['value 2', 'value 3'];
const handle = await page.$(selector);
await SUT.selectOptionsByValueInHandle(handle, selector, values, page, select_options_in_handle_1.defaultSelectOptions);
// Then
expect(true).toBe(true);
});
test('should select option - chromium', async () => {
// Given
browser = await playwright_1.chromium.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'select-options-by-value-in-handle.common.test.html')}`;
await page.goto(url);
// When
const selector = '#enabled-select';
const values = ['value 1'];
const handle = await page.$(selector);
await SUT.selectOptionsByValueInHandle(handle, selector, values, page, select_options_in_handle_1.defaultSelectOptions);
// Then
const options = await (0, get_all_options_of_handle_1.getAllOptionsOfHandle)(handle, selector);
const selectedOption = options.find((o) => o.selected);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(selectedOption.value).toBe('value 1');
});
test('should select multiple options - chromium', async () => {
// Given
browser = await playwright_1.chromium.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'select-options-by-value-in-handle.common.test.html')}`;
await page.goto(url);
// When
const selector = '#multiple-select';
const labels = ['value 1', 'value 2'];
const handle = await page.$(selector);
const optionsBefore = await (0, get_all_options_of_handle_1.getAllOptionsOfHandle)(handle, selector);
await SUT.selectOptionsByValueInHandle(handle, selector, labels, page, select_options_in_handle_1.defaultSelectOptions);
// Then
const optionsAfter = await (0, get_all_options_of_handle_1.getAllOptionsOfHandle)(handle, selector);
const selectedOptionsBefore = optionsBefore.filter((o) => o.selected).map((o) => o.value);
const selectedOptionsAfter = optionsAfter.filter((o) => o.selected).map((o) => o.value);
expect(`${selectedOptionsBefore}`).toBe('value 2,value 3');
expect(`${selectedOptionsAfter}`).toBe('value 1,value 2');
});
});
;