How to use driver.executeAsync method in Appium Xcuitest Driver

Best JavaScript code snippet using appium-xcuitest-driver

Run Appium Xcuitest Driver automation tests on LambdaTest cloud grid

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

Sign up Free
_

execute-specs.js

Source: execute-specs.js Github

copy
1/* globals expect */
2import desired from './desired';
3import setup from '../safari-setup';
4import { loadWebView } from '../../helpers/webview';
5import { MOCHA_SAFARI_TIMEOUT } from '../../helpers/session';
6
7
8const SCROLL_INTO_VIEW = `return arguments[0].scrollIntoView(true);`;
9const GET_RIGHT_INNERHTML = `return document.body.innerHTML.indexOf('I am some page content') > 0`;
10const GET_WRONG_INNERHTML = `return document.body.innerHTML.indexOf('I am not some page content') > 0`;
11const GET_ELEM_BY_TAGNAME = `return document.getElementsByTagName('a');`;
12
13describe('safari - webview - execute', function () {
14  this.timeout(MOCHA_SAFARI_TIMEOUT);
15
16  describe('http', function () {
17    const driver = setup(this, desired).driver;
18    before(async function () { return await loadWebView(desired, driver); });
19    after(async function () {
20      await driver.deleteSession();
21    });
22
23    describe('synchronous', function () {
24      it('should bubble up javascript errors', async function () {
25        await driver.execute(`'nan'--`).should.eventually.be.rejected;
26      });
27
28      it('should eval javascript', async function () {
29        (await driver.execute('return 1')).should.be.equal(1);
30      });
31
32      it('should not be returning hardcoded results', async function () {
33        (await driver.execute('return 1+1')).should.be.equal(2);
34      });
35
36      it(`should return nothing when you don't explicitly return`, async function () {
37        expect(await driver.execute('1+1')).to.not.exist;
38      });
39
40      it('should execute code inside the web view', async function () {
41        (await driver.execute(GET_RIGHT_INNERHTML)).should.be.ok;
42        (await driver.execute(GET_WRONG_INNERHTML)).should.not.be.ok;
43      });
44
45      it('should convert selenium element arg to webview element', async function () {
46        let el = await driver.findElement('id', 'useragent');
47        await driver.execute(SCROLL_INTO_VIEW, [el]);
48      });
49
50      it('should catch stale or undefined element as arg', async function () {
51        let el = await driver.findElement('id', 'useragent');
52        await driver.execute(SCROLL_INTO_VIEW, [{'ELEMENT': (el.value + 1)}]).should.be.rejected;
53      });
54
55      it('should be able to return multiple elements from javascript', async function () {
56        let res = await driver.execute(GET_ELEM_BY_TAGNAME);
57        expect(res).to.have.length.above(0);
58      });
59      it('should pass along non-element arguments', async function () {
60        let arg = 'non-element-argument';
61        (await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args[0];', [arg])).should.be.equal(arg);
62      });
63      it('should handle return values correctly', async function () {
64        let arg = ['one', 'two', 'three'];
65        (await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args;', arg)).should.eql(arg);
66      });
67    });
68
69    describe('asynchronous', function () {
70      it('should bubble up javascript errors', async function () {
71        await driver.execute(`'nan'--`).should.eventually.be.rejected;
72      });
73
74      it('should execute async javascript', async function () {
75        await driver.asyncScriptTimeout(1000);
76        (await driver.executeAsync(`arguments[arguments.length - 1](123);`)).should.be.equal(123);
77      });
78
79      it('should timeout when callback is not invoked', async function () {
80        await driver.asyncScriptTimeout(1000);
81        await driver.executeAsync(`return 1 + 2`).should.eventually.be.rejected;
82      });
83    });
84  });
85
86  describe('https', function () {
87    const driver = setup(this, Object.assign({}, desired, {enableAsyncExecuteFromHttps: true})).driver;
88    before(async function () { return await loadWebView(desired, driver, 'https://www.google.com', 'Google'); });
89    after(async function () {
90      await driver.deleteSession();
91    });
92
93    describe('synchronous', function () {
94      it('should bubble up javascript errors', async function () {
95        await driver.execute(`'nan'--`).should.eventually.be.rejected;
96      });
97
98      it('should eval javascript', async function () {
99        (await driver.execute('return 1')).should.be.equal(1);
100      });
101
102      it('should not be returning hardcoded results', async function () {
103        (await driver.execute('return 1+1')).should.be.equal(2);
104      });
105
106      it(`should return nothing when you don't explicitly return`, async function () {
107        expect(await driver.execute('1+1')).to.not.exist;
108      });
109
110      it('should pass along non-element arguments', async function () {
111        let arg = 'non-element-argument';
112        (await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args[0];', [arg])).should.be.equal(arg);
113      });
114
115      it('should handle return values correctly', async function () {
116        let arg = ['one', 'two', 'three'];
117        (await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args;', arg)).should.eql(arg);
118      });
119    });
120
121    describe('asynchronous', function () {
122      it('should bubble up javascript errors', async function () {
123        await driver.execute(`'nan'--`).should.eventually.be.rejected;
124      });
125
126      it('should execute async javascript', async function () {
127        await driver.asyncScriptTimeout(1000);
128        (await driver.executeAsync(`arguments[arguments.length - 1](123);`)).should.be.equal(123);
129      });
130
131      it('should timeout when callback is not invoked', async function () {
132        await driver.asyncScriptTimeout(1000);
133        await driver.executeAsync(`return 1 + 2`).should.eventually.be.rejected;
134      });
135    });
136  });
137});
138
Full Screen

safari-execute-e2e-specs.js

Source: safari-execute-e2e-specs.js Github

copy
1import _ from 'lodash';
2import chai from 'chai';
3import chaiAsPromised from 'chai-as-promised';
4import http from 'http';
5import { SAFARI_CAPS } from '../desired';
6import { initSession, deleteSession, MOCHA_TIMEOUT } from '../helpers/session';
7import { openPage, GUINEA_PIG_PAGE } from './helpers';
8
9
10chai.should();
11chai.use(chaiAsPromised);
12const expect = chai.expect;
13
14const SCROLL_INTO_VIEW = `return arguments[0].scrollIntoView(true);`;
15const GET_RIGHT_INNERHTML = `return document.body.innerHTML.indexOf('I am some page content') > 0`;
16const GET_WRONG_INNERHTML = `return document.body.innerHTML.indexOf('I am not some page content') > 0`;
17const GET_ELEM_BY_TAGNAME = `return document.getElementsByTagName('a');`;
18
19describe('safari - execute -', function () {
20  this.timeout(MOCHA_TIMEOUT);
21
22  let driver;
23  before(async function () {
24    const caps = _.defaults({
25      safariInitialUrl: GUINEA_PIG_PAGE,
26      showSafariConsoleLog: true,
27    }, SAFARI_CAPS);
28    driver = await initSession(caps);
29  });
30  after(async function () {
31    await deleteSession();
32  });
33
34  async function runTests (secure = false) { // eslint-disable-line require-await
35    describe('mobile: x methods', function () {
36      it('should run in native context', async function () {
37        await driver.execute('mobile: scroll', {direction: 'down'}).should.not.be.rejected;
38      });
39    });
40
41    describe('synchronous', function () {
42      it('should bubble up javascript errors', async function () {
43        await driver.execute(`'nan'--`).should.eventually.be.rejected;
44      });
45
46      it('should eval javascript', async function () {
47        await driver.execute('return 1 + 1').should.eventually.equal(2);
48      });
49
50      it('should not be returning hardcoded results', async function () {
51        await driver.execute('return 1+1').should.eventually.equal(2);
52      });
53
54      it(`should return nothing when you don't explicitly return`, async function () {
55        expect(await driver.execute('1+1')).to.not.exist;
56      });
57
58      if (!secure) {
59        it('should execute code inside the web view', async function () {
60          await driver.execute(GET_RIGHT_INNERHTML).should.eventually.be.ok;
61          await driver.execute(GET_WRONG_INNERHTML).should.eventually.not.be.ok;
62        });
63
64        it('should convert selenium element arg to webview element', async function () {
65          const el = await driver.elementById('useragent');
66          await driver.execute(SCROLL_INTO_VIEW, [el]);
67        });
68
69        it('should catch stale or undefined element as arg', async function () {
70          const el = await driver.elementById('useragent');
71          return driver.execute(SCROLL_INTO_VIEW, [{'ELEMENT': (el.value + 1)}])
72            .should.eventually.be.rejectedWith(/Error converting element ID/);
73        });
74
75        it('should be able to return multiple elements from javascript', async function () {
76          await driver.execute(GET_ELEM_BY_TAGNAME)
77            .should.eventually.have.length.above(0);
78        });
79      }
80
81      it('should pass along non-element arguments', async function () {
82        const arg = 'non-element-argument';
83        await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args[0];', [arg])
84          .should.eventually.equal(arg);
85      });
86
87      it('should handle return values correctly', async function () {
88        const arg = ['one', 'two', 'three'];
89        await driver.execute('var args = Array.prototype.slice.call(arguments, 0); return args;', arg)
90          .should.eventually.eql(arg);
91      });
92    });
93
94    describe('asynchronous', function () {
95      it('should execute async javascript', async function () {
96        await driver.setAsyncScriptTimeout(1000);
97        await driver.executeAsync(`arguments[arguments.length - 1](123);`)
98          .should.eventually.equal(123);
99      });
100
101      it('should bubble up errors', async function () {
102        await driver.executeAsync(`arguments[arguments.length - 1]('nan'--);`)
103          .should.eventually.be.rejectedWith(/operator applied to value that is not a reference/);
104      });
105
106      it('should timeout when callback is not invoked', async function () {
107        await driver.setAsyncScriptTimeout(1000);
108        await driver.executeAsync(`return 1 + 2`)
109          .should.eventually.be.rejectedWith(/Timed out waiting for/);
110      });
111    });
112  }
113
114  describe('http', function () {
115    runTests();
116    describe('cors', function () {
117      let server;
118      const host = '127.0.0.1';
119      const port = 8080;
120      before(function () {
121        // create an http server so we can test CORS handling without
122        // going to an external site
123        server = http.createServer(function (req, res) {
124          res.writeHead(200, {'Content-Type': 'text/html'});
125          res.write('appium-xcuitest-driver async execute tests');
126          res.end();
127        }).listen({host, port});
128      });
129      after(function () {
130        if (server) {
131          server.close();
132        }
133      });
134
135      it('should execute async javascript from a different site', async function () {
136        await driver.get(`http://${host}:${port}`);
137        await driver.setAsyncScriptTimeout(1000);
138        await driver.executeAsync(`arguments[arguments.length - 1](123);`)
139          .should.eventually.equal(123);
140      });
141    });
142  });
143  describe('https', function () {
144    before(async function () {
145      await openPage(driver, 'https://google.com');
146    });
147    runTests(true);
148  });
149});
150
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 Xcuitest 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)