Automate dropdown selection by text



I’m trying to select a dropdown option (simple html select tag) by text in a TypeScript/Protractor project and while doing this I found this guide: How to automate Dropdown menu selections

Unfortunately it does not provide any samples for TypeScript. How can I do that?

Windows 10 64-bit desktop PC
Webtestit 1.10


Hello @TSKdx
Using Protractor you could make use of the by.cssContainingText() method.
In the how-to guide that you referred to, we used the seleniums select() package to pick an option from the dropdown by text.
Supposing we have to do the same thing with Protractor, and our selector is [name=‘country’] > option (which returns all the options from the dropdown) you could make something like this:

public async selectOptionByText(text: string): Promise<YourPoFile> {
    await browser.wait(ExpectedConditions.visibilityOf(element(this.dropdown)), browser.allScriptsTimeout, this.dropdown.toString());

    await element(by.cssContainingText('option', text)).click();

    return this;

where this.dropdown is your selector, and the method takes a text argument for the specific select option that you need.

Happy Testing!


Hello @smatijas

Thanks, got it working. I didn’t see the forest for the trees.

closed #4