Best JavaScript code snippet using appium-base-driver
mjsonwp.js
Source:mjsonwp.js
...133 // return a function which will add all the routes to the driver134 return function (app) {135 for (let [path, methods] of _.pairs(METHOD_MAP)) {136 for (let [method, spec] of _.pairs(methods)) {137 let isSessCmd = isSessionCommand(spec.command);138 // set up the express route handler139 buildHandler(app, method, path, spec, driver, isSessCmd);140 }141 }142 };143}144function buildHandler (app, method, path, spec, driver, isSessCmd) {145 let asyncHandler = async (req, res) => {146 let jsonObj = req.body;147 let httpResBody = {};148 let httpStatus = 200;149 let newSessionId;150 try {151 // if this is a session command but we don't have a session,...
appium.js
Source:appium.js
...159}160// help decide which commands should be proxied to sub-drivers and which161// should be handled by this, our umbrella driver162function isAppiumDriverCommand (cmd) {163 return !isSessionCommand(cmd) || cmd === "deleteSession";164}165function getAppiumRouter (args) {166 let appium = new AppiumDriver(args);167 return routeConfiguringFunction(appium);168}169export { AppiumDriver, getAppiumRouter };...
router.js
Source:router.js
1module.exports = class Router {2 constructor() {3 this.modules = [];4 this.routes = require('../routes/command.js');5 this.sessionRoutes = require('../routes/session.js');6 this.actionRoutes = require('../routes/actions.js');7 this.init();8 this.destroy_session = true;9 }10 async run(data) {1112 if (!await middleware_execute(data, true)) {13 return false;14 }1516 this.destroy_session = true;17 let payload = data.object.message.payload;18 let command;19 if (data.object.message.from_id < 0) return; // СообÑение Ð¾Ñ Ð±Ð¾Ñа2021 if (data.object.message.from_id != data.object.message.peer_id && data.type == 'message_new') {22 await SocketService.sendMessage(data.object.message);23 }2425 if (data.object.message.action != null) {26 if (data.object.message.action.type == 'chat_invite_user' && data.object.message.action.member_id == -process.env.VK_API_GROUP_ID) {27 await this.modules.mainController.invite_chat();28 }29 }30 let user = await User.getUser(data.object.message.from_id);31 if (user == null) {32 // ÐÑо ÑÐ¾Ñ ÑлÑÑай, когда Ñ Ð½Ð°Ñ Ð½ÐµÑколÑко ÑообÑений подÑÑд, а полÑзоваÑÐµÐ»Ñ ÐµÑе не Ñоздан33 // Тогда ÑообÑÐµÐ½Ð¸Ñ Ð¾Ð±ÑабаÑÑваÑÑÑÑ Ð±ÑÑÑÑее Ñем вÑÑавка в Ð±Ð°Ð·Ñ Ð¸ поÑвлÑлиÑÑ Ð´Ñбли34 return await pre_send(await render('app/errors', {type: 'biba_conflict'}, data), data.object.message.from_id)35 }36 await user.updateActive();3738 if (payload == undefined) command = data.object.message.text.toLowerCase();39 else command = (JSON.parse(payload)).content;4041 let isSessionCommand = false;42 let isActionCommand = false;43 if (user !== null) {44 let sessionCommand = await Session.getRouteSession(user.id);45 if (sessionCommand !== null) {46 command = sessionCommand.name;47 isSessionCommand = true;48 }49 }50 socketIo.emit('message', {text: command});5152 let routes = isSessionCommand ? this.sessionRoutes : this.routes;53 if (data.object.message.action !== undefined) {54 command = data.object.message.action.type;55 routes = this.actionRoutes;56 isActionCommand = true;57 }5859 for (let route of routes) {60 let reg = new RegExp(route.command, 'i');61 if (reg.test(command)) {62 let controller = route.controller.split('@');63 let options = {64 controller: {65 name: controller[0],66 method: controller[1]67 },68 command: command,69 user_id: data.object.message.peer_id,70 from_id: data.object.message.from_id,71 user: user,72 user_data: data.object.message.text.toLowerCase(),73 data: data,74 alias: route.name,75 check_spam: true,76 isAction: isActionCommand77 };78 let toreg = new RegExp("(-[0-9]|[0-9])+", 'i');79 if (data.object.message.reply_message != undefined) {80 options['reply_message'] = data.object.message.reply_message;81 options['to_id'] = data.object.message.reply_message.from_id;82 }83 else if (command.match(toreg) != null) {84 options['to_id'] = command.match(toreg)[0];85 }86 else {87 options['to_id'] = data.object.message.from_id;88 }89 options['to_id'] = (options['to_id'] == process.env.VK_API_GROUP_ID || options['to_id'] == -process.env.VK_API_GROUP_ID) ? data.object.message.from_id : options['to_id'];90 this.modules.mainController.conversation(options);91 global.conversation_message_id = data.object.message.conversation_message_id;9293 if (!await middleware_execute(options, false)) {94 return false;95 }9697 this.modules[controller[0]][controller[1]](options);98 this.destroy_session = false;99 break;100 }101 }102 if (user != null && user.session != null && this.destroy_session === true) {103 await user.destroy_session();104 await this.run(data);105 }106 }107 init() {108 let success_module = ['mainController'];109 this.modules['mainController'] = require(`../controllers/mainController.js`);110 this.routes.forEach(route => {111 let name_controller = route.controller.split('@')[0];112 if (success_module.indexOf(name_controller) == -1) {113 success_module.push(name_controller);114 this.modules[name_controller] = require(`../controllers/${name_controller}.js`);115 }116 });117 }
...
proxy-helper.js
Source:proxy-helper.js
1import { errorFromCode, errors, routeToCommandName } from 'appium-base-driver';2import log from '../logger';3import B from 'bluebird';4const SUPPORTED_METHODS = ['GET', 'POST', 'DELETE'];5let helpers = {}, extensions = {};6helpers.proxyCommand = async function (endpoint, method, body, isSessionCommand = true) {7 if (this.shutdownUnexpectedly) {8 return;9 }10 if (!endpoint) {11 log.errorAndThrow('Proxying requires an endpoint');12 } else if (SUPPORTED_METHODS.indexOf(method) === -1) {13 log.errorAndThrow(`Proxying only works for the following requests: ${SUPPORTED_METHODS.join(', ')}`);14 }15 if (!this.wda) {16 throw new Error("Can't call proxyCommand without WDA driver active");17 }18 const proxy = isSessionCommand ? this.wda.jwproxy : this.wda.noSessionProxy;19 if (!proxy) {20 throw new Error("Can't call proxyCommand without WDA proxy active");21 }22 const cmdName = routeToCommandName(endpoint, method);23 const timeout = this._getCommandTimeout(cmdName);24 let res = null;25 if (timeout) {26 log.debug(`Setting custom timeout to ${timeout} ms for "${cmdName}" command`);27 let isCommandExpired = false;28 res = await B.Promise.resolve(proxy.command(endpoint, method, body))29 .timeout(timeout)30 .catch(B.Promise.TimeoutError, () => {31 isCommandExpired = true;32 });33 if (isCommandExpired) {34 proxy.cancelActiveRequests();35 const errMsg = `Appium did not get any response from "${cmdName}" command in ${timeout} ms`;36 await this.startUnexpectedShutdown(new errors.TimeoutError(errMsg));37 log.errorAndThrow(errMsg);38 }39 } else {40 res = await proxy.command(endpoint, method, body);41 }42 // temporarily handle errors that can be returned43 if (res && res.status && parseInt(res.status, 10) !== 0) {44 throw errorFromCode(res.status, res.value);45 }46 return res;47};48Object.assign(extensions, helpers);49export { helpers };...
index.js
Source:index.js
1// transpile:main2import { MJSONWP as MobileJsonWireProtocol, isSessionCommand,3 routeConfiguringFunction } from './lib/mjsonwp';4import { NO_SESSION_ID_COMMANDS, ALL_COMMANDS } from './lib/routes';5import { errors, isErrorType, errorFromCode } from './lib/errors';6export { MobileJsonWireProtocol, routeConfiguringFunction, errors, isErrorType,...
Using AI Code Generation
1var wd = require('wd');2driver.init({3}).then(function () {4 return driver.isSessionCommand('status');5}).then(function (isSessionCommand) {6 console.log(isSessionCommand);7 return driver.quit();8});9import io.appium.java_client.AppiumDriver;10import io.appium.java_client.MobileElement;11import io.appium.java_client.ios.IOSDriver;12import io.appium.java_client.remote.MobileCapabilityType;13import org.openqa.selenium.remote.DesiredCapabilities;14import java.net.MalformedURLException;15import java.net.URL;16public class Test {17 public static void main(String[] args) throws MalformedURLException {18 DesiredCapabilities capabilities = new DesiredCapabilities();19 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "iOS");20 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9.3");21 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone 6");22 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
Using AI Code Generation
1const { BaseDriver } = require('appium-base-driver');2const { util } = require('appium-support');3class TestDriver extends BaseDriver {4 constructor(opts = {}, shouldValidateCaps = true) {5 super(opts, shouldValidateCaps);6 this.desiredCapConstraints = {7 platformName: {8 },9 };10 }11 createSession(caps) {12 return super.createSession(caps);13 }14 async executeCommand(cmd, ...args) {15 if (this.isSessionCommand(cmd)) {16 }17 return await super.executeCommand(cmd, ...args);18 }19}20module.exports = TestDriver;21const { BaseDriver } = require('appium-base-driver');22const { util } = require('appium-support');23class TestDriver extends BaseDriver {24 constructor(opts = {}, shouldValidateCaps = true) {25 super(opts, shouldValidateCaps);26 this.desiredCapConstraints = {27 platformName: {28 },29 };30 }31 createSession(caps) {32 return super.createSession(caps);33 }34 async executeCommand(cmd, ...args) {35 if (this.isSessionCommand(cmd)) {36 }37 return await super.executeCommand(cmd, ...args);38 }39}40module.exports = TestDriver;41const { BaseDriver } = require('appium-base-driver');42const { util } = require('appium-support');43class TestDriver extends BaseDriver {44 constructor(opts = {}, shouldValidateCaps = true) {45 super(opts, shouldValidateCaps);46 this.desiredCapConstraints = {47 platformName: {48 },49 };50 }51 createSession(caps) {52 return super.createSession(caps);53 }54 async executeCommand(cmd, ...args) {55 if (this.isSessionCommand(cmd)) {56 }57 return await super.executeCommand(cmd, ...args);58 }59}60module.exports = TestDriver;61const { Base
Using AI Code Generation
1const BaseDriver = require('appium-base-driver');2const AppiumDriver = new BaseDriver();3console.log(AppiumDriver.isSessionCommand('createSession'));4console.log(AppiumDriver.isSessionCommand('sessions'));5console.log(AppiumDriver.isSessionCommand('session'));6console.log(AppiumDriver.isSessionCommand('deleteSession'));7console.log(AppiumDriver.isSessionCommand('status'));
Using AI Code Generation
1const { BaseDriver } = require('appium-base-driver');2const { AppiumDriver } = require('appium-base-driver');3const { AppiumService } = require('appium');4async function run() {5 const appiumService = new AppiumService();6 await appiumService.start();7 const appiumDriver = new AppiumDriver();8 await appiumDriver.createSession();9 const baseDriver = new BaseDriver();10 baseDriver.setSession(appiumDriver.sessionId);11 const isSessionCommand = baseDriver.isSessionCommand('GET', '/session/:sessionId/element');12 console.log(isSessionCommand);13 await appiumDriver.deleteSession();14 await appiumService.stop();15}16run();17const { BaseDriver } = require('appium-base-driver');18const { AppiumDriver } = require('appium-base-driver');19const { AppiumService } = require('appium');20async function run() {21 const appiumService = new AppiumService();22 await appiumService.start();23 const appiumDriver = new AppiumDriver();24 await appiumDriver.createSession();25 const baseDriver = new BaseDriver();26 const isSessionCommand = baseDriver.isSessionCommand('GET', '/session/:sessionId/element');27 console.log(isSessionCommand);28 await appiumDriver.deleteSession();29 await appiumService.stop();30}31run();32const { BaseDriver } = require('appium-base-driver');33const { AppiumDriver } = require('appium-base-driver');34const { AppiumService } = require('appium');35async function run() {36 const appiumService = new AppiumService();37 await appiumService.start();38 const appiumDriver = new AppiumDriver();39 await appiumDriver.createSession();40 const baseDriver = new BaseDriver();41 baseDriver.setSession(appiumDriver.sessionId);42 const isSessionCommand = baseDriver.isSessionCommand('GET', '/session/:sessionId/element');
Using AI Code Generation
1const {BaseDriver} = require('appium-base-driver');2const driver = new BaseDriver();3const isSessionCommand = driver.isSessionCommand('createSession');4console.log(isSessionCommand);5const {BaseDriver} = require('appium-base-driver');6const driver = new BaseDriver();7const isSessionCommand = driver.isSessionCommand('getSessions');8console.log(isSessionCommand);9const {BaseDriver} = require('appium-base-driver');10const driver = new BaseDriver();11const isSessionCommand = driver.isSessionCommand('getScreenOrientation');12console.log(isSessionCommand);13const {BaseDriver} = require('appium-base-driver');14const driver = new BaseDriver();15const isSessionCommand = driver.isSessionCommand('getDeviceTime');16console.log(isSessionCommand);17const {BaseDriver} = require('appium-base-driver');18const driver = new BaseDriver();19const isSessionCommand = driver.isSessionCommand('getDeviceTime');20console.log(isSessionCommand);21const {BaseDriver} = require('appium-base-driver');22const driver = new BaseDriver();23const isSessionCommand = driver.isSessionCommand('getDeviceTime');24console.log(isSessionCommand);25const {BaseDriver}
Using AI Code Generation
1const { BaseDriver } = require('appium-base-driver');2let driver = new BaseDriver();3console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));4const { AndroidDriver } = require('appium-android-driver');5let driver = new AndroidDriver();6console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));7const { IOSDriver } = require('appium-ios-driver');8let driver = new IOSDriver();9console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));10const { WindowsDriver } = require('appium-windows-driver');11let driver = new WindowsDriver();12console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));13const { MacDriver } = require('appium-mac-driver');14let driver = new MacDriver();15console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));16const { YouiEngineDriver } = require('appium-youiengine-driver');17let driver = new YouiEngineDriver();18console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));19const { EspressoDriver } = require('appium-espresso-driver');20let driver = new EspressoDriver();21console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));22const { XCUITestDriver } = require('appium-xcuitest-driver');23let driver = new XCUITestDriver();24console.log(driver.isSessionCommand('/wd/hub/session/1234/element/1/click'));
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!