Redirect URL test using Cucumber Protractor and Typescript

1.7k Views Asked by At

The last step in the code fails with Assertion error, since the actual value remains LoginPage, I guess because the step complete before the browser is actually redirected to HomePage.

I tried using browser.sleep(10000) and browser.wait(), but they din't work for me. What is the correct way to deal with this kind of issue?

import {browser, by, protractor} from 'protractor';
import { ClientPage } from '../pages/offerScreenPage';
import {CallbackStepDefinition, defineSupportCode} from 'cucumber';
import {By} from "selenium-webdriver";
import {Events} from "../pages/Event";
let chai = require('chai').use(require('chai-as-promised'));
let expect = chai.expect;

defineSupportCode(function ({ Given, When, Then}) {
    let client: ClientPage = new ClientPage();

    Given(/^User is in Login Page $/, async () => {
        await expect(browser.getTitle()).to.eventually.equal('LoginPage');
    });

    When(/^User enters credentials$/, async () => {
        await client.userId.sendKeys("abc123");
        await client.password.sendKeys("passwod");
    });

    When(/^User clicks the submit button$/, async () => {
        await client.submit.click();
    });

    Then(/^User is redirected to a new page$/, async () => {
        await expect(browser.getTitle()).to.eventually.equal('HomePage');
    });        
});
3

There are 3 best solutions below

0
On BEST ANSWER

Your wait function is over complicated. If you are using async/await, your code can be simpler. And trick with saving url outside of promise won't work, your variable will be undefined until promise will be resolved. Also i do not recommend to use absolute URLs in real project, this will be pain if your environment url will be changed. Store it as some config variable, and append needed path.

Check this:

async function waitForUrlToChangeTo(URL) {
    let urlIsChangedTo = async () => (await browser.getCurrentUrl()) == URL
    return browser.wait(urlIsChangedTo, 10000, `Expected URL to be changed to ${URL} in 10 seconds, but it wasn't`)
}
1
On

2 approaches

1) Wait for new url: http://www.protractortest.org/#/api?view=ProtractorExpectedConditions.prototype.urlContains

var EC = protractor.ExpectedConditions;
// Waits for the URL to contain 'foo'.
browser.wait(EC.urlContains('foo'), 5000);

2) Wait for some unique elements that exist only on that page:

http://www.protractortest.org/#/api?view=ProtractorExpectedConditions.prototype.visibilityOf

var EC = protractor.ExpectedConditions;
// Waits for the element with id 'abc' to be visible on the dom.
browser.wait(EC.visibilityOf($('#abc')), 5000);

Optionally pass third parameter to browser.wait() to provide nice error message:

browser.wait(EC.visibilityOf($('ololo')), 5000, 'Expected to be on Home page, but element ololo was not became visible in 5 seconds')
0
On
import {browser, by, protractor} from 'protractor';
import { ClientPage } from '../pages/offerScreenPage';
import {CallbackStepDefinition, defineSupportCode} from 'cucumber';
import {By} from "selenium-webdriver";
import {Events} from "../pages/Event";
let chai = require('chai').use(require('chai-as-promised'));
let expect = chai.expect;

defineSupportCode(function ({ Given, When, Then}) {
    let client: ClientPage = new ClientPage();

/**
 * @name waitForUrlToChangeTo
 * @description Wait until the URL changes to match a provided regex
 * @param {RegExp} urlRegex wait until the URL changes to match this regex
 * @returns {!webdriver.promise.Promise} Promise
 */
function waitForUrlToChangeTo(urlRegex) {
    let currentUrl;

    return browser.getCurrentUrl().then(function storeCurrentUrl(url) {
            currentUrl = url;
        }
    ).then(function waitForUrlToChangeTo() {
            return browser.wait(function waitForUrlToChangeTo() {
                return browser.getCurrentUrl().then(function compareCurrentUrl(url) {
                    return url == urlRegex;
                });
            });
        }
    );
}


    Given(/^User is in Login Page $/, async () => {
        await expect(browser.getTitle()).to.eventually.equal('LoginPage');
    });

    When(/^User enters credentials$/, async () => {
        await client.userId.sendKeys("abc123");
        await client.password.sendKeys("passwod");
    });

    When(/^User clicks the submit button$/, async () => {
        await client.submit.click();
    });

    Then(/^User is redirected to a new page$/, async () => {
        await waitForUrlToChangeTo("https://github.com/login")
        await expect(browser.getTitle()).to.eventually.equal('HomePage');
    });        
});