typescriptprotractorcucumberbddatdd

Redirect URL test using Cucumber Protractor and Typescript


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');
    });        
});

Solution

  • 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`)
    }