Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(types): fix waitForSelector typing to not union null when appropriate (#6344)

Jason Harwig42a556665316
diff --git a/types/types.d.ts b/types/types.d.ts
index 51616743fb..540c2e7928 100644
--- a/types/types.d.ts
+++ b/types/types.d.ts
@@ -21,10 +21,10 @@ import { Readable } from 'stream';
import { Serializable, EvaluationArgument, PageFunction, PageFunctionOn, SmartHandle, ElementHandleForTag, BindingSource } from './structs';
type PageWaitForSelectorOptionsNotHidden = PageWaitForSelectorOptions & {
- state: 'visible'|'attached';
+ state?: 'visible'|'attached';
};
type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelectorOptions & {
- state: 'visible'|'attached';
+ state?: 'visible'|'attached';
};
/**
diff --git a/utils/generate_types/overrides.d.ts b/utils/generate_types/overrides.d.ts
index fa80db64a5..406b13d8db 100644
--- a/utils/generate_types/overrides.d.ts
+++ b/utils/generate_types/overrides.d.ts
@@ -20,10 +20,10 @@ import { Readable } from 'stream';
import { Serializable, EvaluationArgument, PageFunction, PageFunctionOn, SmartHandle, ElementHandleForTag, BindingSource } from './structs';
type PageWaitForSelectorOptionsNotHidden = PageWaitForSelectorOptions & {
- state: 'visible'|'attached';
+ state?: 'visible'|'attached';
};
type ElementHandleWaitForSelectorOptionsNotHidden = ElementHandleWaitForSelectorOptions & {
- state: 'visible'|'attached';
+ state?: 'visible'|'attached';
};
export interface Page {
diff --git a/utils/generate_types/test/test.ts b/utils/generate_types/test/test.ts
index 87249cbe6a..964456d923 100644
--- a/utils/generate_types/test/test.ts
+++ b/utils/generate_types/test/test.ts
@@ -675,47 +675,58 @@ playwright.chromium.launch().then(async browser => {
}
}
+ type AssertCanBeNull<T> = null extends T ? true : false
+
const frameLikes = [page, frame];
for (const frameLike of frameLikes) {
{
const handle = await frameLike.waitForSelector('body');
+ const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
+ const canBeNull: AssertCanBeNull<typeof handle> = false
+ }
+ {
+ const handle = await frameLike.waitForSelector('body', {timeout: 0});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = false;
+ const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const state = Math.random() > .5 ? 'attached' : 'visible';
const handle = await frameLike.waitForSelector('body', {state});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = false;
+ const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const handle = await frameLike.waitForSelector('body', {state: 'hidden'});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = true;
+ const canBeNull: AssertCanBeNull<typeof handle> = true;
}
{
const state = Math.random() > .5 ? 'hidden' : 'visible';
const handle = await frameLike.waitForSelector('body', {state});
const bodyAssertion: AssertType<playwright.ElementHandle<HTMLBodyElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = true;
+ const canBeNull: AssertCanBeNull<typeof handle> = true;
}
-
{
const handle = await frameLike.waitForSelector('something-strange');
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = false;
+ const canBeNull: AssertCanBeNull<typeof handle> = false;
}
+ {
+ const handle = await frameLike.waitForSelector('something-strange', {timeout: 0});
+ const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
+ const canBeNull: AssertCanBeNull<typeof handle> = false;
+ }
{
const state = Math.random() > .5 ? 'attached' : 'visible';
const handle = await frameLike.waitForSelector('something-strange', {state});
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = false;
+ const canBeNull: AssertCanBeNull<typeof handle> = false;
}
{
const state = Math.random() > .5 ? 'hidden' : 'visible';
const handle = await frameLike.waitForSelector('something-strange', {state});
const elementAssertion: AssertType<playwright.ElementHandle<HTMLElement|SVGElement>, typeof handle> = true;
- const canBeNull: AssertType<null, typeof handle> = true;
+ const canBeNull: AssertCanBeNull<typeof handle> = true;
}
}