-1

I'm writing a functional automated test framework against a windows application using Typescript / selenium-appium / WinAppDriver. I've run into an issue where I need to perform a get all elements (findElements), and select a specific one based on the element's index. The issue I'm running into here is with passing the index of the element into a custom function I've written to select a dropdown element.

I must have the type incorrectly defined, or I'm passing the web element incorrectly, because I've tried several combinations of types: Promise<WebElement[]> WebElement[]> Array<WebElement> etc

Here's the function I wrote...

/**
   * Standard function to handle selecting a dropdown item
   *
   * @param dropdown - The dropdown to expand
   * @param dropdownItem - The actual item from the open dropdown
   * 
   */
  async selectDropdownItem(dropdown: Promise<WebElement[]>, dropdownItem: string) {
    if (dropdown instanceof WebElement) {
      await dropdown.click;
    }
    await this.openButton.click();
    await driver.findElement(By2.nativeName(dropdownItem)).click();
  }

Here's the locator for the element I'm trying to pass into this function

async dropDowns() { return await this.driver.findElements(By2.nativeClassName('WindowsForms10.COMBOBOX.app.0.37504c8_r8_ad1')); }

Here's where I pass the element into the selectDropdownItem() function I wrote above

await MainPage.selectDropdownItem(this.dropDowns[0],'Test Scenario 1');

The error is always the same, the type I pass into the function can't be resolved with the type I defined in the function. Anyone have any ideas?

I've tried using several different types for the parameter in the function definition, and I can't seem to land on one that can be resolved. I need to target specific indexes of the findElements returned array and pass those into my function to handle selecting the item from the dropdown.

  • `dropdown: Promise` then `if (dropdown instanceof WebElement)`? And if `this.dropDowns` is a _method_ returning a _promise_ then what's `this.dropDowns[0]` going to be? – jonrsharpe Apr 18 '23 at 19:24
  • So I guess the ultimate goal is essentially to just pass an index of an array of web elements to the function that I wrote. So `this.dropDowns[0]` would in theory point to a specific element in the array of elements that the selector returns. Also, I added the conditional because without the `if (dropdown instanceOf WebElement)` I get this... `Property 'click' does not exist on type 'Promise'`. – John Kelley Apr 18 '23 at 21:26
  • Removing async / await from selector and referencing it this way `this.dropDowns[0]` gives me this `Element implicitly has an 'any' type because expression of type '0' can't be used to index type 'Promise'` – John Kelley Apr 18 '23 at 21:40

1 Answers1

0

I believe I've fixed the problem.

the dropdown param is now a type of WebElement

async selectDropdownItem(dropdown: WebElement, dropdownItem: string) {
  await dropdown.click;
  await this.openButton.click();
  await driver.findElement(By2.nativeName(dropdownItem)).click();
}

the selector is a standard getter

get dropdowns() { return this.driver.findElements(By2.nativeClassName('WindowsForms10.COMBOBOX.app.0.37504c8_r8_ad1')); }

calling the function this way

const dropdownItems = await this.ceclConfigDropdowns;
await MainPage.selectDropdownItem(dropdownItems[0],'Test Scenario 1');

Seems like setting the getter to a const and resolving it first is what worked, now we can access the index and pass it into my function

Tyler2P
  • 2,324
  • 26
  • 22
  • 31