Skip to content

Commit

Permalink
Merge pull request #126 from realzoberg/resolve-url
Browse files Browse the repository at this point in the history
major: make browser.url work like url.resolve
  • Loading branch information
sipayRT authored Apr 3, 2017
2 parents 4f49fa3 + 727f78d commit ea34216
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
15 changes: 8 additions & 7 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
};
10 changes: 5 additions & 5 deletions test/lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,20 @@ 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) => {
session
.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');
});
});

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

Expand Down

0 comments on commit ea34216

Please sign in to comment.