-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FI-1476 feat: add actions for switching tabs
- Loading branch information
Showing
14 changed files
with
134 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import {LogEventType} from '../constants/internal'; | ||
import {clearTab} from '../context/tab'; | ||
import {log} from '../utils/log'; | ||
|
||
/** | ||
* Switches page context to the specified tab. | ||
*/ | ||
export const switchToMainTab = (): void => { | ||
log('Switch page context to the main tab', LogEventType.InternalAction); | ||
|
||
clearTab(); | ||
}; |
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,14 @@ | ||
import {LogEventType} from '../constants/internal'; | ||
import {setTab} from '../context/tab'; | ||
import {log} from '../utils/log'; | ||
|
||
import type {Tab} from '../types/internal'; | ||
|
||
/** | ||
* Switches page context to the specified tab. | ||
*/ | ||
export const switchToTab = (tab: Tab): void => { | ||
log('Switch page context to the specified tab', LogEventType.InternalAction); | ||
|
||
setTab(tab); | ||
}; |
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,22 @@ | ||
import {getPlaywrightPage} from '../../useContext'; | ||
import {getFullPackConfig} from '../../utils/config'; | ||
|
||
import type {InternalTab, Tab} from '../../types/internal'; | ||
|
||
type Options = Readonly<{ | ||
timeout?: number; | ||
}>; | ||
|
||
/** | ||
* Waits for opening of new tab and returns this tab. | ||
*/ | ||
export const waitForNewTab = async (options?: Options): Promise<Tab> => { | ||
const context = getPlaywrightPage().context(); | ||
const timeout = options?.timeout ?? getFullPackConfig().pageRequestTimeout; | ||
|
||
const page = await context.waitForEvent('page', {timeout}); | ||
|
||
const newTab: InternalTab = {page}; | ||
|
||
return newTab as Tab; | ||
}; |
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,24 @@ | ||
// eslint-disable-next-line import/no-internal-modules | ||
import {useContext} from '../useContext/useContext'; | ||
|
||
import type {ClearContext, GetContext, SetContext, Tab} from '../types/internal'; | ||
|
||
const [get, set, clear] = useContext<Tab>(); | ||
|
||
/** | ||
* Get tab. | ||
* @internal | ||
*/ | ||
export const getTab: GetContext<Tab> = get; | ||
|
||
/** | ||
* Set tab. | ||
* @internal | ||
*/ | ||
export const setTab: SetContext<Tab> = set; | ||
|
||
/** | ||
* Clear tab. | ||
* @internal | ||
*/ | ||
export const clearTab: ClearContext = clear; |
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,14 @@ | ||
import type {Page} from '@playwright/test'; | ||
|
||
import type {Brand} from './brand'; | ||
|
||
/** | ||
* Internal presentation of `Tab`. | ||
* @internal | ||
*/ | ||
export type InternalTab = Partial<Tab> & Readonly<{page: Page}>; | ||
|
||
/** | ||
* Tab with page (in test). | ||
*/ | ||
export type Tab = Brand<object, 'Tab'>; |
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,23 @@ | ||
import {AsyncLocalStorage} from 'node:async_hooks'; | ||
|
||
import {assertValueIsDefined} from '../utils/asserts'; | ||
|
||
import type {Page} from '@playwright/test'; | ||
|
||
/** | ||
* Async local storage for `page` of current test. | ||
* @internal | ||
*/ | ||
export const pageStorage = new AsyncLocalStorage<Page>(); | ||
|
||
/** | ||
* Internal get `page` object from context of current test. | ||
* @internal | ||
*/ | ||
export const getInternalPlaywrightPage = (): Page => { | ||
const maybePage = pageStorage.getStore(); | ||
|
||
assertValueIsDefined(maybePage, 'maybePage is defined'); | ||
|
||
return maybePage; | ||
}; |
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 |
---|---|---|
@@ -1,22 +1,20 @@ | ||
import {AsyncLocalStorage} from 'node:async_hooks'; | ||
import {getTab} from '../context/tab'; | ||
|
||
import {assertValueIsDefined} from '../utils/asserts'; | ||
import {getInternalPlaywrightPage} from './internalPage'; | ||
|
||
import type {Page} from '@playwright/test'; | ||
|
||
/** | ||
* Async local storage for `page` of current test. | ||
* @internal | ||
*/ | ||
export const pageStorage = new AsyncLocalStorage<Page>(); | ||
import type {InternalTab} from '../types/internal'; | ||
|
||
/** | ||
* Get `page` object from context of current test. | ||
*/ | ||
export const getPlaywrightPage = (): Page => { | ||
const maybePage = pageStorage.getStore(); | ||
const tab = getTab(); | ||
|
||
assertValueIsDefined(maybePage, 'maybePage is defined'); | ||
if (tab !== undefined) { | ||
return (tab as InternalTab).page; | ||
} | ||
|
||
return maybePage; | ||
return getInternalPlaywrightPage(); | ||
}; |
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