Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add a custom command #17

Open
zac11 opened this issue Apr 28, 2020 · 1 comment
Open

How to add a custom command #17

zac11 opened this issue Apr 28, 2020 · 1 comment

Comments

@zac11
Copy link

zac11 commented Apr 28, 2020

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

@dimadeveatii
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants