|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const _ = require('lodash'); |
| 4 | +const wrapUrlCommand = require('lib/commands/url'); |
| 5 | +const {PAGE_LOAD_TIMEOUT} = require('lib/constants'); |
| 6 | +const {mkBrowser_} = require('../../utils'); |
| 7 | + |
| 8 | +describe('"url" command', () => { |
| 9 | + let browser, initialDocument; |
| 10 | + |
| 11 | + const wrapUrlCommand_ = (browser, opts = {}) => { |
| 12 | + opts = _.defaultsDeep(opts, { |
| 13 | + pageLoadTimeout: null |
| 14 | + }); |
| 15 | + |
| 16 | + wrapUrlCommand(browser, opts); |
| 17 | + }; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + browser = mkBrowser_(); |
| 21 | + initialDocument = global.document; |
| 22 | + global.document = {body: {remove: sinon.stub()}}; |
| 23 | + }); |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + sinon.restore(); |
| 27 | + global.document = initialDocument; |
| 28 | + }); |
| 29 | + |
| 30 | + it('should wrap "url" command', () => { |
| 31 | + wrapUrlCommand_(browser); |
| 32 | + |
| 33 | + assert.calledOnceWith(browser.addCommand, 'url', sinon.match.func, true); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should call base "url" command if url is not passed', async () => { |
| 37 | + const baseUrlFn = browser.url; |
| 38 | + wrapUrlCommand_(browser); |
| 39 | + |
| 40 | + await browser.url(); |
| 41 | + |
| 42 | + assert.calledOnce(baseUrlFn); |
| 43 | + assert.notCalled(browser.execute); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should remove body element from the page before make request', async () => { |
| 47 | + const baseUrlFn = browser.url; |
| 48 | + wrapUrlCommand_(browser); |
| 49 | + |
| 50 | + browser.execute.callsFake(() => { |
| 51 | + browser.execute.firstCall.args[0](); |
| 52 | + }); |
| 53 | + |
| 54 | + await browser.url('/?text=test'); |
| 55 | + |
| 56 | + assert.calledOnceWith(browser.execute, sinon.match.func); |
| 57 | + assert.calledOnce(global.document.body.remove); |
| 58 | + assert.callOrder(browser.execute, baseUrlFn); |
| 59 | + }); |
| 60 | + |
| 61 | + it('should wait until request will be completed', async () => { |
| 62 | + wrapUrlCommand_(browser, {pageLoadTimeout: 100500}); |
| 63 | + |
| 64 | + browser.waitUntil.callsFake(() => { |
| 65 | + browser.waitUntil.firstCall.args[0](); |
| 66 | + }); |
| 67 | + |
| 68 | + await browser.url('/?text=test'); |
| 69 | + |
| 70 | + assert.calledOnceWith( |
| 71 | + browser.waitUntil, |
| 72 | + sinon.match.func, |
| 73 | + 100500, |
| 74 | + 'The page did not load in 100500 ms' |
| 75 | + ); |
| 76 | + assert.calledOnceWith(browser.isVisible, 'body'); |
| 77 | + }); |
| 78 | + |
| 79 | + it('should use default "pageLoadTimeout" if it does not specified in browser config', async () => { |
| 80 | + wrapUrlCommand_(browser, {pageLoadTimeout: null}); |
| 81 | + |
| 82 | + await browser.url('/?text=test'); |
| 83 | + |
| 84 | + assert.calledOnceWith( |
| 85 | + browser.waitUntil, |
| 86 | + sinon.match.func, |
| 87 | + PAGE_LOAD_TIMEOUT, |
| 88 | + `The page did not load in ${PAGE_LOAD_TIMEOUT} ms` |
| 89 | + ); |
| 90 | + }); |
| 91 | +}); |
0 commit comments