Search code examples
javascriptseleniumtestingprotractorend-to-end

How to get the value from selectbox?


I am trying to verify that correct option is selected from selectbox using protractor.

This is how I pick value from selectbox:

element(by.id('form-mileage-unit')).click().then(function() {
    element(by.cssContainingText('option', browser.params.lengthUnit)).click();         
});;

So base on this I write code below:

it('Verify paint color', function() {       
    element(by.id('form-mileage-unit')).click().then(function() {
        element(by.cssContainingText('option', browser.params.lengthUnit).getAttribute("value")).toEqual(browser.params.lengthUnit);            
    });;
});     

Unfortunately I am getting error:

TypeError: Object by.cssContainingText("option", "Motohodin") has no method 'getText'

Can someone advise me with this?


Solution

  • You are closing the parenthesis in the wrong place and there is no expect(). Replace:

    element(by.cssContainingText('option', browser.params.lengthUnit).getAttribute("value")).toEqual(browser.params.lengthUnit);
    

    with:

    var option = element(by.cssContainingText('option', browser.params.lengthUnit));
    expect(option.getAttribute("value")).toEqual(browser.params.lengthUnit);
    

    If you are dealing with select->option constructions a lot, consider using an abstraction over it that would make your tests cleaner and more readable, see: