How to use patchWithSessionId method in Appium Base Driver

Best JavaScript code snippet using appium-base-driver

Run Appium Base Driver automation tests on LambdaTest cloud grid

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

middleware.js

Source: middleware.js Github

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

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Appium Base Driver on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)