Best JavaScript code snippet using cypress
browser-pool.js
Source:browser-pool.js
...41 it('should create browser with correct args', async () => {42 const config = stubConfig();43 const emitter = new EventEmitter();44 const browserPool = createPool({config, emitter});45 const browser = stubBrowser({browserId: 'bro-id'});46 Browser.create.withArgs(config, 'bro-id', undefined, emitter).returns(browser);47 await browserPool.getBrowser({browserId: 'bro-id'});48 assert.calledOnceWith(Browser.create, config, 'bro-id', undefined, emitter);49 });50 it('should create specific version of browser with correct args', async () => {51 const config = stubConfig();52 const emitter = new EventEmitter();53 const browserPool = createPool({config, emitter});54 const browser = stubBrowser({browserId: 'bro-id'});55 Browser.create.withArgs(config, 'bro-id', '10.1', emitter).returns(browser);56 await browserPool.getBrowser({browserId: 'bro-id', browserVersion: '10.1'});57 assert.calledOnceWith(Browser.create, config, 'bro-id', '10.1', emitter);58 });59 it('should create a new browser if there are no free browsers in a cache', () => {60 const config = stubConfig();61 const browserPool = createPool({config});62 const browser = stubBrowser({browserId: 'bro-id'});63 Browser.create.withArgs(config, 'bro-id').returns(browser);64 return assert.becomes(browserPool.getBrowser({browserId: 'bro-id'}), browser);65 });66 it('should create a new browser with specific version if there are no free browsers in a cache', () => {67 const config = stubConfig();68 const browserPool = createPool({config});69 const browser = stubBrowser({browserId: 'bro-id'});70 Browser.create.withArgs(config, 'bro-id', '10.1').returns(browser);71 return assert.becomes(browserPool.getBrowser({browserId: 'bro-id', browserVersion: '10.1'}), browser);72 });73 it('should init a new created browser if there are no free browsers in a cache', async () => {74 const browser = stubBrowser({browserId: 'bro-id'});75 Browser.create.returns(browser);76 await createPool().getBrowser({77 browserId: 'bro-id', sessionId: '100-500', sessionCaps: 'some-caps', sessionOpts: 'some-opts'78 });79 assert.calledOnceWith(80 browser.init,81 {sessionId: '100-500', sessionCaps: 'some-caps', sessionOpts: 'some-opts'},82 sinon.match.instanceOf(Calibrator)83 );84 });85 it('should emit "NEW_BROWSER" event on creating of a browser', async () => {86 const emitter = new EventEmitter();87 const onNewBrowser = sandbox.spy().named('onNewBrowser');88 const browserPool = createPool({emitter});89 emitter.on(RunnerEvents.NEW_BROWSER, onNewBrowser);90 Browser.create.returns(stubBrowser({id: 'bro-id', publicAPI: {some: 'api'}}));91 await browserPool.getBrowser({browserId: 'bro-id', browserVersion: '10.1'});92 assert.calledOnceWith(onNewBrowser, {some: 'api'}, {browserId: 'bro-id', browserVersion: '10.1'});93 });94 it('should not create a new browser if there is a free browser in a cache', async () => {95 const browserPool = createPool();96 Browser.create.returns(stubBrowser());97 const browser = await browserPool.getBrowser({browserId: 'bro-id', sessionId: '100-500'});98 browserPool.freeBrowser(browser);99 Browser.create.resetHistory();100 const anotherBrowser = await browserPool.getBrowser({browserId: 'bro-id', sessionsId: '500-100'});101 assert.deepEqual(browser, anotherBrowser);102 assert.notCalled(Browser.create);103 });104 it('should not create a new browser if there is a free browser in a cache with same version', async () => {105 const browserPool = createPool();106 Browser.create.returns(stubBrowser({version: '1.1'}));107 const browser = await browserPool.getBrowser({108 browserId: 'bro-id', browserVersion: '1.1', sessionId: '100-500'109 });110 browserPool.freeBrowser(browser);111 Browser.create.resetHistory();112 const anotherBrowser = await browserPool.getBrowser({113 browserId: 'bro-id', browserVersion: '1.1', sessionId: '500-100'114 });115 assert.deepEqual(browser, anotherBrowser);116 assert.notCalled(Browser.create);117 });118 it('should reinit a given session to a free browser in a cache', async () => {119 const browserPool = createPool();120 Browser.create.returns(stubBrowser());121 const browser = await browserPool.getBrowser({122 browserId: 'bro-id',123 sessionId: '100-500',124 sessionOpts: {foo: 'bar'}125 });126 browserPool.freeBrowser(browser);127 const anotherBrowser = await browserPool.getBrowser({128 browserId: 'bro-id',129 sessionId: '500-100',130 sessionOpts: {bar: 'foo'}131 });132 assert.calledOnceWith(anotherBrowser.reinit, '500-100', {bar: 'foo'});133 });134 it('should not emit "NEW_BROWSER" event on getting of a free browser from a cache', async () => {135 const emitter = new EventEmitter();136 const onNewBrowser = sandbox.spy().named('onNewBrowser');137 const browserPool = createPool({emitter});138 Browser.create.returns(stubBrowser());139 emitter.on(RunnerEvents.NEW_BROWSER, onNewBrowser);140 const browser = await browserPool.getBrowser({browserId: 'bro-id'});141 browserPool.freeBrowser(browser);142 onNewBrowser.reset();143 await browserPool.getBrowser({browserId: 'bro-id'});144 assert.notCalled(onNewBrowser);145 });146 describe('getting of browser fails', () => {147 beforeEach(() => {148 sandbox.spy(BrowserPool.prototype, 'freeBrowser');149 });150 it('should be rejected if instance of browser was not created', () => {151 Browser.create.throws(new Error('foo bar'));152 return assert.isRejected(createPool().getBrowser({}), /foo bar/);153 });154 describe('init fails', () => {155 const stubBrowserWhichRejectsOnInit = (params = {}) => {156 const browser = stubBrowser(params);157 Browser.create.returns(browser);158 browser.init.rejects();159 return browser;160 };161 it('should mark browser as broken', async () => {162 const browser = stubBrowserWhichRejectsOnInit({id: 'bro-id'});163 await createPool().getBrowser({browserId: 'bro-id'}).catch((e) => e);164 assert.calledOnceWith(browser.markAsBroken);165 });166 it('should extend browser with session id', async () => {167 const browser = stubBrowserWhichRejectsOnInit({id: 'bro-id'});168 await createPool().getBrowser({browserId: 'bro-id', sessionId: '100500'}).catch((e) => e);169 assert.equal(browser.sessionId, '100500');170 });171 it('should extend browser with session id and browser version', async () => {172 const browser = stubBrowserWhichRejectsOnInit({id: 'bro-id', version: '10.1'});173 await createPool()174 .getBrowser({browserId: 'bro-id', browserVersion: '10.1', sessionId: '100500'})175 .catch((e) => e);176 assert.equal(browser.version, '10.1');177 });178 it('should free browser', async () => {179 const browser = stubBrowserWhichRejectsOnInit({id: 'bro-id'});180 await createPool().getBrowser({browserId: 'bro-id'}).catch((e) => e);181 assert.calledOnceWith(BrowserPool.prototype.freeBrowser, browser);182 });183 it('should free browser after marking browser as broken', async () => {184 const browser = stubBrowserWhichRejectsOnInit({id: 'bro-id'});185 await createPool().getBrowser({browserId: 'bro-id'}).catch((e) => e);186 assert.callOrder(browser.markAsBroken, BrowserPool.prototype.freeBrowser);187 });188 it('should be rejected with error extended by browser meta', async () => {189 stubBrowserWhichRejectsOnInit({id: 'bro-id', meta: {foo: 'bar'}});190 const error = await createPool().getBrowser({browserId: 'bro-id'}).catch((e) => e);191 assert.deepEqual(error.meta, {foo: 'bar'});192 });193 });194 describe('reinit fails', () => {195 const stubBrowserWhichRejectsOnReinit = (params = {}) => {196 const browser = stubBrowser(params);197 Browser.create.returns(browser);198 browser.reinit.rejects();199 return browser;200 };201 it('should mark browser as broken', async () => {202 const browser = stubBrowserWhichRejectsOnReinit({id: 'bro-id'});203 const browserPool = createPool();204 await browserPool.getBrowser({browserId: 'bro-id'});205 await browserPool.freeBrowser(browser);206 await browserPool.getBrowser({browserId: 'bro-id'}).catch((e) => e);207 assert.calledOnce(browser.markAsBroken);208 });209 it('should extend browser with session id', async () => {210 const browser = stubBrowserWhichRejectsOnReinit({id: 'bro-id'});211 const browserPool = createPool();212 await browserPool.getBrowser({browserId: 'bro-id'});213 await browserPool.freeBrowser(browser);214 await browserPool.getBrowser({browserId: 'bro-id', sessionId: '100500'}).catch((e) => e);215 assert.equal(browser.sessionId, '100500');216 });217 it('should free browser', async () => {218 const browser = stubBrowserWhichRejectsOnReinit({id: 'bro-id'});219 const browserPool = createPool();220 await browserPool.getBrowser({browserId: 'bro-id'});221 await browserPool.freeBrowser(browser);222 browserPool.freeBrowser.resetHistory();223 await browserPool.getBrowser({browserId: 'bro-id'}).catch((e) => e);224 assert.calledOnceWith(browserPool.freeBrowser, browser);225 });226 it('should free browser after marking browser as broken', async () => {227 const browser = stubBrowserWhichRejectsOnReinit({id: 'bro-id'});228 const browserPool = createPool();229 await browserPool.getBrowser({browserId: 'bro-id'});230 await browserPool.freeBrowser(browser);231 browserPool.freeBrowser.resetHistory();232 await browserPool.getBrowser({browserId: 'bro-id'}).catch((e) => e);233 assert.callOrder(browser.markAsBroken, browserPool.freeBrowser);234 });235 it('should be rejected with error extended by browser meta', async () => {236 const browser = stubBrowserWhichRejectsOnReinit({id: 'bro-id', meta: {foo: 'bar'}});237 const browserPool = createPool();238 await browserPool.getBrowser({browserId: 'bro-id'});239 await browserPool.freeBrowser(browser);240 const error = await browserPool.getBrowser({browserId: 'bro-id'}).catch((e) => e);241 assert.deepEqual(error.meta, {foo: 'bar'});242 });243 });244 });245 });246 describe('freeBrowser', () => {247 it('should create a new browser if there is a broken browser in a cache', async () => {248 const browserPool = createPool();249 Browser.create.returns(stubBrowser({id: 'bro-id', state: {isBroken: true}}));250 const browser = await browserPool.getBrowser({browserId: 'bro-id'});251 browserPool.freeBrowser(browser);252 Browser.create.resetHistory();253 await browserPool.getBrowser({browserId: 'bro-id'});254 assert.calledOnce(Browser.create);255 });256 it('should set session id to "null"', async () => {257 const browserPool = createPool();258 Browser.create.returns(stubBrowser());259 const browser = await browserPool.getBrowser({browserId: 'bro-id'});260 browserPool.freeBrowser(browser);261 assert.calledOnce(browser.quit);262 });263 it('should send test related freeBrowser event on browser release', async () => {264 createPool().freeBrowser(stubBrowser({sessionId: '100500', state: {foo: 'bar'}}));265 assert.calledOnceWith(ipc.emit, 'worker.100500.freeBrowser', {foo: 'bar'});266 });267 });...
limited-pool.js
Source:limited-pool.js
...11 {limit, isSpecificBrowserLimiter, logNamespace: 'hermione'}12 );13 beforeEach(() => {14 underlyingPool = {15 getBrowser: sinon.stub().callsFake((id) => Promise.resolve(stubBrowser(id))),16 freeBrowser: sinon.stub().returns(Promise.resolve()),17 cancel: sinon.stub()18 };19 });20 afterEach(() => sandbox.restore());21 describe('getBrowser', () => {22 it('should request browser from underlying pool', async () => {23 const browser = stubBrowser('bro');24 underlyingPool.getBrowser.returns(Promise.resolve(browser));25 const bro = await makePool_().getBrowser('bro');26 assert.equal(bro, browser);27 });28 it('should pass opts to underlying pool', async () => {29 const browser = stubBrowser('bro');30 underlyingPool.getBrowser.returns(Promise.resolve(browser));31 await makePool_().getBrowser('bro', {some: 'opt'});32 assert.calledOnceWith(underlyingPool.getBrowser, 'bro', {some: 'opt'});33 });34 });35 describe('freeBrowser', () => {36 it('should correctly pass params to an underlying pool to be able to force free', async () => {37 const pool = makePool_({limit: 1});38 const browser = stubBrowser('bro');39 underlyingPool.getBrowser.returns(Promise.resolve(browser));40 pool.getBrowser('bro');41 pool.getBrowser('bro', {version: '10.1'});42 await pool.freeBrowser(browser);43 assert.calledOnceWith(underlyingPool.freeBrowser, browser, sinon.match({44 compositeIdForNextRequest: 'bro.10.1',45 hasFreeSlots: false46 }));47 });48 it('should handle case if there is no next item in queue', async () => {49 const pool = makePool_({limit: 1});50 const browser = stubBrowser('bro');51 underlyingPool.getBrowser.returns(Promise.resolve(browser));52 pool.getBrowser('bro');53 await pool.freeBrowser(browser);54 assert.calledOnceWith(underlyingPool.freeBrowser, browser, sinon.match({55 compositeIdForNextRequest: undefined56 }));57 });58 });59 describe('should return browser to underlying pool', () => {60 let browser;61 let pool;62 beforeEach(() => {63 browser = stubBrowser();64 pool = makePool_();65 underlyingPool.getBrowser.returns(Promise.resolve(browser));66 });67 it('when freed', () => {68 return pool.freeBrowser(browser)69 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser));70 });71 it('for release with force if there are no more requests in specific browser limiter', () => {72 pool = makePool_({isSpecificBrowserLimiter: true});73 return pool.getBrowser('first')74 .then(() => pool.freeBrowser(browser))75 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser, sinon.match({force: true})));76 });77 it('for release without force if there are no more requests in all browser limiter', () => {78 pool = makePool_({isSpecificBrowserLimiter: false});79 return pool.getBrowser('first')80 .then(() => pool.freeBrowser(browser))81 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser, sinon.match({force: false})));82 });83 it('for caching if there is at least one pending request', () => {84 return pool.getBrowser('first')85 .then(() => {86 pool.getBrowser('second');87 return pool.freeBrowser(browser);88 })89 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser, sinon.match({force: false})));90 });91 it('for release if there are pending requests but forced to free', () => {92 return pool.getBrowser('first')93 .then(() => {94 pool.getBrowser('second');95 return pool.freeBrowser(browser, {force: true});96 })97 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser, sinon.match({force: true})));98 });99 it('for caching if there are pending requests', () => {100 return pool.getBrowser('first')101 .then(() => {102 pool.getBrowser('second');103 pool.getBrowser('third');104 return pool.freeBrowser(browser);105 })106 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser, sinon.match({force: false})));107 });108 it('taking into account number of failed browser requests', () => {109 const browser = stubBrowser();110 const pool = makePool_({limit: 2});111 underlyingPool.getBrowser112 .withArgs('first').returns(Promise.resolve(browser))113 .withArgs('second').returns(Promise.reject());114 return Promise115 .all([116 pool.getBrowser('first'),117 pool.getBrowser('second').reflect()118 ])119 .then(() => pool.freeBrowser(browser))120 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser, sinon.match({force: true})));121 });122 });123 it('should launch next request from queue on fail to receive browser from underlying pool', () => {124 const browser = stubBrowser();125 const pool = makePool_();126 underlyingPool.getBrowser.onFirstCall().returns(Promise.reject());127 underlyingPool.getBrowser.onSecondCall().returns(Promise.resolve(browser));128 pool.getBrowser('bro').catch(() => {});129 assert.eventually.equal(pool.getBrowser('bro'), browser);130 });131 describe('limit', () => {132 it('should launch all browser in limit', () => {133 underlyingPool.getBrowser134 .withArgs('first').returns(Promise.resolve(stubBrowser()))135 .withArgs('second').returns(Promise.resolve(stubBrowser()));136 const pool = makePool_({limit: 2});137 return Promise.all([pool.getBrowser('first'), pool.getBrowser('second')])138 .then(() => {139 assert.calledTwice(underlyingPool.getBrowser);140 assert.calledWith(underlyingPool.getBrowser, 'first');141 assert.calledWith(underlyingPool.getBrowser, 'second');142 });143 });144 it('should not launch browsers out of limit', () => {145 underlyingPool.getBrowser.returns(Promise.resolve(stubBrowser()));146 const pool = makePool_({limit: 1});147 const result = pool.getBrowser('first')148 .then(() => pool.getBrowser('second').timeout(100, 'timeout'));149 return assert.isRejected(result, /timeout$/);150 });151 it('should launch next browser after previous is released', () => {152 const expectedBrowser = stubBrowser();153 const pool = makePool_({limit: 1});154 underlyingPool.getBrowser155 .withArgs('first').returns(Promise.resolve(stubBrowser()))156 .withArgs('second').returns(Promise.resolve(expectedBrowser));157 const result = pool.getBrowser('first')158 .then((browser) => pool.freeBrowser(browser))159 .then(() => pool.getBrowser('second'));160 return assert.eventually.equal(result, expectedBrowser);161 });162 it('should launch queued browser when previous is released', () => {163 const expectedBrowser = stubBrowser();164 const pool = makePool_({limit: 1});165 underlyingPool.getBrowser166 .withArgs('first').returns(Promise.resolve(stubBrowser()))167 .withArgs('second').returns(Promise.resolve(expectedBrowser));168 const result = pool.getBrowser('first')169 .then((browser) => {170 const secondPromise = pool.getBrowser('second');171 return Promise.delay(100)172 .then(() => pool.freeBrowser(browser))173 .then(() => secondPromise);174 });175 return assert.eventually.equal(result, expectedBrowser);176 });177 it('should perform high priority request first', async () => {178 const firstBrowserRequest = underlyingPool.getBrowser.withArgs('first').named('firstRequest');179 const secondBrowserRequest = underlyingPool.getBrowser.withArgs('second').named('secondRequest');180 const thirdBrowserRequest = underlyingPool.getBrowser.withArgs('third').named('thirdRequest');181 const pool = makePool_({limit: 1});182 const free_ = (bro) => pool.freeBrowser(bro);183 await Promise.all([184 pool.getBrowser('first').then(free_),185 pool.getBrowser('second').then(free_),186 pool.getBrowser('third', {highPriority: true}).then(free_)187 ]);188 assert.callOrder(189 firstBrowserRequest,190 thirdBrowserRequest,191 secondBrowserRequest192 );193 });194 it('should launch next browsers if free failed', () => {195 const expectedBrowser = stubBrowser();196 const pool = makePool_({limit: 1});197 underlyingPool.getBrowser198 .withArgs('first').returns(Promise.resolve(stubBrowser()))199 .withArgs('second').returns(Promise.resolve(expectedBrowser));200 underlyingPool.freeBrowser.callsFake(() => Promise.reject());201 return pool.getBrowser('first')202 .then((browser) => {203 const secondPromise = pool.getBrowser('second');204 return Promise.delay(100)205 .then(() => pool.freeBrowser(browser))206 .catch(() => secondPromise);207 })208 .then((browser) => assert.equal(browser, expectedBrowser));209 });210 it('should not wait for queued browser to start after release browser', () => {211 const pool = makePool_({limit: 1});212 const afterFree = sinon.spy().named('afterFree');213 const afterSecondGet = sinon.spy().named('afterSecondGet');214 underlyingPool.getBrowser215 .withArgs('first').returns(Promise.resolve(stubBrowser()))216 .withArgs('second').returns(Promise.resolve());217 return pool.getBrowser('first')218 .then((browser) => {219 const freeFirstBrowser = Promise.delay(100)220 .then(() => pool.freeBrowser(browser))221 .then(afterFree);222 const getSecondBrowser = pool.getBrowser('second')223 .then(afterSecondGet);224 return Promise.all([getSecondBrowser, freeFirstBrowser])225 .then(() => assert.callOrder(afterFree, afterSecondGet));226 });227 });228 it('should reject the queued call when underlying pool rejects the request', () => {229 const pool = makePool_({limit: 1});...
caching-pool.js
Source:caching-pool.js
...19 };20 const makePool_ = () => poolWithReuseLimits_({bro: Infinity});21 beforeEach(() => {22 underlyingPool = {23 getBrowser: sinon.stub().callsFake((id, opts) => Promise.resolve(stubBrowser(id, opts.version))),24 freeBrowser: sinon.stub().returns(Promise.resolve()),25 finalizeBrowsers: sinon.stub().returns(Promise.resolve()),26 cancel: sinon.stub()27 };28 });29 afterEach(() => sandbox.restore());30 describe('based on browser id only', () => {31 createCommonTestCases();32 });33 describe('based on both browser id and version', () => {34 createCommonTestCases({version: 1.0});35 });36 function createCommonTestCases(opts = {}) {37 it('should create new browser when requested first time', () => {38 const pool = makePool_();39 return pool.getBrowser('bro', opts)40 .then(() => {41 assert.calledOnceWith(underlyingPool.getBrowser, 'bro', opts);42 });43 });44 it('should return same browser as returned by underlying pool', () => {45 const browser = stubBrowser('bro');46 underlyingPool.getBrowser.withArgs('bro', opts).returns(Promise.resolve(browser));47 return makePool_()48 .getBrowser('bro', opts)49 .then((bro) => assert.equal(bro, browser));50 });51 it('should not reset new browser', () => {52 const browser = stubBrowser();53 underlyingPool.getBrowser.withArgs('bro', opts).returns(Promise.resolve(browser));54 return makePool_()55 .getBrowser('bro', opts)56 .then(() => assert.notCalled(browser.reset));57 });58 it('should create and launch new browser if there is free browser with different id', () => {59 underlyingPool.getBrowser60 .withArgs('first').returns(Promise.resolve(stubBrowser('first', opts.version)))61 .withArgs('second').returns(Promise.resolve(stubBrowser('second', opts.version)));62 const pool = poolWithReuseLimits_({63 first: 1,64 second: 165 });66 return pool.getBrowser('first', opts)67 .then((browser) => pool.freeBrowser(browser))68 .then(() => pool.getBrowser('second', opts))69 .then(() => assert.calledWith(underlyingPool.getBrowser, 'second', opts));70 });71 it('should not quit browser when freed', () => {72 underlyingPool.getBrowser.withArgs('bro', opts)73 .returns(Promise.resolve(stubBrowser('bro', opts.version)));74 const pool = makePool_();75 return pool.getBrowser('bro', opts)76 .then((browser) => pool.freeBrowser(browser, {force: false}))77 .then(() => assert.notCalled(underlyingPool.freeBrowser));78 });79 it('should quit browser when there are no more requests', () => {80 underlyingPool.getBrowser.withArgs('bro', opts)81 .returns(Promise.resolve(stubBrowser('bro', opts.version)));82 const pool = makePool_();83 return pool.getBrowser('bro', opts)84 .then((browser) => pool.freeBrowser(browser, {force: true}))85 .then(() => assert.calledOnce(underlyingPool.freeBrowser));86 });87 describe('when there is free browser with the same id', () => {88 let browser, pool;89 beforeEach(() => {90 browser = stubBrowser('bro', opts.version);91 pool = makePool_();92 pool.getBrowser('bro', opts);93 underlyingPool.getBrowser.reset();94 return pool.freeBrowser(browser);95 });96 it('should not create second instance', () => {97 return pool.getBrowser('bro', opts)98 .then(() => assert.notCalled(underlyingPool.getBrowser));99 });100 it('should reset the browser', () => {101 return pool.getBrowser('bro', opts)102 .then(() => assert.calledOnce(browser.reset));103 });104 describe('when reset failed', () => {105 it('should fail to get browser', () => {106 browser.reset.returns(Promise.reject('some-error'));107 return assert.isRejected(pool.getBrowser('bro', opts), /some-error/);108 });109 it('should put browser back', () => {110 browser.reset.returns(Promise.reject());111 return pool.getBrowser('bro', opts)112 .catch(() => {113 assert.calledOnce(underlyingPool.freeBrowser);114 assert.calledWith(underlyingPool.freeBrowser, browser);115 });116 });117 it('should keep original error if failed to put browser back', () => {118 browser.reset.returns(Promise.reject('reset-error'));119 underlyingPool.freeBrowser.returns(Promise.reject('free-error'));120 return assert.isRejected(pool.getBrowser('bro', opts), /reset-error/);121 });122 });123 });124 describe('when there are multiple browsers with same id', () => {125 let firstBrowser, secondBrowser, pool;126 beforeEach(() => {127 firstBrowser = stubBrowser('bro', opts.version);128 secondBrowser = stubBrowser('bro', opts.version);129 pool = makePool_();130 pool.getBrowser('bro', opts);131 return Promise.all([132 pool.freeBrowser(firstBrowser),133 pool.freeBrowser(secondBrowser)134 ]);135 });136 it('should return last browser in cache on first getBrowser', () => {137 return assert.becomes(pool.getBrowser('bro', opts), secondBrowser);138 });139 it('should return first browser on second getBrowser', () => {140 return pool.getBrowser('bro', opts)141 .then(() => assert.becomes(pool.getBrowser('bro', opts), firstBrowser));142 });143 it('should launch new session when there are no free browsers left', () => {144 return pool.getBrowser('bro', opts)145 .then(() => pool.getBrowser('bro', opts))146 .then(() => pool.getBrowser('bro', opts))147 .then(() => assert.calledWith(underlyingPool.getBrowser, 'bro', opts));148 });149 });150 describe('when there is reuse limit', () => {151 const launchAndFree_ = (pool, id) => {152 return pool.getBrowser(id, opts)153 .then((browser) => pool.freeBrowser(browser));154 };155 it('should launch only one session within the reuse limit', () => {156 underlyingPool.getBrowser.returns(Promise.resolve(stubBrowser('bro', opts.version)));157 const pool = poolWithReuseLimits_({bro: 2});158 return launchAndFree_(pool, 'bro')159 .then(() => pool.getBrowser('bro', opts))160 .then(() => assert.calledOnce(underlyingPool.getBrowser));161 });162 it('should launch next session when over reuse limit', () => {163 underlyingPool.getBrowser164 .onFirstCall().returns(Promise.resolve(stubBrowser('bro', opts.version)))165 .onSecondCall().returns(Promise.resolve(stubBrowser('bro', opts.version)));166 const pool = poolWithReuseLimits_({bro: 2});167 return launchAndFree_(pool, 'bro')168 .then(() => launchAndFree_(pool, 'bro'))169 .then(() => pool.getBrowser('bro', opts))170 .then(() => assert.calledTwice(underlyingPool.getBrowser));171 });172 it('should get new session for each suite if reuse limit equal 1', () => {173 underlyingPool.getBrowser174 .onFirstCall().returns(Promise.resolve(stubBrowser('browserId', opts.version)))175 .onSecondCall().returns(Promise.resolve(stubBrowser('browserId', opts.version)));176 const pool = poolWithReuseLimits_({browserId: 1});177 return launchAndFree_(pool, 'browserId')178 .then(() => pool.getBrowser('browserId', opts))179 .then(() => assert.calledTwice(underlyingPool.getBrowser));180 });181 it('should close old session when reached reuse limit', () => {182 const browser = stubBrowser('bro', opts.version);183 underlyingPool.getBrowser.returns(Promise.resolve(browser));184 const pool = poolWithReuseLimits_({bro: 2});185 return launchAndFree_(pool, 'bro')186 .then(() => launchAndFree_(pool, 'bro'))187 .then(() => assert.calledWith(underlyingPool.freeBrowser, browser));188 });189 it('should cache browser with different id even if the first one is over limit', () => {190 underlyingPool.getBrowser191 .withArgs('first').returns(Promise.resolve(stubBrowser('first', opts.version)));192 const createSecondBrowser = underlyingPool.getBrowser.withArgs('second', opts);193 createSecondBrowser.returns(Promise.resolve(stubBrowser('second', opts.version)));194 const pool = poolWithReuseLimits_({195 first: 2,196 second: 2197 });198 return launchAndFree_(pool, 'first')199 .then(() => launchAndFree_(pool, 'second'))200 .then(() => launchAndFree_(pool, 'first'))201 .then(() => pool.getBrowser('second', opts))202 .then(() => assert.calledOnce(createSecondBrowser));203 });204 });205 }206 describe('freeBrowser', () => {207 it('should free if next test requires specific browser version and limit has reached', () => {208 const browser = stubBrowser('bro', '10.1');209 const pool = makePool_();210 const freeOpts = {211 compositeIdForNextRequest: buildCompositeBrowserId('bro', '10.1'),212 hasFreeSlots: false213 };214 return pool.freeBrowser(browser, freeOpts)215 .then(() => assert.calledOnce(underlyingPool.freeBrowser));216 });217 it('should NOT free if next test requires specific browser version and limit has NOT reached', () => {218 const browser = stubBrowser('bro', '10.1');219 const pool = makePool_();220 const freeOpts = {221 compositeIdForNextRequest: buildCompositeBrowserId('bro', '10.1'),222 hasFreeSlots: true223 };224 return pool.freeBrowser(browser, freeOpts)225 .then(() => assert.notCalled(underlyingPool.freeBrowser));226 });227 it('should NOT free if next test requires specific browser version and there is cache', () => {228 const pool = makePool_();229 const freeOpts = {230 compositeIdForNextRequest: buildCompositeBrowserId('bro', '10.1'),231 hasFreeSlots: false232 };...
basic-pool.js
Source:basic-pool.js
...7describe('browser-pool/basic-pool', () => {8 const sandbox = sinon.sandbox.create();9 const stubBrowserManager_ = () => {10 return {11 create: sinon.stub().callsFake((id) => stubBrowser(id)),12 start: sinon.stub().returns(Promise.resolve()),13 onStart: sinon.stub().returns(Promise.resolve()),14 onQuit: sinon.stub().returns(Promise.resolve()),15 quit: sinon.stub().returns(Promise.resolve())16 };17 };18 const mkPool_ = (opts) => {19 opts = _.defaults(opts, {20 browserManager: stubBrowserManager_()21 });22 return BasicPool.create(opts.browserManager, {});23 };24 afterEach(() => sandbox.restore());25 it('should create new browser when requested', () => {26 const browserManager = stubBrowserManager_();27 const pool = mkPool_({browserManager});28 return pool.getBrowser('broId')29 .then(() => assert.calledWith(browserManager.create, 'broId'));30 });31 it('should create new browser with specified version when requested', () => {32 const browserManager = stubBrowserManager_();33 const pool = mkPool_({browserManager});34 return pool.getBrowser('broId', {version: '1.0'})35 .then(() => assert.calledWith(browserManager.create, 'broId', '1.0'));36 });37 it('should launch a browser', () => {38 const browser = stubBrowser();39 const browserManager = stubBrowserManager_();40 browserManager.create.returns(browser);41 const pool = mkPool_({browserManager});42 return pool.getBrowser()43 .then(() => {44 assert.calledOnceWith(browserManager.start);45 assert.calledWith(browserManager.start, browser);46 });47 });48 it('should not finalize browser if failed to start it', async () => {49 const publicAPI = null;50 const browser = stubBrowser('some-id', 'some-version', publicAPI);51 const browserManager = stubBrowserManager_();52 browserManager.create.returns(browser);53 browserManager.start.rejects();54 const pool = mkPool_({browserManager});55 await assert.isRejected(pool.getBrowser());56 assert.notCalled(browserManager.quit);57 });58 it('should finalize browser if failed after start it', async () => {59 const publicAPI = {};60 const browser = stubBrowser('some-id', 'some-version', publicAPI);61 const browserManager = stubBrowserManager_();62 browserManager.create.returns(browser);63 browserManager.onStart.rejects();64 const pool = mkPool_({browserManager});65 await assert.isRejected(pool.getBrowser());66 assert.calledOnceWith(browserManager.quit, browser);67 });68 describe('onStart', () => {69 it('should be called after browser start', () => {70 const browser = stubBrowser();71 const browserManager = stubBrowserManager_();72 browserManager.create.returns(browser);73 const pool = mkPool_({browserManager});74 return pool.getBrowser()75 .then(() => {76 assert.calledOnce(browserManager.onStart);77 assert.calledWith(browserManager.onStart, browser);78 assert.callOrder(browserManager.start, browserManager.onStart);79 });80 });81 it('handler should be waited by pool', () => {82 const browser = stubBrowser();83 const afterSessionStart = sinon.spy();84 const browserManager = stubBrowserManager_();85 browserManager.create.returns(browser);86 browserManager.onStart.callsFake(() => Promise.delay(10).then(afterSessionStart));87 const pool = mkPool_({browserManager});88 return pool.getBrowser()89 .then(() => assert.callOrder(afterSessionStart, browser.reset));90 });91 it('handler fail should fail browser request', () => {92 const browserManager = stubBrowserManager_();93 browserManager.onStart.returns(Promise.reject('some-error'));94 const pool = mkPool_({browserManager});95 return assert.isRejected(pool.getBrowser(), 'some-error');96 });97 it('on handler fail browser should be finalized', () => {98 const browser = stubBrowser();99 const browserManager = stubBrowserManager_();100 browserManager.create.returns(browser);101 browserManager.onStart.returns(Promise.reject());102 const pool = mkPool_({browserManager});103 return assert.isRejected(pool.getBrowser())104 .then(() => {105 assert.calledOnce(browserManager.quit);106 assert.calledWith(browserManager.quit, browser);107 });108 });109 });110 it('should quit a browser when freed', () => {111 const browser = stubBrowser();112 const browserManager = stubBrowserManager_();113 browserManager.create.returns(browser);114 const pool = mkPool_({browserManager});115 return pool.getBrowser()116 .then((browser) => pool.freeBrowser(browser))117 .then(() => {118 assert.calledOnce(browserManager.quit);119 assert.calledWith(browserManager.quit, browser);120 });121 });122 describe('onQuit', () => {123 it('should be emitted before browser quit', () => {124 const browser = stubBrowser();125 const browserManager = stubBrowserManager_();126 browserManager.create.returns(browser);127 const pool = mkPool_({browserManager});128 return pool.getBrowser()129 .then((browser) => pool.freeBrowser(browser))130 .then(() => {131 assert.calledOnce(browserManager.onQuit);132 assert.calledWith(browserManager.onQuit, browser);133 assert.callOrder(browserManager.onQuit, browserManager.quit);134 });135 });136 it('handler should be waited before actual quit', () => {137 const beforeSessionQuit = sinon.spy();138 const browserManager = stubBrowserManager_();...
index.js
Source:index.js
...41 assert.deepEqual(getBrowserManager().create('bro'), {some: 'browser'});42 });43 it('should start a browser', () => {44 BrowserPool.create();45 const browser = stubBrowser();46 browser.init.resolves({session: 'id'});47 assert.becomes(getBrowserManager().start(browser), {session: 'id'});48 });49 _.forEach({onStart: Events.SESSION_START, onQuit: Events.SESSION_END}, (event, method) => {50 describe(`${method}`, () => {51 it(`should emit browser event "${event}"`, () => {52 const emitter = new AsyncEmitter();53 const onEvent = sandbox.spy();54 BrowserPool.create(null, emitter);55 const BrowserManager = getBrowserManager();56 emitter.on(event, onEvent);57 const browser = stubBrowser({58 id: 'bro',59 sessionId: '100500',60 publicAPI: {public: 'api'}61 });62 return BrowserManager[method](browser)63 .then(() => assert.calledOnceWith(onEvent, {public: 'api'}, {browserId: 'bro', sessionId: '100500'}));64 });65 it('should wait all async listeners', () => {66 const emitter = new AsyncEmitter();67 const onEvent = sandbox.stub().callsFake(() => Promise.delay(1).then(() => ({foo: 'bar'})));68 BrowserPool.create(null, emitter);69 const BrowserManager = getBrowserManager();70 emitter.on(event, onEvent);71 return assert.becomes(BrowserManager[method](stubBrowser()), [{foo: 'bar'}]);72 });73 });74 });75 it('should quit a browser', () => {76 const browser = stubBrowser();77 browser.quit.resolves({foo: 'bar'});78 BrowserPool.create();79 assert.becomes(getBrowserManager().quit(browser), {foo: 'bar'});80 });81 });82 describe('adapter config', () => {83 const getAdapterConfig = () => CoreBrowserPool.create.lastCall.args[1].config;84 it('should return config for a browser', () => {85 const configStub = makeConfigStub({86 browsers: ['bro'],87 sessionsPerBrowser: 777,88 testsPerSession: 99989 });90 BrowserPool.create(configStub);...
browser-tabs.spec.js
Source:browser-tabs.spec.js
...17 const tabId = 4218 const appId = 4719 const searchterm = 'searchterm'20 const sendMessage = sinon.stub().returns('foobar')21 stubBrowser({sendMessage})22 sendToTab(suggestion, tabId, appId, searchterm)23 .fork(t.fail, ret => {24 t.equals(ret, 'foobar', 'result of sendMessage returned')25 t.ok(26 sendMessage.calledWith(tabId, {appId, suggestion, searchterm}),27 'proper data sent'28 )29 })30 } catch (e) {31 console.error(e.stack)32 t.fail(e)33 t.end()34 }35 }36)37test(38 'UNIT => sendToTab(suggestion, tabId, appId) => noop on no suggestion', t => {39 t.plan(2)40 try {41 const suggestion = null42 const tabId = 4243 const appId = 4744 const sendMessage = sinon.stub().returns('foobar')45 stubBrowser({sendMessage})46 sendToTab(suggestion, tabId, appId)47 .fork(t.fail, ret => {48 t.equals(ret, undefined, 'returned undefined')49 t.ok(sendMessage.notCalled, 'no message sent')50 })51 } catch (e) {52 console.error(e.stack)53 t.fail(e)54 t.end()55 }56 }57)58test('UNIT => populateActiveTab() => returns active tab id', t => {59 t.plan(1)60 try {61 const rawTabInfo = [{id: 0}, {id: 1}]62 const query = sinon.stub()63 .withArgs({active: true, currentWindow: true})64 .returns(Promise.resolve(rawTabInfo))65 stubBrowser({query})66 populateActiveTab()67 .fork(t.fail, ret => {68 t.deepEquals(ret, {tabId: 0}, 'proper return and format')69 })70 } catch (e) {71 console.error(e.stack)72 t.fail(e)73 t.end()74 }...
browser.test.js
Source:browser.test.js
...13 stub.restore()14 })15 it('should return true for browsers in whitelist', () => {16 SUPPORTED_BROWSERS_CODES.forEach((browser) => {17 stubBrowser(browser)18 expect(isBrowserSupported()).to.be.true19 stub.restore()20 })21 })22 it('should return false for non-whitelisted browsers', () => {23 stubBrowser('ie')24 expect(isBrowserSupported()).to.be.false25 })26 it('should return false for non-detected browsers', () => {27 stub = sinon28 .stub(DetectBrowser, 'detect')29 .callsFake(() => null)30 expect(isBrowserSupported()).to.be.false31 })...
puppeteerMock.js
Source:puppeteerMock.js
1export const stubPuppeteer = {2 launch() {3 return Promise.resolve(stubBrowser);4 },5};6export const stubBrowser = {7 newPage() {8 return Promise.resolve(stubPage);9 },10 close() {11 return Promise.resolve();12 },13};14export const stubPage = {15 goto(url) {16 return Promise.resolve();17 },18 $$(selector) {19 return Promise.resolve([]);20 },21 $(selector) {22 return Promise.resolve(stubElementHandle);23 },24 $eval(selector, pageFunction) {25 return Promise.resolve();26 },27};28export const stubElementHandle = {29 $eval() {30 return Promise.resolve();31 },...
Using AI Code Generation
1describe('My First Test', function() {2 it('Visits the Kitchen Sink', function() {3 cy.stubBrowser();4 })5})6describe('My First Test', function() {7 it('Visits the Kitchen Sink', function() {8 cy.stubBrowser();9 })10})11describe('My First Test', function() {12 it('Visits the Kitchen Sink', function() {13 cy.stubBrowser();14 })15})16describe('My First Test', function() {17 it('Visits the Kitchen Sink', function() {18 cy.stubBrowser();19 })20})21describe('My First Test', function() {22 it('Visits the Kitchen Sink', function() {23 cy.stubBrowser();24 })25})26describe('My First Test', function() {27 it('Visits the Kitchen Sink', function() {28 cy.stubBrowser();29 })30})31describe('My First Test', function() {32 it('Visits the Kitchen Sink', function() {33 cy.stubBrowser();34 })35})36describe('My First Test', function() {37 it('Visits the Kitchen Sink', function() {38 cy.stubBrowser();39 })40})41describe('My First Test', function() {42 it('Visits the Kitchen Sink', function() {43 cy.stubBrowser();44 })45})46describe('My First Test', function() {47 it('Visits the Kitchen Sink', function() {48 cy.stubBrowser();49 })50})
Using AI Code Generation
1describe("test", () => {2 it("test", () => {3 cy.stubBrowser();4 cy.get(".home-list > :nth-child(1) > a").click();5 cy.get(".action-email").type("email");6 cy.get(".action-form").sub
Using AI Code Generation
1import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'2stubBrowser()3import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'4stubBrowser()5import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'6stubBrowser()7import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'8stubBrowser()9import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'10stubBrowser()11import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'12stubBrowser()13import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'14stubBrowser()15import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'16stubBrowser()17import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'18stubBrowser()19import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'20stubBrowser()21import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'22stubBrowser()23import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'24stubBrowser()25import { stubBrowser } from 'cypress-browser-extension-plugin/stub-browser'26stubBrowser()27import { stubBrowser } from 'cypress-browser-extension-plugin/stub
Using AI Code Generation
1cy.stubBrowser();2cy.stubBrowser({3});4cy.stubBrowser({5});6cy.stubBrowser({7});8cy.stubBrowser({9});10cy.stubBrowser({11});12cy.stubBrowser({13});14cy.stubBrowser({15});16cy.stubBrowser({
Using AI Code Generation
1import { stubBrowser } from 'cypress-browser-stub';2describe('Test', () => {3 it('should stub browser', () => {4 stubBrowser('Chrome', '85.0.4183.83');5 });6});7describe('Test', () => {8 it('should access browser', () => {9 cy.log(cypress.browser.name);10 cy.log(cypress.browser.version);11 });12});13import { unstubBrowser } from 'cypress-browser-stub';14describe('Test', () => {15 it('should unstub browser', () => {16 unstubBrowser();17 });18});19describe('Test', () => {20 it('should stub browser', () => {21 cy.stubBrowser('Chrome', '85.0.4183.83');22 });23});24describe('Test', () => {25 it('should access browser', () => {26 cy.log(cy.browser().name);27 cy.log(cy.browser().version);28 });29});30describe('Test', () => {31 it('should unstub browser', () => {32 cy.unstubBrowser();33 });34});35[MIT](
Using AI Code Generation
1describe('stubbing the browser', () => {2 it('stubs the browser', () => {3 cy.stubBrowser()4 })5})6describe('stubbing the browser', () => {7 it('stubs the browser', () => {8 cy.stubBrowser()9 })10})11Cypress.Commands.add('stubBrowser', () => {12 cy.window().then((win) => {13 cy.stub(win, 'open').returns({14 focus: () => {},15 close: () => {},16 })17 })18})19Cypress.Commands.add('stubBrowser', () => {20 cy.window().then((win) => {21 cy.stub(win, 'open').returns({22 focus: () => {},23 close: () => {},24 })25 })26})27Cypress.Commands.add('stubBrowser', () => {28 cy.window().then((win) => {29 cy.stub(win, 'open').returns({30 focus: () => {},31 close: () => {},32 })33 })34})35Cypress.Commands.add('stubBrowser', () => {36 cy.window().then((win) => {37 cy.stub(win, 'open').returns({38 focus: () => {},39 close: () => {},40 })41 })42})43Cypress.Commands.add('stubBrowser', () => {44 cy.window().then((win) => {45 cy.stub(win, 'open').returns({46 focus: () => {},47 close: () => {},48 })49 })50})51Cypress.Commands.add('stubBrowser', () => {52 cy.window().then((win) => {53 cy.stub(win, 'open').returns({54 focus: () => {},55 close: () => {},56 })57 })58})59Cypress.Commands.add('stubBrowser', () => {60 cy.window().then((win) => {61 cy.stub(win, 'open').returns({62 focus: () => {},63 close: () => {},64 })65 })66})67Cypress.Commands.add('stubBrowser', () => {68 cy.window().then((win) => {69 cy.stub(win, 'open').returns({70 focus: () => {},71 close: () => {},72 })73 })74})75Cypress.Commands.add('stubBrowser', () => {76 cy.window().then((win) => {77 cy.stub(win, 'open').returns({78 focus: ()
Using AI Code Generation
1import { stubBrowser } from "cypress-browser-geolocation";2describe("My First Test", () => {3 it("Visits the Kitchen Sink", () => {4 stubBrowser();5 cy.get(".tactile-searchbox-input").type("Mumbai");6 cy.get(".searchbox-searchbutton").click();7 });8});9{10}
Using AI Code Generation
1import { stubBrowser } from '../support/commands'2describe('My First Test', function() {3 it('Does not do much!', function() {4 stubBrowser()5 cy.contains('type').click()6 cy.url().should('include', '/commands/actions')7 cy.get('.action-email')8 .type('
Using AI Code Generation
1import { stubBrowser } from 'cypress-browser-extension-plugin/stubBrowser'2import { Geolocation } from 'webextension-polyfill-ts'3const coords = { latitude: 40.7128, longitude: -74.0060 }4stubBrowser(Geolocation, 'getCurrentPosition', (success) => {5 const position = { coords }6 success(position)7})8import { stubBrowser } from 'cypress-browser-extension-plugin/stubBrowser'9import { Geolocation } from 'webextension-polyfill-ts'10const coords = { latitude: 40.7128, longitude: -74.0060 }11const stub = stubBrowser(Geolocation, 'getCurrentPosition', (success) => {12 const position = { coords }13 success(position)14})15cy.wrap(stub).should('have.been.calledOnce')
Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.
You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.
Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.
Get 100 minutes of automation test minutes FREE!!