Search code examples
javascriptangularjstestingprotractorend-to-end

How to count total number of option of drop-down in protractor


I am trying to verify number of options of drop-down but it is not working. I have used the following code:

campaignManagementPage.statusDropDown().Count();
this.statusDropDown = function () {
    return element(by.model('campaign.lifeStage'));
}

HTML for drop down is following:

<select class="form-control ng-pristine ng-valid ng-touched" data-ng- model="campaign.lifeStage">
    <option value="design">Design</option>
    <option value="preview">Preview</option>
    <option value="live">Live</option>
    <option value="completed">Completed</option>
</select>`

Solution

  • Define a separate field in your page object:

    this.statusDropDownItems = function () {
        return element.all('select.form-control option');
    }
    

    or relying on the model and chaining element and element.all:

    this.statusDropDownItems = function () {
        return element(by.model('campaign.lifeStage')).element.all('option');
    }
    

    Then, in your test spec you can use expect to assert the count:

    expect(page.statusDropDownItems.count()).toEqual(4);
    

    where page is your Page Object instance.