d_z90
d_z90

Reputation: 1263

Click() and then() not working - Protractor

I am writing a test with protractor and my aim is click an element and check if it has a specific class. The issue is that I am using click(), followed by then(), but I am getting the following error:

Cannot read property 'getAttribute' of null

The issue is located in the following chunk of code:

element(by.css('#region1 polygon:first-child')).click()
    .then(function(selected){
        expect(selected.getAttribute('class')).toContain('highlighted');
    });

Do you have an idea on how to solve this issue? Thanks in advance for your replies!!

Upvotes: 2

Views: 417

Answers (1)

alecxe
alecxe

Reputation: 473873

The click() callback does not have an element itself as an argument. In other words, selected in your case is not an element.

Instead, just do it step by step and let the Control Flow queue do the job:

var elm = element(by.css('#region1 polygon:first-child'));

elm.click();
expect(elm.getAttribute('class')).toContain('highlighted');

Note that toContain() is not the best matcher to apply to the class attribute value. For example, this test would pass if an element would have a not-highlighted class. A better way to do that is to introduce a custom toHaveClass matcher, please see:

Upvotes: 3

Related Questions