How to handle modal-dialog box in Protractor?

2019-04-16 21:47发布

问题:

I am trying to use sendKeys() on a modal-dialog box on this website. This dialog box appears after clicking Sign In button. I cannot seem to find any way to switch focus on the box. See the gist

I tried using browser.driver.switchTo().activeElement(); in

InvalidLogInUnSuccess: {
        get: function () {
            this.loginButton.click();
            browser.driver.switchTo().activeElement();
            this.email.sendKeys("Test");
        }
    }

with no luck and throws ElementNotVisibleError

Message: ElementNotVisibleError: element not visible (Session info: chrome=41.0.2272.101) (Driver info: chromedriver=2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf),platform=Windows NT 6.3 x86_64) Stacktrace: ElementNotVisibleError: element not visible

回答1:

I've experienced a similar issue while testing an internal application when a popup was being opened with an animation effect (I think it is a culprit here) which had me think about waiting for an element inside the popup to become visible.

visibilityOf expected condition works for me in this case:

var email = element(by.css('.container.login.ng-scope #email'));
browser.wait(EC.visibilityOf(email), 5000);

email.sendKeys('test');

where EC is something I usually define globally in the onPrepare():

onPrepare: function () {
    ...

    global.EC = protractor.ExpectedConditions;
},

Just a side note, I think the locator could be improved here:

  • ng-scope is not something I would rely on
  • there is a model defined on the email field, how about:

    element(by.model('email'));
    

FYI, the complete spec I've executed:

"use strict";

describe("gifteng test", function () {
    var scope = {};

    beforeEach(function () {
        browser.get("http://www.gifteng.com/?login");
        browser.waitForAngular();
    });

    describe("Logging in", function () {
        it("should send keys to email", function () {
            var email = element(by.css('.container.login.ng-scope #email'));
            browser.wait(EC.visibilityOf(email), 5000);

            email.sendKeys('test');
        });

    });
});