How to use baseDriverUnitTests method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

driver-tests.js

Source:driver-tests.js Github

copy

Full Screen

1import _ from 'lodash';2import chai from 'chai';3import chaiAsPromised from 'chai-as-promised';4import B from 'bluebird';5import { DeviceSettings } from '../..';6import sinon from 'sinon';7const should = chai.should();8chai.use(chaiAsPromised);9// wrap these tests in a function so we can export the tests and re-use them10// for actual driver implementations11function baseDriverUnitTests (DriverClass, defaultCaps = {}) {12 const w3cCaps = {13 alwaysMatch: Object.assign({}, defaultCaps, {14 platformName: 'Fake',15 deviceName: 'Commodore 64',16 }),17 firstMatch: [{}],18 };19 describe('BaseDriver', function () {20 let d;21 beforeEach(function () {22 d = new DriverClass();23 });24 afterEach(async function () {25 await d.deleteSession();26 });27 it('should return an empty status object', async function () {28 let status = await d.getStatus();29 status.should.eql({});30 });31 it('should return a sessionId from createSession', async function () {32 let [sessId] = await d.createSession(defaultCaps);33 should.exist(sessId);34 sessId.should.be.a('string');35 sessId.length.should.be.above(5);36 });37 it('should not be able to start two sessions without closing the first', async function () {38 await d.createSession(defaultCaps);39 await d.createSession(defaultCaps).should.eventually.be.rejectedWith('session');40 });41 it('should be able to delete a session', async function () {42 let sessionId1 = await d.createSession(defaultCaps);43 await d.deleteSession();44 should.equal(d.sessionId, null);45 let sessionId2 = await d.createSession(defaultCaps);46 sessionId1.should.not.eql(sessionId2);47 });48 it('should get the current session', async function () {49 let [, caps] = await d.createSession(defaultCaps);50 caps.should.equal(await d.getSession());51 });52 it('should return sessions if no session exists', async function () {53 let sessions = await d.getSessions();54 sessions.length.should.equal(0);55 });56 it('should return sessions', async function () {57 let caps = _.clone(defaultCaps);58 caps.a = 'cap';59 await d.createSession(caps);60 let sessions = await d.getSessions();61 sessions.length.should.equal(1);62 sessions[0].should.eql({63 id: d.sessionId,64 capabilities: caps65 });66 });67 it('should fulfill an unexpected driver quit promise', async function () {68 // make a command that will wait a bit so we can crash while it's running69 d.getStatus = async function () {70 await B.delay(1000);71 return 'good status';72 }.bind(d);73 let cmdPromise = d.executeCommand('getStatus');74 await B.delay(10);75 const p = new B((resolve, reject) => {76 setTimeout(() => reject(new Error('onUnexpectedShutdown event is expected to be fired within 5 seconds timeout')), 5000);77 d.onUnexpectedShutdown(resolve);78 });79 d.startUnexpectedShutdown(new Error('We crashed'));80 await cmdPromise.should.be.rejectedWith(/We crashed/);81 await p;82 });83 it('should not allow commands in middle of unexpected shutdown', async function () {84 // make a command that will wait a bit so we can crash while it's running85 d.oldDeleteSession = d.deleteSession;86 d.deleteSession = async function () {87 await B.delay(100);88 await this.oldDeleteSession();89 }.bind(d);90 let caps = _.clone(defaultCaps);91 await d.createSession(caps);92 const p = new B((resolve, reject) => {93 setTimeout(() => reject(new Error('onUnexpectedShutdown event is expected to be fired within 5 seconds timeout')), 5000);94 d.onUnexpectedShutdown(resolve);95 });96 d.startUnexpectedShutdown(new Error('We crashed'));97 await p;98 await d.executeCommand('getSession').should.be.rejectedWith(/shut down/);99 });100 it('should allow new commands after done shutting down', async function () {101 // make a command that will wait a bit so we can crash while it's running102 d.oldDeleteSession = d.deleteSession;103 d.deleteSession = async function () {104 await B.delay(100);105 await this.oldDeleteSession();106 }.bind(d);107 let caps = _.clone(defaultCaps);108 await d.createSession(caps);109 const p = new B((resolve, reject) => {110 setTimeout(() => reject(new Error('onUnexpectedShutdown event is expected to be fired within 5 seconds timeout')), 5000);111 d.onUnexpectedShutdown(resolve);112 });113 d.startUnexpectedShutdown(new Error('We crashed'));114 await p;115 await d.executeCommand('getSession').should.be.rejectedWith(/shut down/);116 await B.delay(500);117 await d.executeCommand('createSession', caps);118 await d.deleteSession();119 });120 it('should distinguish between W3C and JSONWP session', async function () {121 // Test JSONWP122 await d.executeCommand('createSession', Object.assign({}, defaultCaps, {123 platformName: 'Fake',124 deviceName: 'Commodore 64',125 }));126 d.protocol.should.equal('MJSONWP');127 await d.executeCommand('deleteSession');128 // Test W3C (leave first 2 args null because those are the JSONWP args)129 await d.executeCommand('createSession', null, null, {130 alwaysMatch: Object.assign({}, defaultCaps, {131 platformName: 'Fake',132 deviceName: 'Commodore 64',133 }),134 firstMatch: [{}],135 });136 d.protocol.should.equal('W3C');137 });138 describe('protocol detection', function () {139 it('should use MJSONWP if only JSONWP caps are provided', async function () {140 await d.createSession(defaultCaps);141 d.protocol.should.equal('MJSONWP');142 });143 it('should use W3C if only W3C caps are provided', async function () {144 await d.createSession(null, null, {alwaysMatch: defaultCaps, firstMatch: [{}]});145 d.protocol.should.equal('W3C');146 });147 });148 it('should have a method to get driver for a session', async function () {149 let [sessId] = await d.createSession(defaultCaps);150 d.driverForSession(sessId).should.eql(d);151 });152 describe('command queue', function () {153 let d = new DriverClass();154 let waitMs = 10;155 d.getStatus = async function () {156 await B.delay(waitMs);157 return Date.now();158 }.bind(d);159 d.getSessions = async function () {160 await B.delay(waitMs);161 throw new Error('multipass');162 }.bind(d);163 afterEach(function () {164 d.clearNewCommandTimeout();165 });166 it('should queue commands and.executeCommand/respond in the order received', async function () {167 let numCmds = 10;168 let cmds = [];169 for (let i = 0; i < numCmds; i++) {170 cmds.push(d.executeCommand('getStatus'));171 }172 let results = await B.all(cmds);173 for (let i = 1; i < numCmds; i++) {174 if (results[i] <= results[i - 1]) {175 throw new Error('Got result out of order');176 }177 }178 });179 it('should handle errors correctly when queuing', async function () {180 let numCmds = 10;181 let cmds = [];182 for (let i = 0; i < numCmds; i++) {183 if (i === 5) {184 cmds.push(d.executeCommand('getSessions'));185 } else {186 cmds.push(d.executeCommand('getStatus'));187 }188 }189 let results = await B.settle(cmds);190 for (let i = 1; i < 5; i++) {191 if (results[i].value() <= results[i - 1].value()) {192 throw new Error('Got result out of order');193 }194 }195 results[5].reason().message.should.contain('multipass');196 for (let i = 7; i < numCmds; i++) {197 if (results[i].value() <= results[i - 1].value()) {198 throw new Error('Got result out of order');199 }200 }201 });202 it('should not care if queue empties for a bit', async function () {203 let numCmds = 10;204 let cmds = [];205 for (let i = 0; i < numCmds; i++) {206 cmds.push(d.executeCommand('getStatus'));207 }208 let results = await B.all(cmds);209 cmds = [];210 for (let i = 0; i < numCmds; i++) {211 cmds.push(d.executeCommand('getStatus'));212 }213 results = await B.all(cmds);214 for (let i = 1; i < numCmds; i++) {215 if (results[i] <= results[i - 1]) {216 throw new Error('Got result out of order');217 }218 }219 });220 });221 describe('timeouts', function () {222 before(async function () {223 await d.createSession(defaultCaps);224 });225 describe('command', function () {226 it('should exist by default', function () {227 d.newCommandTimeoutMs.should.equal(60000);228 });229 it('should be settable through `timeouts`', async function () {230 await d.timeouts('command', 20);231 d.newCommandTimeoutMs.should.equal(20);232 });233 });234 describe('implicit', function () {235 it('should not exist by default', function () {236 d.implicitWaitMs.should.equal(0);237 });238 it('should be settable through `timeouts`', async function () {239 await d.timeouts('implicit', 20);240 d.implicitWaitMs.should.equal(20);241 });242 });243 });244 describe('timeouts (W3C)', function () {245 beforeEach(async function () {246 await d.createSession(null, null, w3cCaps);247 });248 afterEach(async function () {249 await d.deleteSession();250 });251 it('should get timeouts that we set', async function () {252 await d.timeouts(undefined, undefined, undefined, undefined, 1000);253 await d.getTimeouts().should.eventually.have.property('implicit', 1000);254 await d.timeouts('command', 2000);255 await d.getTimeouts().should.eventually.deep.equal({256 implicit: 1000,257 command: 2000,258 });259 await d.timeouts(undefined, undefined, undefined, undefined, 3000);260 await d.getTimeouts().should.eventually.deep.equal({261 implicit: 3000,262 command: 2000,263 });264 });265 });266 describe('reset compatibility', function () {267 it('should not allow both fullReset and noReset to be true', async function () {268 let newCaps = Object.assign({}, defaultCaps, {269 fullReset: true,270 noReset: true271 });272 await d.createSession(newCaps).should.eventually.be.rejectedWith(273 /noReset.+fullReset/);274 });275 });276 describe('proxying', function () {277 let sessId;278 beforeEach(async function () {279 [sessId] = await d.createSession(defaultCaps);280 });281 describe('#proxyActive', function () {282 it('should exist', function () {283 d.proxyActive.should.be.an.instanceof(Function);284 });285 it('should return false', function () {286 d.proxyActive(sessId).should.be.false;287 });288 it('should throw an error when sessionId is wrong', function () {289 (() => { d.proxyActive('aaa'); }).should.throw;290 });291 });292 describe('#getProxyAvoidList', function () {293 it('should exist', function () {294 d.getProxyAvoidList.should.be.an.instanceof(Function);295 });296 it('should return an array', function () {297 d.getProxyAvoidList(sessId).should.be.an.instanceof(Array);298 });299 it('should throw an error when sessionId is wrong', function () {300 (() => { d.getProxyAvoidList('aaa'); }).should.throw;301 });302 });303 describe('#canProxy', function () {304 it('should have a #canProxy method', function () {305 d.canProxy.should.be.an.instanceof(Function);306 });307 it('should return false from #canProxy', function () {308 d.canProxy(sessId).should.be.false;309 });310 it('should throw an error when sessionId is wrong', function () {311 (() => { d.canProxy(); }).should.throw;312 });313 });314 describe('#proxyRouteIsAvoided', function () {315 it('should validate form of avoidance list', function () {316 const avoidStub = sinon.stub(d, 'getProxyAvoidList');317 avoidStub.returns([['POST', /\/foo/], ['GET']]);318 (() => { d.proxyRouteIsAvoided(); }).should.throw;319 avoidStub.returns([['POST', /\/foo/], ['GET', /^foo/, 'bar']]);320 (() => { d.proxyRouteIsAvoided(); }).should.throw;321 avoidStub.restore();322 });323 it('should reject bad http methods', function () {324 const avoidStub = sinon.stub(d, 'getProxyAvoidList');325 avoidStub.returns([['POST', /^foo/], ['BAZETE', /^bar/]]);326 (() => { d.proxyRouteIsAvoided(); }).should.throw;327 avoidStub.restore();328 });329 it('should reject non-regex routes', function () {330 const avoidStub = sinon.stub(d, 'getProxyAvoidList');331 avoidStub.returns([['POST', /^foo/], ['GET', '/bar']]);332 (() => { d.proxyRouteIsAvoided(); }).should.throw;333 avoidStub.restore();334 });335 it('should return true for routes in the avoid list', function () {336 const avoidStub = sinon.stub(d, 'getProxyAvoidList');337 avoidStub.returns([['POST', /^\/foo/]]);338 d.proxyRouteIsAvoided(null, 'POST', '/foo/bar').should.be.true;339 avoidStub.restore();340 });341 it('should strip away any wd/hub prefix', function () {342 const avoidStub = sinon.stub(d, 'getProxyAvoidList');343 avoidStub.returns([['POST', /^\/foo/]]);344 d.proxyRouteIsAvoided(null, 'POST', '/wd/hub/foo/bar').should.be.true;345 avoidStub.restore();346 });347 it('should return false for routes not in the avoid list', function () {348 const avoidStub = sinon.stub(d, 'getProxyAvoidList');349 avoidStub.returns([['POST', /^\/foo/]]);350 d.proxyRouteIsAvoided(null, 'GET', '/foo/bar').should.be.false;351 d.proxyRouteIsAvoided(null, 'POST', '/boo').should.be.false;352 avoidStub.restore();353 });354 });355 });356 describe('event timing framework', function () {357 let beforeStartTime;358 beforeEach(async function () {359 beforeStartTime = Date.now();360 d.shouldValidateCaps = false;361 await d.executeCommand('createSession', defaultCaps);362 });363 describe('#eventHistory', function () {364 it('should have an eventHistory property', function () {365 should.exist(d.eventHistory);366 should.exist(d.eventHistory.commands);367 });368 it('should have a session start timing after session start', function () {369 let {newSessionRequested, newSessionStarted} = d.eventHistory;370 newSessionRequested.should.have.length(1);371 newSessionStarted.should.have.length(1);372 newSessionRequested[0].should.be.a('number');373 newSessionStarted[0].should.be.a('number');374 (newSessionRequested[0] >= beforeStartTime).should.be.true;375 (newSessionStarted[0] >= newSessionRequested[0]).should.be.true;376 });377 it('should include a commands list', async function () {378 await d.executeCommand('getStatus', []);379 d.eventHistory.commands.length.should.equal(2);380 d.eventHistory.commands[1].cmd.should.equal('getStatus');381 d.eventHistory.commands[1].startTime.should.be.a('number');382 d.eventHistory.commands[1].endTime.should.be.a('number');383 });384 });385 describe('#logEvent', function () {386 it('should allow logging arbitrary events', function () {387 d.logEvent('foo');388 d.eventHistory.foo[0].should.be.a('number');389 (d.eventHistory.foo[0] >= beforeStartTime).should.be.true;390 });391 it('should not allow reserved or oddly formed event names', function () {392 (() => {393 d.logEvent('commands');394 }).should.throw();395 (() => {396 d.logEvent(1);397 }).should.throw();398 (() => {399 d.logEvent({});400 }).should.throw();401 });402 });403 it('should allow logging the same event multiple times', function () {404 d.logEvent('bar');405 d.logEvent('bar');406 d.eventHistory.bar.should.have.length(2);407 d.eventHistory.bar[1].should.be.a('number');408 (d.eventHistory.bar[1] >= d.eventHistory.bar[0]).should.be.true;409 });410 describe('getSession decoration', function () {411 it('should decorate getSession response if opt-in cap is provided', async function () {412 let res = await d.getSession();413 should.not.exist(res.events);414 d.caps.eventTimings = true;415 res = await d.getSession();416 should.exist(res.events);417 should.exist(res.events.newSessionRequested);418 res.events.newSessionRequested[0].should.be.a('number');419 });420 });421 });422 describe('.reset', function () {423 it('should reset as W3C if the original session was W3C', async function () {424 const caps = {425 alwaysMatch: Object.assign({}, {426 app: 'Fake',427 deviceName: 'Fake',428 automationName: 'Fake',429 platformName: 'Fake',430 }, defaultCaps),431 firstMatch: [{}],432 };433 await d.createSession(undefined, undefined, caps);434 d.protocol.should.equal('W3C');435 await d.reset();436 d.protocol.should.equal('W3C');437 });438 it('should reset as MJSONWP if the original session was MJSONWP', async function () {439 const caps = Object.assign({}, {440 app: 'Fake',441 deviceName: 'Fake',442 automationName: 'Fake',443 platformName: 'Fake',444 }, defaultCaps);445 await d.createSession(caps);446 d.protocol.should.equal('MJSONWP');447 await d.reset();448 d.protocol.should.equal('MJSONWP');449 });450 });451 });452 describe('DeviceSettings', function () {453 it('should not hold on to reference of defaults in constructor', function () {454 let obj = {foo: 'bar'};455 let d1 = new DeviceSettings(obj);456 let d2 = new DeviceSettings(obj);457 d1._settings.foo = 'baz';458 d1._settings.should.not.eql(d2._settings);459 });460 });461 describe('.isFeatureEnabled', function () {462 const d = new DriverClass();463 afterEach(function () {464 d.denyInsecure = null;465 d.allowInsecure = null;466 d.relaxedSecurityEnabled = null;467 });468 it('should say a feature is enabled when it is explicitly allowed', function () {469 d.allowInsecure = ['foo', 'bar'];470 d.isFeatureEnabled('foo').should.be.true;471 d.isFeatureEnabled('bar').should.be.true;472 d.isFeatureEnabled('baz').should.be.false;473 });474 it('should say a feature is not enabled if it is not enabled', function () {475 d.allowInsecure = [];476 d.isFeatureEnabled('foo').should.be.false;477 });478 it('should prefer denyInsecure to allowInsecure', function () {479 d.allowInsecure = ['foo', 'bar'];480 d.denyInsecure = ['foo'];481 d.isFeatureEnabled('foo').should.be.false;482 d.isFeatureEnabled('bar').should.be.true;483 d.isFeatureEnabled('baz').should.be.false;484 });485 it('should allow global setting for insecurity', function () {486 d.relaxedSecurityEnabled = true;487 d.isFeatureEnabled('foo').should.be.true;488 d.isFeatureEnabled('bar').should.be.true;489 d.isFeatureEnabled('baz').should.be.true;490 });491 it('global setting should be overrideable', function () {492 d.relaxedSecurityEnabled = true;493 d.denyInsecure = ['foo', 'bar'];494 d.isFeatureEnabled('foo').should.be.false;495 d.isFeatureEnabled('bar').should.be.false;496 d.isFeatureEnabled('baz').should.be.true;497 });498 });499}...

Full Screen

Full Screen

driver-specs.js

Source:driver-specs.js Github

copy

Full Screen

1// transpile:mocha2import BaseDriver from '../..';3import baseDriverUnitTests from './driver-tests';4baseDriverUnitTests(BaseDriver, {5 platformName: 'iOS',6 deviceName: 'Delorean'...

Full Screen

Full Screen

index.js

Source:index.js Github

copy

Full Screen

1// transpile:main2import baseDriverUnitTests from './driver-tests';3import baseDriverE2ETests from './driver-e2e-tests';...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var chai = require('chai');3var chaiAsPromised = require('chai-as-promised');4var should = chai.should();5var expect = chai.expect;6chai.use(chaiAsPromised);7var serverConfig = require('./helpers/appium-servers');8var desired = require('./helpers/caps').android19;9var baseDriverUnitTests = require('./helpers/unit').baseDriverUnitTests;10var driver = wd.promiseChainRemote(serverConfig.local);11baseDriverUnitTests(desired, driver);12var wd = require('wd');13var chai = require('chai');14var chaiAsPromised = require('chai-as-promised');15var should = chai.should();16var expect = chai.expect;17chai.use(chaiAsPromised);18var serverConfig = require('./helpers/appium-servers');19var desired = require('./helpers/caps').android19;20var baseDriverUnitTests = require('./helpers/unit').baseDriverUnitTests;21var driver = wd.promiseChainRemote(serverConfig.local);22baseDriverUnitTests(desired, driver);

Full Screen

Using AI Code Generation

copy

Full Screen

1import { baseDriverUnitTests } from 'appium-base-driver';2import { AppiumDriver } from './appiumDriver';3baseDriverUnitTests(AppiumDriver, {4});5import { baseDriverUnitTests } from 'appium-base-driver';6import { AppiumDriver } from './appiumDriver';7baseDriverUnitTests(AppiumDriver, {8});9import { baseDriverUnitTests } from 'appium-base-driver';10import { AppiumDriver } from './appiumDriver';11baseDriverUnitTests(AppiumDriver, {12});13import { baseDriverUnitTests } from 'appium-base-driver';14import { AppiumDriver } from './appiumDriver';15baseDriverUnitTests(AppiumDriver, {

Full Screen

Using AI Code Generation

copy

Full Screen

1var baseDriverUnitTests = require('appium-base-driver').unitTests;2var MyDriver = require('./myDriver.js');3baseDriverUnitTests(MyDriver, {4});5baseDriverUnitTests(MyDriver, {6});7baseDriverUnitTests(MyDriver, {8 appiumCaps: {9 }10});11{12}13baseDriverUnitTests(MyDriver, {14 caps: {

Full Screen

Using AI Code Generation

copy

Full Screen

1const wd = require('wd');2const chai = require('chai');3const chaiAsPromised = require('chai-as-promised');4const { baseDriverUnitTests } = require('appium-base-driver');5chai.should();6chai.use(chaiAsPromised);7const driver = wd.promiseChainRemote();8const caps = {9};10driver.init(caps).then(async () => {11 await baseDriverUnitTests(driver);12}).finally(() => {13 driver.quit();14});

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Appium Base Driver automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful