Protractor: Get text of an alert?

Inside your test, get the current Protractor instance and use switchTo().alert() to access the alert dialog box:

var ptor = protractor.getInstance();

var alertDialog = ptor.switchTo().alert();

expect(alertDialog.getText()).toEqual("Hello");

Keep in mind that Protractor is basically just a wrapper for Selenium WebDriver so, as far as I know, anything you can do with Selenium WebDriver you can do with Protractor.

Edited to include the full test:

describe('Alert dialog', function () {

    var ptor = protractor.getInstance(),
        button;

    beforeEach(function () {
        // This line is necessary on my end to get to my test page.
        // browser.driver.get('http://localhost:8000/test.html');
        button = ptor.findElement(protractor.By.id('alertButton'));
        button.click();
    });

    it('tells the alert message', function () {
        var alertDialog = ptor.switchTo().alert();
        expect(alertDialog.getText()).toEqual("Hello");
    });

});

It is possible that your app is still initializing at the point that your test is executed, which would explain why no dialog seems to be appearing. Ensure that your app is "ready to go" and can actually display the alert before making your assertion. Hope that helps!


You have to wait for the browser to open/display the alert. Example using Protractor 2.2.0:

   var timeoutInMilliseconds = 1000;
   browser.wait(protractor.ExpectedConditions.alertIsPresent(), timeoutInMilliseconds);
   var alertDialog = browser.switchTo().alert();
   expect(alertDialog.getText()).toEqual("Hello World!");