From 727f78d95e957cdf89ca95e72f52cd01e300d96e Mon Sep 17 00:00:00 2001 From: Ruslan Login Date: Mon, 20 Mar 2017 17:56:11 +0300 Subject: [PATCH] feat: make browser.url work like url.resolve --- lib/browser.js | 15 ++++++++------- test/lib/browser.js | 10 +++++----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/browser.js b/lib/browser.js index fd964e6e4..711d7962c 100644 --- a/lib/browser.js +++ b/lib/browser.js @@ -3,6 +3,7 @@ const _ = require('lodash'); const q = require('q'); const URI = require('urijs'); +const url = require('url'); const webdriverio = require('webdriverio'); const logger = require('./utils').logger; @@ -136,17 +137,17 @@ module.exports = class Browser { const baseUrlFn = session.url.bind(session); session.addCommand('url', (uri) => { - if (uri) { - this._meta.url = this._getMetaUrl(uri); + if (!uri) { + return baseUrlFn(uri); } - return baseUrlFn(uri); + const newUri = this._resolveUrl(uri); + this._meta.url = newUri; + return baseUrlFn(newUri); }, true); // overwrite original `url` method } - _getMetaUrl(uri) { - return this._config.baseUrl && /^(\/|\?)/.test(uri) - ? URI(this._config.baseUrl + uri).normalize().toString() - : uri; + _resolveUrl(uri) { + return this._config.baseUrl ? url.resolve(this._config.baseUrl, uri) : uri; } }; diff --git a/test/lib/browser.js b/test/lib/browser.js index 9fef1d13a..518bf5a6b 100644 --- a/test/lib/browser.js +++ b/test/lib/browser.js @@ -211,12 +211,12 @@ describe('Browser', () => { .then(() => { session.url('/foo/bar?baz=qux'); - assert.calledWith(baseUrlFn, '/foo/bar?baz=qux'); + assert.calledWith(baseUrlFn, 'http://base_url/foo/bar?baz=qux'); assert.calledOn(baseUrlFn, session); }); }); - it('should add last url to meta-info if it starts from /', () => { + it('should add last url to meta-info and replace path if it starts from /', () => { return mkBrowser_({baseUrl: 'http://some.domain.org/root'}) .init() .then((browser) => { @@ -224,7 +224,7 @@ describe('Browser', () => { .url('/some/url') .url('/foo/bar?baz=qux'); - assert.equal(browser.meta.url, 'http://some.domain.org/root/foo/bar?baz=qux'); + assert.equal(browser.meta.url, 'http://some.domain.org/foo/bar?baz=qux'); }); }); @@ -238,13 +238,13 @@ describe('Browser', () => { }); }); - it('should not concat url without slash at the beginning to the base url', () => { + it('should concat url without slash at the beginning to the base url', () => { return mkBrowser_({baseUrl: 'http://some.domain.org'}) .init() .then((browser) => { session.url('some/url'); - assert.equal(browser.meta.url, 'some/url'); + assert.equal(browser.meta.url, 'http://some.domain.org/some/url'); }); });