playwright-fluent
Version:
Fluent API around playwright
80 lines (79 loc) • 3.95 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 page_actions_1 = require("../../../page-actions");
describe('get previous siblings', () => {
let browser = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach(() => { });
afterEach(async () => {
if (browser) {
await browser.close();
}
});
test('should return an empty array when root elements is empty', async () => {
// Given
// When
const result = await SUT.getPreviousSiblingsOf([]);
// Then
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
test('should get previous siblings', 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, 'get-previous-sibling-of-handles.test.html')}`;
await page.goto(url);
// When
const rootElements = await (0, page_actions_1.querySelectorAllInPage)('[role="row"]', page);
const result = await SUT.getPreviousSiblingsOf(rootElements);
// Then
expect(rootElements.length).toBe(3);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(2);
expect(await result[0].evaluate((node) => node.tagName)).toContain('TR');
expect(await result[0].evaluate((node) => node.getAttribute('data-test-id'))).toBe('row1');
expect(await result[1].evaluate((node) => node.tagName)).toContain('TR');
expect(await result[1].evaluate((node) => node.getAttribute('data-test-id'))).toBe('row2');
});
test('should get previous sibling', 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, 'get-previous-sibling-of-handles.test.html')}`;
await page.goto(url);
// When
const rootElements = await (0, page_actions_1.querySelectorAllInPage)('select[data-test-id="my-select2"] option', page);
const lastOption = rootElements[2];
const result = await SUT.getPreviousSiblingsOf([lastOption]);
// Then
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(1);
expect(await lastOption.evaluate((node) => node.tagName)).toContain('OPTION');
expect(await lastOption.evaluate((node) => node.innerText)).toBe('Select 2 - label 3');
const previousOption = result[0];
expect(await previousOption.evaluate((node) => node.tagName)).toContain('OPTION');
expect(await previousOption.evaluate((node) => node.innerText)).toBe('Select 2 - label 2');
});
test('should return no elements when sibling is not found', 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, 'get-previous-sibling-of-handles.test.html')}`;
await page.goto(url);
// When
const rootElements = await (0, page_actions_1.querySelectorAllInPage)('html', page);
const result = await SUT.getPreviousSiblingsOf(rootElements);
// Then
expect(rootElements.length).toBe(1);
expect(Array.isArray(result)).toBe(true);
expect(result.length).toBe(0);
});
});
;