Basti
Basti

Reputation: 686

Nightwatch get URL part in test

In the current nightwatch e2e test that im a writing, I need to save the last part of the browsers current url to a variable.

The steps I need to do are the following:

  1. Load url: http://master.local
  2. Click on a Link
  3. Get the current url (could be for example: http://master.local/node/123)
  4. Save last part of the URL, in the prev. case "123", to a variable

Could someone give me a suggestion how I could achieve step 3 and 4?

Upvotes: 1

Views: 872

Answers (1)

Alapan Das
Alapan Das

Reputation: 18634

You can do it using javascript. Below is the code for reference.

module.exports = {

'Test 1': function (browser) {

    var url, urlTrimmed;

    browser
        .url('http://master.local')
        .click('selector')
        .execute(function () {
            urlData = {
                wholeUrl: document.URL,
                trimmedURL: document.URL.split('/')[4]
            };
            return urlData
        }, [], function (result) {
            url = result.value.wholeUrl;
            urlTrimmed = result.value.trimmedURL
        });
    browser.perform(function () {
        console.log("The url is" + url);
        console.log("The last part of the Url is" + urlTrimmed);
    });
}
};

Upvotes: 1

Related Questions