Skip to content

Commit ba6cf5c

Browse files
authored
🤖 Merge PR DefinitelyTyped#74149 [selenium-webdriver] Fix driver.wait() return type for elementsLocated() by @slhck
1 parent 225b1d5 commit ba6cf5c

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

‎types/selenium-webdriver/lib/webdriver.d.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ export class Condition<T> {
110110
* Defines a condition that will result in a {@link WebElement}.
111111
*/
112112
export class WebElementCondition extends Condition<WebElement> {
113+
/**
114+
* Private discriminator to prevent structural type compatibility with
115+
* Condition<WebElement[]>. This ensures TypeScript correctly resolves
116+
* driver.wait() overloads when using elementsLocated() vs elementLocated().
117+
* @see https://github.com/SeleniumHQ/selenium/issues/14239
118+
*/
119+
private readonly __isWebElementCondition: true;
120+
113121
/**
114122
* @param {string} message A descriptive error message. Should complete the
115123
* sentence "Waiting [...]"

‎types/selenium-webdriver/test/index.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,29 @@ function TestUntilModule() {
599599
conditionWebElements = webdriver.until.elementsLocated(webdriver.By.className("class"));
600600
}
601601

602+
// Test for https://github.com/SeleniumHQ/selenium/issues/14239
603+
// driver.wait(until.elementsLocated(...)) should return Promise<WebElement[]>, not WebElementPromise
604+
async function TestElementsLocatedReturnsArray() {
605+
let driver: webdriver.WebDriver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
606+
607+
// elementsLocated should return Condition<WebElement[]>
608+
// driver.wait should correctly resolve this to Promise<WebElement[]>
609+
const elements: webdriver.WebElement[] = await driver.wait(
610+
webdriver.until.elementsLocated(webdriver.By.css(".foo")),
611+
);
612+
613+
// These should compile - elements is an array
614+
const length: number = elements.length;
615+
elements.forEach((el: webdriver.WebElement) => el.click());
616+
const mapped: string[] = elements.map((el: webdriver.WebElement) => "test");
617+
618+
// elementLocated (singular) should still return WebElementPromise
619+
const singleElement: webdriver.WebElement = await driver.wait(
620+
webdriver.until.elementLocated(webdriver.By.css(".foo")),
621+
);
622+
singleElement.click();
623+
}
624+
602625
function TestShadowRoot() {
603626
let driver: webdriver.WebDriver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
604627

0 commit comments

Comments
 (0)