How to use defaultToJSONContentType method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

server.js

Source:server.js Github

copy

Full Screen

1import path from 'path';2import express from 'express';3import http from 'http';4import favicon from 'serve-favicon';5import bodyParser from 'body-parser';6import methodOverride from 'method-override';7import log from './logger';8import { startLogFormatter, endLogFormatter } from './express-logging';9import { allowCrossDomain, fixPythonContentType, defaultToJSONContentType,10 catchAllHandler, catch404Handler, catch4XXHandler,11 allowCrossDomainAsyncExecute} from './middleware';12import { guineaPig, guineaPigScrollable, guineaPigAppBanner, welcome, STATIC_DIR } from './static';13import { produceError, produceCrash } from './crash';14import { addWebSocketHandler, removeWebSocketHandler, removeAllWebSocketHandlers,15 getWebSocketHandlers } from './websocket';16import B from 'bluebird';17async function server (configureRoutes, port, hostname = null, allowCors = true) {18 // create the actual http server19 const app = express();20 let httpServer = http.createServer(app);21 httpServer.addWebSocketHandler = addWebSocketHandler;22 httpServer.removeWebSocketHandler = removeWebSocketHandler;23 httpServer.removeAllWebSocketHandlers = removeAllWebSocketHandlers;24 httpServer.getWebSocketHandlers = getWebSocketHandlers;25 // http.Server.close() only stops new connections, but we need to wait until26 // all connections are closed and the `close` event is emitted27 const close = httpServer.close.bind(httpServer);28 httpServer.close = async () => {29 return await new B((resolve, reject) => {30 httpServer.on('close', resolve);31 close((err) => {32 if (err) reject(err); // eslint-disable-line curly33 });34 });35 };36 return await new B((resolve, reject) => {37 httpServer.on('error', (err) => {38 if (err.code === 'EADDRNOTAVAIL') {39 log.error('Could not start REST http interface listener. ' +40 'Requested address is not available.');41 } else {42 log.error('Could not start REST http interface listener. The requested ' +43 'port may already be in use. Please make sure there is no ' +44 'other instance of this server running already.');45 }46 reject(err);47 });48 httpServer.on('connection', (socket) => {49 socket.setTimeout(600 * 1000); // 10 minute timeout50 socket.on('error', reject);51 });52 configureServer(app, configureRoutes, allowCors);53 let serverArgs = [port];54 if (hostname) {55 // If the hostname is omitted, the server will accept56 // connections on any IP address57 serverArgs.push(hostname);58 }59 httpServer.listen(...serverArgs, (err) => {60 if (err) {61 reject(err);62 }63 resolve(httpServer);64 });65 });66}67function configureServer (app, configureRoutes, allowCors = true) {68 app.use(endLogFormatter);69 // set up static assets70 app.use(favicon(path.resolve(STATIC_DIR, 'favicon.ico')));71 app.use(express.static(STATIC_DIR));72 // crash routes, for testing73 app.use('/wd/hub/produce_error', produceError);74 app.use('/wd/hub/crash', produceCrash);75 // add middlewares76 if (allowCors) {77 app.use(allowCrossDomain);78 } else {79 app.use(allowCrossDomainAsyncExecute);80 }81 app.use(fixPythonContentType);82 app.use(defaultToJSONContentType);83 app.use(bodyParser.urlencoded({extended: true}));84 app.use(methodOverride());85 app.use(catch4XXHandler);86 app.use(catchAllHandler);87 // make sure appium never fails because of a file size upload limit88 app.use(bodyParser.json({limit: '1gb'}));89 // set up start logging (which depends on bodyParser doing its thing)90 app.use(startLogFormatter);91 configureRoutes(app);92 // dynamic routes for testing, etc.93 app.all('/welcome', welcome);94 app.all('/test/guinea-pig', guineaPig);95 app.all('/test/guinea-pig-scrollable', guineaPigScrollable);96 app.all('/test/guinea-pig-app-banner', guineaPigAppBanner);97 // catch this last, so anything that falls through is 404ed98 app.use(catch404Handler);99}...

Full Screen

Full Screen

middleware.js

Source:middleware.js Github

copy

Full Screen

1import _ from 'lodash';2import log from './logger';3import { errors } from '../protocol';4import { handleIdempotency } from './idempotency';5function allowCrossDomain (req, res, next) {6 try {7 res.header('Access-Control-Allow-Origin', '*');8 res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, OPTIONS, DELETE');9 res.header('Access-Control-Allow-Headers', 'Cache-Control, Pragma, Origin, X-Requested-With, Content-Type, Accept, User-Agent');10 // need to respond 200 to OPTIONS11 if ('OPTIONS' === req.method) {12 return res.sendStatus(200);13 }14 } catch (err) {15 log.error(`Unexpected error: ${err.stack}`);16 }17 next();18}19function allowCrossDomainAsyncExecute (basePath) {20 return (req, res, next) => {21 // there are two paths for async responses, so cover both22 // https://regex101.com/r/txYiEz/123 const receiveAsyncResponseRegExp = new RegExp(`${_.escapeRegExp(basePath)}/session/[a-f0-9-]+/(appium/)?receive_async_response`);24 if (!receiveAsyncResponseRegExp.test(req.url)) {25 return next();26 }27 allowCrossDomain(req, res, next);28 };29}30function fixPythonContentType (basePath) {31 return (req, res, next) => {32 // hack because python client library gives us wrong content-type33 if (new RegExp(`^${_.escapeRegExp(basePath)}`).test(req.path) && /^Python/.test(req.headers['user-agent'])) {34 if (req.headers['content-type'] === 'application/x-www-form-urlencoded') {35 req.headers['content-type'] = 'application/json; charset=utf-8';36 }37 }38 next();39 };40}41function defaultToJSONContentType (req, res, next) {42 if (!req.headers['content-type']) {43 req.headers['content-type'] = 'application/json; charset=utf-8';44 }45 next();46}47function catchAllHandler (err, req, res, next) {48 if (res.headersSent) {49 return next(err);50 }51 log.error(`Uncaught error: ${err.message}`);52 log.error('Sending generic error response');53 const error = errors.UnknownError;54 res.status(error.w3cStatus()).json(patchWithSessionId(req, {55 status: error.code(),56 value: {57 error: error.error(),58 message: `An unknown server-side error occurred while processing the command: ${err.message}`,59 stacktrace: err.stack,60 }61 }));62 log.error(err);63}64function catch404Handler (req, res) {65 log.debug(`No route found for ${req.url}`);66 const error = errors.UnknownCommandError;67 res.status(error.w3cStatus()).json(patchWithSessionId(req, {68 status: error.code(),69 value: {70 error: error.error(),71 message: 'The requested resource could not be found, or a request was ' +72 'received using an HTTP method that is not supported by the mapped ' +73 'resource',74 stacktrace: '',75 }76 }));77}78const SESSION_ID_PATTERN = /\/session\/([^/]+)/;79function patchWithSessionId (req, body) {80 const match = SESSION_ID_PATTERN.exec(req.url);81 if (match) {82 body.sessionId = match[1];83 }84 return body;85}86export {87 allowCrossDomain, fixPythonContentType, defaultToJSONContentType,88 catchAllHandler, allowCrossDomainAsyncExecute, handleIdempotency,89 catch404Handler,...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var webdriver = require('selenium-webdriver'),2 until = webdriver.until;3var driver = new webdriver.Builder()4 .forBrowser('chrome')5 .build();6driver.findElement(By.name('q')).sendKeys('webdriver');7driver.findElement(By.name('btnG')).click();8driver.wait(until.titleIs('webdriver - Google Search'), 1000);9driver.quit();10var webdriver = require('selenium-webdriver'),11 until = webdriver.until;12var driver = new webdriver.Builder()13 .forBrowser('chrome')14 .build();15driver.findElement(By.name('q')).sendKeys('webdriver');16driver.findElement(By.name('btnG')).click();17driver.wait(until.titleIs('webdriver - Google Search'), 1000);18driver.quit();19var webdriver = require('selenium-webdriver'),20 until = webdriver.until;21var driver = new webdriver.Builder()22 .forBrowser('chrome')23 .build();24driver.findElement(By.name('q')).sendKeys('webdriver');25driver.findElement(By.name('btnG')).click();26driver.wait(until.titleIs('webdriver - Google Search'), 1000);27driver.quit();28var webdriver = require('selenium-webdriver'),29 until = webdriver.until;30var driver = new webdriver.Builder()31 .forBrowser('chrome')32 .build();33driver.findElement(By.name('q')).sendKeys('webdriver');34driver.findElement(By.name('btnG')).click();35driver.wait(until.titleIs('webdriver - Google Search'), 1000);36driver.quit();

Full Screen

Using AI Code Generation

copy

Full Screen

1var wd = require('wd');2var desired = {3};4var driver = wd.promiseChainRemote('localhost', 4723);5driver.init(desired).then(function () {6 return driver.defaultToJSONContentType();7}).then(function () {8 return driver.source();9}).then(function (source) {10 console.log(source);11}).fin(function () {12 return driver.quit();13}).done();14{15 "scripts": {16 },17 "dependencies": {

Full Screen

Using AI Code Generation

copy

Full Screen

1const appiumBaseDriver = require('appium-base-driver');2appiumBaseDriver.prototype.defaultToJSONContentType = function () {3 return true;4};5const appiumBaseDriver = require('appium-base-driver');6appiumBaseDriver.prototype.defaultToJSONContentType = function () {7 return true;8};9const appiumBaseDriver = require('appium-base-driver');10appiumBaseDriver.prototype.defaultToJSONContentType = function () {11 return true;12};13const appiumBaseDriver = require('appium-base-driver');14appiumBaseDriver.prototype.defaultToJSONContentType = function () {15 return true;16};17const appiumBaseDriver = require('appium-base-driver');18appiumBaseDriver.prototype.defaultToJSONContentType = function () {19 return true;20};21const appiumBaseDriver = require('appium-base-driver');22appiumBaseDriver.prototype.defaultToJSONContentType = function () {23 return true;24};25const appiumBaseDriver = require('appium-base-driver');26appiumBaseDriver.prototype.defaultToJSONContentType = function () {27 return true;28};29const appiumBaseDriver = require('appium-base-driver');30appiumBaseDriver.prototype.defaultToJSONContentType = function () {31 return true;32};33const appiumBaseDriver = require('appium-base-driver');34appiumBaseDriver.prototype.defaultToJSONContentType = function () {35 return true;36};37const appiumBaseDriver = require('appium-base-driver');38appiumBaseDriver.prototype.defaultToJSONContentType = function () {39 return true;40};41const appiumBaseDriver = require('appium-base-driver');42appiumBaseDriver.prototype.defaultToJSONContentType = function () {43 return true;44};

Full Screen

Using AI Code Generation

copy

Full Screen

1const { defaultToJSONContentType } = require('appium-base-driver');2const { BaseDriver } = require('appium-base-driver');3const { AndroidDriver } = require('appium-android-driver');4const { IosDriver } = require('appium-ios-driver');5class MyDriver extends BaseDriver {6 constructor(opts = {}, shouldValidateCaps = true) {7 super(opts, shouldValidateCaps);8 this.desiredCapConstraints = {9 platformName: {10 },11 platformVersion: {12 },13 deviceName: {14 },15 };16 }17 async createSession(caps) {18 let driver;19 switch (caps.platformName) {20 driver = new AndroidDriver();21 break;22 driver = new IosDriver();23 break;24 throw new Error(`Unsupported platformName: ${caps.platformName}`);25 }26 return await driver.createSession(caps);27 }28}29const myDriver = new MyDriver();30myDriver.defaultToJSONContentType = defaultToJSONContentType;31module.exports = MyDriver;32const { defaultToJSONContentType } = require('appium-base-driver');33const { BaseDriver } = require('appium-base-driver');34const { AndroidDriver } = require('appium-android-driver');35const { IosDriver } = require('appium-ios-driver');36class MyDriver extends BaseDriver {37 constructor(opts = {}, shouldValidateCaps = true) {38 super(opts, shouldValidateCaps);39 this.desiredCapConstraints = {40 platformName: {41 },42 platformVersion: {43 },44 deviceName: {45 },46 };47 }48 async createSession(caps) {49 let driver;50 switch (caps.platformName) {51 driver = new AndroidDriver();52 break;53 driver = new IosDriver();54 break;55 throw new Error(`Unsupported platformName: ${caps.platformName}`);56 }57 return await driver.createSession(caps);58 }59}

Full Screen

Using AI Code Generation

copy

Full Screen

1const { BaseDriver } = require('appium-base-driver');2const { server, routeConfiguringFunction } = require('appium-express');3const { defaultToJSONContentType } = BaseDriver;4const PORT = 4723;5const appium = server(routeConfiguringFunction, { port: PORT, basePath: '' });6appium.listen(PORT, function () {7 console.log('Appium REST http interface listener started on port ' + PORT);8});9defaultToJSONContentType(appium);10appium.post('/wd/hub/session', (req, res) => {11 res.send('test');12});13const { BaseDriver } = require('appium-base-driver');14const { server, routeConfiguringFunction } = require('appium-express');15const { defaultToJSONContentType } = BaseDriver;16const PORT = 4723;17const appium = server(routeConfiguringFunction, { port: PORT, basePath: '' });18appium.listen(PORT, function () {19 console.log('Appium REST http interface listener started on port ' + PORT);20});21defaultToJSONContentType(appium);22appium.post('/wd/hub/session', (req, res) => {23 res.send('test');24});25const { server, routeConfiguringFunction } = require('appium-express');26const PORT = 4723;27const appium = server(routeConfiguringFunction, { port: PORT, basePath: '' });28appium.listen(PORT, function () {29 console.log('Appium REST http interface listener started on port ' + PORT);30});31appium.post('/wd/hub/session', (req, res) => {32 res.send('test');33});34const { server, routeConfiguringFunction } = require('appium-express');35const PORT = 4723;36const appium = server(routeConfiguringFunction, { port: PORT, basePath: '' });37appium.listen(PORT, function () {38 console.log('Appium REST http interface listener started on port ' + PORT);39});40appium.post('/wd/hub/session', (req, res) => {41 res.send('test');42});43const { server, routeConfiguringFunction } = require('appium-express');

Full Screen

Using AI Code Generation

copy

Full Screen

1"use strict";2var wd = require("wd");3var assert = require("assert");4var caps = require("./caps");5 .init(caps.ios)6 .then(function() {7 return driver.defaultToJSONContentType();8 })9 .then(function() {10 return driver.source();11 })12 .then(function(source) {13 console.log("Source", source);14 return driver.quit();15 })16 .done();17"use strict";18exports.ios = {19};20{21 "scripts": {22 },23 "dependencies": {24 }25}26{27 "dependencies": {28 "appium": {

Full Screen

Using AI Code Generation

copy

Full Screen

1const {BaseDriver} = require('appium-base-driver');2const {defaultToJSONContentType} = BaseDriver;3defaultToJSONContentType();4const {BaseDriver} = require('appium-base-driver');5const {defaultToJSONContentType} = BaseDriver;6defaultToJSONContentType();7const {BaseDriver} = require('appium-base-driver');8const {defaultToJSONContentType} = BaseDriver;9defaultToJSONContentType();10const {BaseDriver} = require('appium-base-driver');11const {defaultToJSONContentType} = BaseDriver;12defaultToJSONContentType();13const {BaseDriver} = require('appium-base-driver');14const {defaultToJSONContentType} = BaseDriver;15defaultToJSONContentType();16const {BaseDriver} = require('appium-base-driver');17const {defaultToJSONContentType} = BaseDriver;18defaultToJSONContentType();19const {BaseDriver} = require('appium-base-driver');20const {defaultToJSONContentType} = BaseDriver;21defaultToJSONContentType();22const {BaseDriver} = require('appium-base-driver');23const {defaultToJSONContentType} = BaseDriver;24defaultToJSONContentType();25const {BaseDriver} = require('appium-base-driver');26const {defaultToJSONContentType} = BaseDriver;27defaultToJSONContentType();28const {BaseDriver} = require('appium-base-driver');29const {defaultToJSONContentType} = BaseDriver;30defaultToJSONContentType();31const {BaseDriver} = require('appium-base-driver');32const {defaultToJSONContentType} = BaseDriver;33defaultToJSONContentType();34const {BaseDriver} = require('appium-base-driver');

Full Screen

Using AI Code Generation

copy

Full Screen

1const BaseDriver = require('appium-base-driver');2const defaultToJSONContentType = BaseDriver.defaultToJSONContentType;3const W3CActions = require('appium-base-driver').W3C_ACTIONS;4describe('defaultToJSONContentType', () => {5 it('should return application/json', () => {6 defaultToJSONContentType('application/json').should.eql('application/json');7 });8 it('should return application/json', () => {9 defaultToJSONContentType('application/json; charset=utf-8').should.eql('application/json');10 });11 it('should return application/json', () => {12 defaultToJSONContentType('application/json;charset=utf-8').should.eql('application/json');13 });14 it('should return application/json', () => {15 defaultToJSONContentType('application/json; charset=utf-8;').should.eql('application/json');16 });17 it('should return application/json', () => {18 defaultToJSONContentType('application/json;charset=utf-8;').should.eql('application/json');19 });20 it('should return application/json', () => {21 defaultToJSONContentType('application/json; charset=utf-8;').should.eql('application/json');22 });23 it('should return application/json', () => {24 defaultToJSONContentType('application/json;charset=utf-8;').should.eql('application/json');25 });26 it('should return application/json', () => {27 defaultToJSONContentType('application/json; charset=utf-8;').should.eql('application/json');28 });29 it('should return application/json', () => {30 defaultToJSONContentType('application/json;charset=utf-8;').should.eql('application/json');31 });32 it('should return application/json', () => {33 defaultToJSONContentType('application/json; charset=utf-8;').should.eql('application/json');34 });35 it('should return application/json', () => {36 defaultToJSONContentType('application/json;charset=utf-8;').should.eql('application/json');37 });38 it('should return application/json', () => {39 defaultToJSONContentType('application/json; charset=utf-8;').should.eql('application/json');40 });41 it('should return application/json', () => {42 defaultToJSONContentType('application/json;charset=utf-8;').should.eql('application/json');43 });44 it('

Full Screen

Using AI Code Generation

copy

Full Screen

1driver.defaultToJSONContentType();2driver.executeScript("mobile: scroll", {direction: "down"});3driver.executeScript("mobile: scroll", {direction: "up"});4driver.executeScript("mobile: scroll", {direction: "left"});5driver.executeScript("mobile: scroll", {direction: "right"});6driver.executeScript("mobile: scroll", {direction: "down", element: element});7driver.executeScript("mobile: scroll", {direction: "up", element: element});8driver.executeScript("mobile: scroll", {direction: "left", element: element});9driver.executeScript("mobile: scroll", {direction: "right", element: element});10driver.executeScript("mobile: scroll", {direction: "down", element: element, toVisible: true});11driver.executeScript("mobile: scroll", {direction: "up", element: element, toVisible: true});12driver.executeScript("mobile: scroll", {direction: "left", element: element, toVisible: true});13driver.executeScript("mobile: scroll", {direction: "right", element: element, toVisible: true});14driver.executeScript("mobile: scroll", {direction: "down", element: element, toVisible: true, predicateString: "name == 'test'"});

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