How to use startTimeoutSession 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.

driver-e2e-tests.js

Source: driver-e2e-tests.js Github

copy
1import _ from 'lodash';
2import { server, routeConfiguringFunction, DeviceSettings } from '../..';
3import request from 'request-promise';
4import chai from 'chai';
5import chaiAsPromised from 'chai-as-promised';
6import B from 'bluebird';
7
8const should = chai.should();
9chai.use(chaiAsPromised);
10
11function baseDriverE2ETests (DriverClass, defaultCaps = {}) {
12  describe('BaseDriver (e2e)', function () {
13    let baseServer, d = new DriverClass();
14    before(async function () {
15      baseServer = await server(routeConfiguringFunction(d), 8181);
16    });
17    after(async function () {
18      await baseServer.close();
19    });
20
21    function startSession (caps) {
22      return request({
23        url: 'http://localhost:8181/wd/hub/session',
24        method: 'POST',
25        json: {desiredCapabilities: caps, requiredCapabilities: {}},
26      });
27    }
28
29    function endSession (id) {
30      return request({
31        url: `http://localhost:8181/wd/hub/session/${id}`,
32        method: 'DELETE',
33        json: true,
34        simple: false
35      });
36    }
37
38    function getSession (id) {
39      return request({
40        url: `http://localhost:8181/wd/hub/session/${id}`,
41        method: 'GET',
42        json: true,
43        simple: false
44      });
45    }
46
47    describe('session handling', function () {
48      it('should create session and retrieve a session id, then delete it', async function () {
49        let res = await request({
50          url: 'http://localhost:8181/wd/hub/session',
51          method: 'POST',
52          json: {desiredCapabilities: defaultCaps, requiredCapabilities: {}},
53          simple: false,
54          resolveWithFullResponse: true
55        });
56
57        res.statusCode.should.equal(200);
58        res.body.status.should.equal(0);
59        should.exist(res.body.sessionId);
60        res.body.value.should.eql(defaultCaps);
61
62        res = await request({
63          url: `http://localhost:8181/wd/hub/session/${d.sessionId}`,
64          method: 'DELETE',
65          json: true,
66          simple: false,
67          resolveWithFullResponse: true
68        });
69
70        res.statusCode.should.equal(200);
71        res.body.status.should.equal(0);
72        should.equal(d.sessionId, null);
73      });
74    });
75
76    it.skip('should throw NYI for commands not implemented', async function () {
77    });
78
79    describe('command timeouts', function () {
80      function startTimeoutSession (timeout) {
81        let caps = _.clone(defaultCaps);
82        caps.newCommandTimeout = timeout;
83        return startSession(caps);
84      }
85
86      d.findElement = function () {
87        return 'foo';
88      }.bind(d);
89
90      d.findElements = async function () {
91        await B.delay(200);
92        return ['foo'];
93      }.bind(d);
94
95      it('should set a default commandTimeout', async function () {
96        let newSession = await startTimeoutSession();
97        d.newCommandTimeoutMs.should.be.above(0);
98        await endSession(newSession.sessionId);
99      });
100
101      it('should timeout on commands using commandTimeout cap', async function () {
102        let newSession = await startTimeoutSession(0.25);
103
104        await request({
105          url: `http://localhost:8181/wd/hub/session/${d.sessionId}/element`,
106          method: 'POST',
107          json: {using: 'name', value: 'foo'},
108        });
109        await B.delay(400);
110        let res = await request({
111          url: `http://localhost:8181/wd/hub/session/${d.sessionId}`,
112          method: 'GET',
113          json: true,
114          simple: false
115        });
116        res.status.should.equal(6);
117        should.equal(d.sessionId, null);
118        res = await endSession(newSession.sessionId);
119        res.status.should.equal(6);
120      });
121
122      it('should not timeout with commandTimeout of false', async function () {
123        let newSession = await startTimeoutSession(0.1);
124        let start = Date.now();
125        let res = await request({
126          url: `http://localhost:8181/wd/hub/session/${d.sessionId}/elements`,
127          method: 'POST',
128          json: {using: 'name', value: 'foo'},
129        });
130        (Date.now() - start).should.be.above(150);
131        res.value.should.eql(['foo']);
132        await endSession(newSession.sessionId);
133      });
134
135      it('should not timeout with commandTimeout of 0', async function () {
136        d.newCommandTimeoutMs = 2;
137        let newSession = await startTimeoutSession(0);
138
139        await request({
140          url: `http://localhost:8181/wd/hub/session/${d.sessionId}/element`,
141          method: 'POST',
142          json: {using: 'name', value: 'foo'},
143        });
144        await B.delay(400);
145        let res = await request({
146          url: `http://localhost:8181/wd/hub/session/${d.sessionId}`,
147          method: 'GET',
148          json: true,
149          simple: false
150        });
151        res.status.should.equal(0);
152        res = await endSession(newSession.sessionId);
153        res.status.should.equal(0);
154
155        d.newCommandTimeoutMs = 60 * 1000;
156      });
157
158      it('should not timeout if its just the command taking awhile', async function () {
159        let newSession = await startTimeoutSession(0.25);
160        await request({
161          url: `http://localhost:8181/wd/hub/session/${d.sessionId}/element`,
162          method: 'POST',
163          json: {using: 'name', value: 'foo'},
164        });
165        await B.delay(400);
166        let res = await request({
167          url: `http://localhost:8181/wd/hub/session/${d.sessionId}`,
168          method: 'GET',
169          json: true,
170          simple: false
171        });
172        res.status.should.equal(6);
173        should.equal(d.sessionId, null);
174        res = await endSession(newSession.sessionId);
175        res.status.should.equal(6);
176      });
177
178      it('should not have a timer running before or after a session', async function () {
179        should.not.exist(d.noCommandTimer);
180        let newSession = await startTimeoutSession(0.25);
181        newSession.sessionId.should.equal(d.sessionId);
182        should.exist(d.noCommandTimer);
183        await endSession(newSession.sessionId);
184        should.not.exist(d.noCommandTimer);
185      });
186
187    });
188
189    describe('settings api', function () {
190      before(function () {
191        d.settings = new DeviceSettings({ignoreUnimportantViews: false});
192      });
193      it('should be able to get settings object', function () {
194        d.settings.getSettings().ignoreUnimportantViews.should.be.false;
195      });
196      it('should throw error when updateSettings method is not defined', async function () {
197        await d.settings.update({ignoreUnimportantViews: true}).should.eventually
198                .be.rejectedWith('onSettingsUpdate');
199      });
200      it('should throw error for invalid update object', async function () {
201        await d.settings.update('invalid json').should.eventually
202                .be.rejectedWith('JSON');
203      });
204    });
205
206    describe('unexpected exits', function () {
207      it('should reject a current command when the driver crashes', async function () {
208        d._oldGetStatus = d.getStatus;
209        d.getStatus = async function () {
210          await B.delay(5000);
211        }.bind(d);
212        let p = request({
213          url: 'http://localhost:8181/wd/hub/status',
214          method: 'GET',
215          json: true,
216          simple: false
217        });
218        // make sure that the request gets to the server before our shutdown
219        await B.delay(100);
220        d.startUnexpectedShutdown(new Error('Crashytimes'));
221        let res = await p;
222        res.status.should.equal(13);
223        res.value.message.should.contain('Crashytimes');
224        await d.onUnexpectedShutdown.should.be.rejectedWith('Crashytimes');
225      });
226    });
227
228    describe('event timings', function () {
229      it('should not add timings if not using opt-in cap', async function () {
230        let session = await startSession(defaultCaps);
231        let res = await getSession(session.sessionId);
232        should.not.exist(res.events);
233        await endSession(session.sessionId);
234      });
235      it('should add start session timings', async function () {
236        let caps = Object.assign({}, defaultCaps, {eventTimings: true});
237        let session = await startSession(caps);
238        let res = (await getSession(session.sessionId)).value;
239        should.exist(res.events);
240        should.exist(res.events.newSessionRequested);
241        should.exist(res.events.newSessionStarted);
242        res.events.newSessionRequested[0].should.be.a('number');
243        res.events.newSessionStarted[0].should.be.a('number');
244        await endSession(session.sessionId);
245      });
246    });
247
248  });
249}
250
251export default baseDriverE2ETests;
252
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)