playwright#webkit TypeScript Examples
The following examples show how to use
playwright#webkit.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: get-client-rectangle-of.webkit.test.ts From playwright-fluent with MIT License | 6 votes |
describe('get client rectangle', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return an error when selector is not found - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
// When
// Then
const expectedError = new Error(
'page.$eval: Error: failed to find element matching selector "foobar"',
);
await SUT.getClientRectangleOf('foobar', page).catch((error): void =>
expect(error).toMatchObject(expectedError),
);
});
});
Example #2
Source File: inject-cursor.webkit.test.ts From playwright-fluent with MIT License | 6 votes |
describe.skip('inject-cursor', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should show cursor on the page on webkit', async (): Promise<void> => {
// Given
const url = 'https://reactstrap.github.io/components/form';
browser = await webkit.launch({ headless: true });
const context = await browser.newContext({ viewport: null });
const page = await context.newPage();
await page.goto(url);
// When
await SUT.injectCursor(page);
await SUT.injectCursor(page);
await SUT.injectCursor(page);
await SUT.injectCursor(page);
// Then
const cursorExists = await exists('playwright-mouse-pointer', page);
expect(cursorExists).toBe(true);
});
});
Example #3
Source File: get-client-rectangle-of-handle.webkit.test.ts From playwright-fluent with MIT License | 6 votes |
describe('get client rectangle of an element handle', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return Client Rectangle - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'get-client-rectangle-of-handle.test.html')}`;
await page.goto(url);
await sleep(1000);
// When
const handle = await page.$('#foo');
const result = await SUT.getClientRectangleOfHandle(handle);
// Then
const expectedClientRectangle: SerializableDOMRect = {
bottom: 32,
height: 21,
left: 12,
right: 32,
top: 11,
width: 20,
x: 12, // left
y: 11, // top
};
expect(result).not.toBe(null);
expect(result).toMatchObject(expectedClientRectangle);
});
});
Example #4
Source File: get-focused-handle.webkit.test.ts From playwright-fluent with MIT License | 6 votes |
describe.skip('get-focused-handle', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return handle when selector exists on the page - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const context = await browser.newContext({ viewport: null });
const page = await context.newPage();
// When
const result = await SUT.getFocusedHandle(page);
// Then
expect(result).toBeDefined();
});
});
Example #5
Source File: server.ts From Assistive-Webdriver with MIT License | 5 votes |
browsers: { [key: string]: BrowserType<Browser> } = {
chromium,
webkit,
firefox
}
Example #6
Source File: launch-browser.ts From playwright-fluent with MIT License | 5 votes |
export async function launchBrowser(name: BrowserName, options: LaunchOptions): Promise<Browser> {
switch (name) {
case 'chrome': {
if (options && options.executablePath !== undefined) {
const browser = await chromium.launch(options);
return browser;
}
{
const chromeOptions: LaunchOptions = {
...options,
executablePath: getChromePath(),
};
const browser = await chromium.launch(chromeOptions);
return browser;
}
}
case 'chrome-canary': {
if (options && options.executablePath !== undefined) {
const browser = await chromium.launch(options);
return browser;
}
{
const chromeOptions: LaunchOptions = {
...options,
executablePath: getChromeCanaryPath(),
};
const browser = await chromium.launch(chromeOptions);
return browser;
}
}
case 'msedge': {
if (options && options.executablePath !== undefined) {
const browser = await chromium.launch(options);
return browser;
}
{
const chromeOptions: LaunchOptions = {
...options,
executablePath: getEdgePath(),
};
const browser = await chromium.launch(chromeOptions);
return browser;
}
}
case 'chromium': {
const browser = await chromium.launch(options);
return browser;
}
case 'firefox': {
const browser = await firefox.launch(options);
return browser;
}
case 'webkit': {
const browser = await webkit.launch(options);
return browser;
}
default:
throw new Error(
`Browser named '${name}' is unknown. It should be one of 'chrome', 'chromium', 'chrome-canary', 'firefox', 'webkit'`,
);
}
}
Example #7
Source File: show-mouse-position.webkit.test.ts From playwright-fluent with MIT License | 5 votes |
describe.skip('show-mouse-position', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should show cursor on the page on webkit', async (): Promise<void> => {
// Given
const url = 'https://reactstrap.github.io/components/form';
browser = await webkit.launch({ headless: true });
const context = await browser.newContext({ viewport: null });
const page = await context.newPage();
// When
await SUT.showMousePosition(page);
await page.goto(url);
// Then
const cursorExists = await exists('playwright-mouse-pointer', page);
expect(cursorExists).toBe(true);
});
test('should show cursor on navigating to another page on webkit', async (): Promise<void> => {
// Given
const url = 'https://reactstrap.github.io/components/form';
browser = await webkit.launch({ headless: true });
const context = await browser.newContext({ viewport: null });
const page = await context.newPage();
// When
await SUT.showMousePosition(page);
await page.goto(url);
await page.waitForTimeout(3000);
await page.goto('https://github.com/');
// Then
const cursorExists = await exists('playwright-mouse-pointer', page);
expect(cursorExists).toBe(true);
});
});
Example #8
Source File: is-handle-moving.webkit.test.ts From playwright-fluent with MIT License | 5 votes |
describe.skip('handle is moving', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should detect that selector is moving - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
await showMousePosition(page);
const url = `file:${path.join(__dirname, 'is-handle-moving.test1.html')}`;
await page.goto(url);
await sleep(100); // wait for the animation to be started
// When
const selector = '#moving';
const handle = await page.$(selector);
const isMoving = await SUT.isHandleMoving(handle);
// Then
expect(isMoving).toBe(true);
});
test('should detect that selector is not moving - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
await showMousePosition(page);
const url = `file:${path.join(__dirname, 'is-handle-moving.test2.html')}`;
await page.goto(url);
await sleep(2000); // wait twice the animation duration
// When
const selector = '#moving';
const handle = await page.$(selector);
const isMoving = await SUT.isHandleMoving(handle);
// Then
expect(isMoving).toBe(false);
});
});
Example #9
Source File: get-handle-of.webkit.test.ts From playwright-fluent with MIT License | 5 votes |
describe('get-handle-of', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return handle when selector exists on the page - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const context = await browser.newContext({ viewport: null });
const page = await context.newPage();
// When
const result = await SUT.getHandleOf('body', page, defaultWaitUntilOptions);
// Then
expect(result).toBeDefined();
});
test('should throw an error when selector does not exist on the page - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const context = await browser.newContext({ viewport: null });
const page = await context.newPage();
const options: WaitUntilOptions = {
...defaultWaitUntilOptions,
timeoutInMilliseconds: 2000,
};
// When
let result: Error | undefined = undefined;
try {
await SUT.getHandleOf('foobar', page, options);
} catch (error) {
result = error as Error;
}
// Then
expect(result && result.message).toContain("Selector 'foobar' was not found in DOM");
});
});
Example #10
Source File: get-viewport-rectangle-of.webkit.test.ts From playwright-fluent with MIT License | 5 votes |
describe('get viewport rectangle of page', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return defaultViewport - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext();
const page = await browserContext.newPage();
// When
const result = await SUT.getViewportRectangleOf(page);
// Then
const defaultViewportRectangle: ViewportRect = {
height: 720,
offsetLeft: 0,
offsetTop: 0,
pageLeft: 0,
pageTop: 0,
scale: 1,
width: 1280,
};
const defaultViewportRectangleOnCI: ViewportRect = {
height: 720,
offsetLeft: 0,
offsetTop: 0,
pageLeft: 0,
pageTop: 0,
scale: 1,
width: 1024,
};
expect(result).toBeDefined();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(result!.height).toBe(defaultViewportRectangle.height);
expect(
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
result!.width === defaultViewportRectangle.width ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
result!.width === defaultViewportRectangleOnCI.width,
).toBe(true);
});
});
Example #11
Source File: hover-on-selector.webkit.test.ts From playwright-fluent with MIT License | 5 votes |
describe('hover on selector', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should wait for the selector to exists before hovering - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
await showMousePosition(page);
const url = `file:${path.join(__dirname, 'hover-on-selector.test.html')}`;
await page.goto(url);
const selector = '#dynamically-added';
let handle = await page.$(selector);
const isSelectorVisibleBeforeScroll = await isHandleVisible(handle, defaultVerboseOptions);
const options: HoverOptions = {
...defaultHoverOptions,
};
// When
await SUT.hoverOnSelector(selector, page, options);
handle = await page.$(selector);
const isSelectorVisibleAfterScroll = await isHandleVisible(handle, defaultVerboseOptions);
// Then
expect(isSelectorVisibleBeforeScroll).toBe(false);
expect(isSelectorVisibleAfterScroll).toBe(true);
const mousePositionClientRectangle = await getClientRectangleOf(
'playwright-mouse-pointer',
page,
);
const mouseX = mousePositionClientRectangle.left + mousePositionClientRectangle.width / 2;
const mouseY = mousePositionClientRectangle.top + mousePositionClientRectangle.height / 2;
const currentClientRectangle = await getClientRectangleOf(selector, page);
const expectedX = currentClientRectangle.left + currentClientRectangle.width / 2;
const expectedY = currentClientRectangle.top + currentClientRectangle.height / 2;
expect(Math.abs(mouseX - expectedX)).toBeLessThanOrEqual(1);
expect(Math.abs(mouseY - expectedY)).toBeLessThanOrEqual(1);
});
});
Example #12
Source File: ctor-1.test.ts From playwright-fluent with MIT License | 5 votes |
describe('Playwright Fluent - ctor usage', (): void => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
test('should take existing browser and page instance of chromium', async (): Promise<void> => {
// Given
const browser = await chromium.launch({ headless: true });
const context = await browser.newContext();
const url = `file:${path.join(__dirname, 'ctor.test.html')}`;
const page = await context.newPage();
await page.goto(url);
// When
const p = new PlaywrightFluent(browser, page);
// Then
expect(p.currentBrowser()).toBe(browser);
expect(p.currentPage()).toBe(page);
expect(p.currentFrame()).toBeUndefined();
await browser.close();
});
test('should take existing browser and page instance of firefox', async (): Promise<void> => {
// Given
const browser = await firefox.launch({ headless: true });
const context = await browser.newContext();
// const url = `file:${path.join(__dirname, 'ctor.test.html')}`;
const page = await context.newPage();
await page.goto('https://google.com');
// When
const p = new PlaywrightFluent(browser, page);
// Then
expect(p.currentBrowser()).toBe(browser);
expect(p.currentPage()).toBe(page);
expect(p.currentFrame()).toBeUndefined();
await browser.close();
});
test('should take existing browser and page instance of webkit', async (): Promise<void> => {
// Given
const browser = await webkit.launch({ headless: true });
const context = await browser.newContext();
const url = `file:${path.join(__dirname, 'ctor.test.html')}`;
const page = await context.newPage();
await page.goto(url);
// When
const p = new PlaywrightFluent(browser, page);
// Then
expect(p.currentBrowser()).toBe(browser);
expect(p.currentPage()).toBe(page);
expect(p.currentFrame()).toBeUndefined();
await browser.close();
});
test.skip('should take existing browser and page instance of firefox', async (): Promise<void> => {
// Given
const browser = await firefox.launch({ headless: true });
const context = await browser.newContext();
const url = `file:${path.join(__dirname, 'ctor.test.html')}`;
const page = await context.newPage();
await page.goto(url);
// When
const p = new PlaywrightFluent(browser, page);
// Then
expect(p.currentBrowser()).toBe(browser);
expect(p.currentPage()).toBe(page);
expect(p.currentFrame()).toBeUndefined();
await browser.close();
});
});
Example #13
Source File: is-handle-visible.webkit.test.ts From playwright-fluent with MIT License | 4 votes |
describe.skip('handle is visible', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return false when selector is hidden - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#hidden');
// When
const result = await SUT.isHandleVisible(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test('should return true when selector is visible', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#visible');
// When
const result = await SUT.isHandleVisible(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(true);
});
test('should return false when selector is transparent', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#transparent');
// When
const result = await SUT.isHandleVisible(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test('should return false when selector is out of screen', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#out-of-screen');
// When
const result = await SUT.isHandleVisible(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test('should return true when selector is visible but out of viewport', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#out-of-viewport');
// When
const result = await SUT.isHandleVisible(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(true);
});
test.skip('should return 1 when selector is in viewport - issue playwright headless', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#visible');
// When
const visibleRatio = await getIntersectionRatioOfHandle(handle);
// eslint-disable-next-line no-console
console.log(`visible ratio is ${visibleRatio}`);
const windowState = await getWindowState(page);
// eslint-disable-next-line no-console
console.log(`windowState:\n ${JSON.stringify(windowState, null, 2)}`);
// Then
expect(handle).toBeDefined();
expect(visibleRatio).toBe(1);
});
test.skip('should return 1 when selector is in viewport - issue playwright headfull', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: false });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#visible');
// When
const visibleRatio = await getIntersectionRatioOfHandle(handle);
// eslint-disable-next-line no-console
console.log(`visible ratio is ${visibleRatio}`);
const windowState = await getWindowState(page);
// eslint-disable-next-line no-console
console.log(`windowState:\n ${JSON.stringify(windowState, null, 2)}`);
// Then
expect(handle).toBeDefined();
expect(visibleRatio).toBe(1);
});
});
Example #14
Source File: is-handle-visible.webkit.test.ts From playwright-fluent with MIT License | 4 votes |
describe.skip('handle is visible in viewport', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should return false when selector is hidden - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#hidden');
// When
const result = await SUT.isHandleVisibleInViewport(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test('should return true when selector is visible in viewport', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#visible');
// When
const result = await SUT.isHandleVisibleInViewport(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(true);
});
test('should return false when selector is transparent', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#transparent');
// When
const result = await SUT.isHandleVisibleInViewport(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test('should return false when selector is out of screen', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#out-of-screen');
// When
const result = await SUT.isHandleVisibleInViewport(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test('should return false when selector is out of viewport', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#out-of-viewport');
// When
const result = await SUT.isHandleVisibleInViewport(handle, defaultVerboseOptions);
// Then
expect(handle).toBeDefined();
expect(result).toBe(false);
});
test.skip('should return 1 when selector is in viewport - issue playwright headless', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#visible');
// When
const visibleRatio = await getIntersectionRatioOfHandle(handle);
// eslint-disable-next-line no-console
console.log(`visible ratio is ${visibleRatio}`);
const windowState = await getWindowState(page);
// eslint-disable-next-line no-console
console.log(`windowState:\n ${JSON.stringify(windowState, null, 2)}`);
// Then
expect(handle).toBeDefined();
expect(visibleRatio).toBe(1);
});
test.skip('should return 1 when selector is in viewport - issue playwright headfull', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: false });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
const url = `file:${path.join(__dirname, 'is-handle-visible-in-viewport.test.html')}`;
await page.goto(url);
await sleep(1000);
const handle = await page.$('#visible');
// When
const visibleRatio = await getIntersectionRatioOfHandle(handle);
// eslint-disable-next-line no-console
console.log(`visible ratio is ${visibleRatio}`);
const windowState = await getWindowState(page);
// eslint-disable-next-line no-console
console.log(`windowState:\n ${JSON.stringify(windowState, null, 2)}`);
// Then
expect(handle).toBeDefined();
expect(visibleRatio).toBe(1);
});
});
Example #15
Source File: scroll-to-handle.webkit.test.ts From playwright-fluent with MIT License | 4 votes |
describe('scroll to handle', (): void => {
let browser: Browser | undefined = undefined;
// eslint-disable-next-line @typescript-eslint/no-empty-function
beforeEach((): void => {});
afterEach(async (): Promise<void> => {
if (browser) {
await browser.close();
}
});
test('should scroll to a selector that is out of viewport - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
await showMousePosition(page);
const url = `file:${path.join(__dirname, 'scroll-to-handle.test.html')}`;
await page.goto(url);
await sleep(1000);
const selector = '#out-of-view-port';
const previousClientRectangle = await getClientRectangleOf(selector, page);
const previousViewportRectangle = await getViewportRectangleOf(page);
const handle = await page.$(selector);
const isSelectorVisibleBeforeScroll = await isHandleVisibleInViewport(
handle,
defaultVerboseOptions,
);
// When
await SUT.scrollToHandle(handle);
await sleep(2000);
const currentClientRectangle = await getClientRectangleOf(selector, page);
const currentViewportRectangle = await getViewportRectangleOf(page);
const isSelectorVisibleAfterScroll = await isHandleVisibleInViewport(
handle,
defaultVerboseOptions,
);
// Then
expect(isSelectorVisibleBeforeScroll).toBe(false);
expect(isSelectorVisibleAfterScroll).toBe(true);
expect(previousClientRectangle.top).toBeGreaterThan(currentClientRectangle.top);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(previousViewportRectangle!.pageTop).toBe(0);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(currentViewportRectangle!.pageTop).toBeGreaterThan(1000);
});
test('should not scroll to a hidden selector - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
await showMousePosition(page);
const url = `file:${path.join(__dirname, 'scroll-to-handle.test.html')}`;
await page.goto(url);
await sleep(1000);
const selector = '#hidden';
const handle = await page.$(selector);
const previousClientRectangle = await getClientRectangleOf(selector, page);
const previousViewportRectangle = await getViewportRectangleOf(page);
// When
await SUT.scrollToHandle(handle);
await sleep(2000);
const currentClientRectangle = await getClientRectangleOf(selector, page);
const currentViewportRectangle = await getViewportRectangleOf(page);
// Then
expect(previousClientRectangle).toMatchObject(currentClientRectangle);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(previousViewportRectangle!.pageTop).toBe(0);
expect(currentViewportRectangle).toMatchObject(previousViewportRectangle || {});
});
test('should scroll to a transparent selector - webkit', async (): Promise<void> => {
// Given
browser = await webkit.launch({ headless: true });
const browserContext = await browser.newContext({ viewport: null });
const page = await browserContext.newPage();
await showMousePosition(page);
const url = `file:${path.join(__dirname, 'scroll-to-handle.test.html')}`;
await page.goto(url);
await sleep(1000);
const selector = '#transparent';
const handle = await page.$(selector);
const previousClientRectangle = await getClientRectangleOf(selector, page);
const previousViewportRectangle = await getViewportRectangleOf(page);
// When
await SUT.scrollToHandle(handle);
await sleep(2000);
const currentClientRectangle = await getClientRectangleOf(selector, page);
const currentViewportRectangle = await getViewportRectangleOf(page);
// Then
expect(previousClientRectangle.top).toBeGreaterThan(currentClientRectangle.top);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(previousViewportRectangle!.pageTop).toBe(0);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
expect(currentViewportRectangle!.pageTop).toBeGreaterThan(1000);
});
});