Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a visible element in Protractor with Angularjs

I have a SPA where there are multiple divs with the same class. I want protractor to select the visible div and click it. I keep getting the Failed: element not visible which makes be believe it's getting some element that isn't on this particular page (maybe?). I also get WARNING - more than one element found for locator By.cssSelector('.myDiv') - the first result will be used which again makes me think it's not clicking the visible one, but the invisible one.

Here is my spec:

describe('User actions', function () {
    it("should be able to click a my div.", function () {
    var myDiv = element(by.css('.myDiv'));
    myDiv.click();

    // some expect... haven't gotten this far yet.
});

How do I select the visible .myDiv and click it?

like image 437
Jeff Avatar asked Oct 08 '15 16:10

Jeff


People also ask

How to hide and show div in AngularJS?

Just get rid of the display: none; from your CSS. AngularJS is in control of showing/hiding that div. If you want to hide it by default, just set the value of scope. myvalue to false initially - which you're already doing.

What is ngShow in Angular?

The ng-show directive shows the specified HTML element if the expression evaluates to true, otherwise the HTML element is hidden.


1 Answers

You can filter() it out:

var myDiv = element.all(by.css('.myDiv')).filter(function (elm) {
    return elm.isDisplayed().then(function (isDisplayed) {
        return isDisplayed;
    });
}).first();
like image 101
alecxe Avatar answered Oct 01 '22 19:10

alecxe