user2020347
user2020347

Reputation: 883

How do I return the text of an alert in Protractor?

I have a test that fills out a create customer page. Upon clicking save, an alert pops up with the newly generate guid of the customer. I'm trying to make a function that clicks save, captures the alert text, and then returns it to my protractor test to use later.

I am not understanding the way these promises work, and was hoping for some help!

Currently, I use this function to click the save button, then check the alert to contain a message:

function saveContactAndCheckMessage(saveMessage){
    var addContact = new addContactPage;
    addContact.saveButton.click().then(function(){
        browser.wait(function(){
            return browser.switchTo().alert().then(
                function(){return true;},
                function(){return false;}
            );
        },3000);
        var popupAlert = browser.switchTo().alert();
        expect(popupAlert.getText()).toContain(saveMessage);
        popupAlert.dismiss();
    });
};

With this function, it waits until the alert is opened, and then checks the message. I'd like to alter it a bit so it doesn't check the message, instead it returns the message for my Protractor test to store and use later.

function saveContactAndReturnGuid(){
    var addContact = new addContactPage;
    addContact.saveButton.click().then(function(){
        browser.wait(function(){
            return browser.switchTo().alert().then(
                function(){return true;},
                function(){return false;}
            );
        },3000);
        var popupAlert = browser.switchTo().alert();
        var result = popupAlert.getText();
        popupAlert.dismiss();
        return result;
    });
};

And then the code calling it:

 var customerGuid = addContactFunctions.saveContactAndReturnGuid();
 expect(customerGuid).toMatch('Example to check what the real guid value is');

Unfortunately, this function returns an undefined variable. If I were to alter it slightly:

        ...
        var popupAlert = browser.switchTo().alert();
        var result = popupAlert.getText();
        expect(result).toMatch('Test');
        return result;
        ...

The expect is able to see the guid generated just fine inside of the function. Even with the return directly after expect though, it still returns 'undefined'.

I know this is due to it being asynchronous, but I'm not really sure what to look up to solve this. Any ideas of where to go with this one?

Edit: An alternate that still shows the data as undefined outside of the function.

    var customerGuid;

    addContact.saveButton.click().then(function(){
        browser.wait(function(){
            return browser.switchTo().alert().then(
                function(alert){
                    expect(alert.getText()).toContain('test'); <---- This shows the alert message correctly
                    customerGuid = alert.getText(); <---- This shows up as undefined later
                    alert.dismiss();
                    return true;
                },
                function(){
                    return false;
            });
        },3000);
    });

expect(customerGuid).toMatch('test'); <---- Fails, undefined

Upvotes: 2

Views: 2370

Answers (2)

user2020347
user2020347

Reputation: 883

Here is the solution I ended up with:

addContact_spec.js

var addContactFunctions = require('./../../pages/addContact/addContact_functions.js');

describe('From an alert, ', function(){
     it('the alert text can be saved', function(){
         var addContactFuncts = new addContactFunctions();

        // Save and get the customer guid
        var custGuid;
        custGuid = addContactFuncts.saveAndReturnGuid();
        expect(custGuid).toContain('randomguid');  <---- This will fail, but have the real guid inside
    });    
});

addContact_functions.js

var addContactPage = require('./../../pages/addContact/addContact_page.js');

module.exports = function(){
this.saveAndReturnGuid = saveAndReturnGuid;

function saveAndReturnGuid() {
    var addContact = new addContactPage();
    return addContact.saveButton.click().then(function (alertText) {
        browser.wait(function () {
            return browser.switchTo().alert().then(
                function () {return true;},
                function () {return false;}
            ); 
        }, 3000);
        var popupAlert = browser.switchTo().alert();
        alertText = popupAlert.getText();
        popupAlert.dismiss();
        return alertText;
    });
};
}

I need the browser.wait and return browser.switchTo.alert for it to wait for the alert to pop up. Finding the right space to put the alertText was just trial and error due to my limited knowledge.

Upvotes: 3

adamweeks
adamweeks

Reputation: 1332

What you're looking for is to get the alert in the then function.

var myAlertText;
browser.switchTo().alert()
    .then(function (alert) {
        if (alert) {
            alert.getText(function(alertText){
                expect(alertText).toContain('Test');
                myAlertText = alertText;
            });
            return alert.dismiss();
        }
    });

Upvotes: 2

Related Questions