Freewind
Freewind

Reputation: 198198

How to check if an element is not in the page anymore?

I have defined a directive, which can be used on a button, and if the button is clicked, it will remote some elements from document, the code looks like:

.directive("mydirective", function() {
  return {
     link: function(scope, element) {
        element.bind('click', function() {
           $timeout(function() {
               angular.element("#aaa").remove();
           }, 1000);
        });
     }
});

Now I want to test it with protractor, the code looks like:

$("#mybutton").click();   // the button has the 'mydirective'

// wait until "#aaa" is removed
browser.wait(function() {
    var deferred = protractor.promise.defer();
    $("#aaa").isPresent().then(function(data) {       // !!!!
        deferred.fulfill(!data);
    });
    return deferred.promise;
}, 2000);

// checking
expect($("#aaa").isPresent()).toBe(false);

But when I run the test, it will report:

Error: Locator cannot be empty

on the !!!! line in the code.

Why and how to fix it?

Upvotes: 0

Views: 80

Answers (2)

alecxe
alecxe

Reputation: 473833

Why don't use the ElementFinder:

The ElementFinder can be treated as a WebElement for most purposes, in particular, you may perform actions (i.e. click, getText) on them as you would a WebElement. ElementFinders extend Promise, and once an action is performed on an ElementFinder, the latest result from the chain can be accessed using then. Unlike a WebElement, an ElementFinder will wait for angular to settle before performing finds or actions.

element(by.id('mybutton')).click();
expect(element(by.id('aaa')).isPresent()).toBe(false);

UPD. Turned out that isElementPresent() eventually worked in this case:

expect(browser.isElementPresent(by.id('aaa'))).toBe(false);

Upvotes: 1

Brennan Cheung
Brennan Cheung

Reputation: 4541

You don't need the promises. They are implied.

browser.driver.wait ->
    browser.driver.isElementPresent(By.id 'aaa').then (present) -> !present

Upvotes: 1

Related Questions