labs42io / web-automation

BDD tests with Cucumber, WebdriverIO and Docker Selenium

Home Page:https://labs42io.github.io/web-automation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add a custom command

zac11 opened this issue · comments

First of all thanks for this awesome setup. Makes life a lot easier for me.

I am trying to add a custom command to make the page wait for a URL to change to a particular route.

I made the following changes but I'm not able to get the command to work.

Here is my feature -

Scenario: Checking the URL
Given I open the url "https://abcd.somedomain.com/route1/#/"
When I wait for URL to change to "https://abcd.somedomain.com/route1/#/subroute" for 10s

  • Add the code for custom command in a new file under /src/actions/waitForUrl.ts
 * @alias browser.waitForUrl
 * @param {string|RegExp|Function} value
 * @param {number} timeout — ms
 * @param {number} revert
 * @returns {boolean}
  */

export const waitForUrl = {

    waitForUrl: function (value, timeout, revert) {
        let url, actual;

        try {
            return this.waitUntil(() => {
                url = this.getUrl();
                actual = value === url;

                if (typeof value === 'string' && !value.endsWith('/')) {
                    url = url.replace(/\/$/, '');
                }

                if (typeof value === 'function') {
                    actual = value(url);
                } else if (value[Symbol.match]) {
                    actual = value.test(url);
                }

                return value && actual && !revert;
            }, timeout, '');
        } catch (error) {
            let message = 'Could not wait for the required url:';
            message += `\n\tActual: ${url}`;
            message += `\n\tExpected: ${actual}`;

            throw new Error(message);
        }
    }
}
  • Call this under the config/index.ts file
export const config = {

  before: function name(capabilities: any, specs) {
    Object.keys(waitForUrl.waitForUrl).forEach((key) => {
      browser.addCommand(key, waitForUrl.waitForUrl[key])
    })
  },
  runner: 'local',
.....
  • Try to get this to work in the when steps
When(/^ I wait for URL to change to "([^"]*)?" for "([^"]*)?"s$/,
  waitForUrl.waitForUrl();

However this shows error stating the there are 3 arguments expected but 0 was provided, which I'm failing to understand. Surely there is something that I am missing. Can you please take a look and let me know where the issue lies.

TY

Hi @zac11, you should pass to when the function reference, not the invocation:

When(/^I wait for URL to change to "([^"]*)?" for "([^"]*)?"s$/, waitForUrl.waitForUrl);