Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor clear a dropdown

Tags:

protractor

While executing protractor tests, I would like to clear a dropdown. I know how to select a particular option in dropdown. How can it be cleared?

The dropdown is part of a form. The values of the options for dropdown are loaded with an ajax call. So, the select looks something like this.

<select>
  <option>First Value</option>
  <option>Second Value</option>
<select>

Now, when the form is loaded, no value is selected. But, as soon as a value is selected it is not possible to clear it (even manually) as this is a mandatory field. It is only possible to select an alternate value from the dropdown.

However, I would like to do a second test, which checks if the value of this field is blank (no selection). For this I have to clear the dropdown. Does protractor provide a way?

Let's say I can't change the order of tests for some reason.

Currently, I reload the entire form in order to clear the dropdown.

like image 349
Aneesh Avatar asked Aug 19 '26 00:08

Aneesh


1 Answers

If the dropdown cannot be typed into, the only way to clear a dropdown using Protractor is to have an option in your dropdown that acts as a default selection before a user has selected 'First Value' or 'Second Value. This can be a blank option as well, like you desire

Something like this:

<select>
    <option></option>
    <option>First Value</option>
    <option>Second Value</option>
</select>

And then select the first option via

element.all(by.tagName('option')).get(0).click();

This should get you the desired effect. Let me know if this helps!

like image 123
modusAndrew Avatar answered Aug 22 '26 03:08

modusAndrew