How to use driver.toggleData method in Appium Android Driver

Best JavaScript code snippet using appium-android-driver

Run Appium Android Driver automation tests on LambdaTest cloud grid

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

sensor.test.js

Source: sensor.test.js Github

copy
1
2const SensorPage = require('../../pageObjects/sensors.page.js');
3const expect = require('chai').expect;
4const multiremote = require('webdriverio');
5
6
7const app_package = "com.android.settings"
8const app_package_activity_setting_home = "com.android.settings.Settings"
9const app_package_activity_wifi = "com.android.settings.Settings$WifiSettings2Activity"
10const app_package_activity_location = "com.android.settings.Settings$LocationSettingsActivity"
11
12
13describe('Android Sensor tests', () => {
14    let sensorPage = SensorPage;
15    const delay = ms => new Promise(res => setTimeout(res, ms));
16    autoLaunch = false;
17
18    // before(async () => {
19    //     driver.reset();
20    // });
21
22
23    // beforeEach(() => {
24    //     driver.launchApp();
25    // });
26
27
28    it('wifi', async () => {
29        
30        await delay(3000);
31
32        await driver.startActivity(app_package,app_package_activity_wifi )
33        await delay(3000);
34 
35        expect(await(await sensorPage.device_connected).isDisplayed()).equal(true);
36
37        await driver.toggleWiFi();
38        await delay(5000);
39
40        expect(await(await sensorPage.device_disconnected).isDisplayed()).equal(true);
41
42        await driver.toggleWiFi();
43        await delay(8000);
44        expect(await(await sensorPage.device_connected).isDisplayed()).equal(true);
45  
46
47    });
48
49    it('blutooh', async () => {
50        
51
52
53        await driver.startActivity(app_package,app_package_activity_setting_home )
54
55        await(await sensorPage.connected_device).click()
56        await delay(3000);
57
58        await(await sensorPage.connected_preference).click()
59        await delay(3000);
60
61        await(await sensorPage.blue).click()
62        await delay(3000);
63
64        await(await sensorPage.blue_sw).click()
65        await delay(5000);
66
67        expect(await(await sensorPage.blue_Device).isDisplayed()).equal(true);
68        await delay(3000);
69
70        await(await sensorPage.blue_sw).click()
71        await delay(3000);
72
73
74
75    });
76
77
78        it('orientation', async () => {
79        
80        await delay(2000);
81
82        await driver.startActivity(app_package,app_package_activity_setting_home )
83        // expect(await(await sensorPage.connected).isDisplayed()).equal(true);
84
85        let orientation = await driver.getOrientation();
86        expect(orientation).equal('PORTRAIT');
87
88        await delay(2000);
89        await driver.setOrientation("LANDSCAPE");
90
91        let orientation_1 = await driver.getOrientation();
92        expect(orientation_1).equal('LANDSCAPE');
93
94        await delay(2000);
95        await driver.setOrientation("PORTRAIT");
96
97        let a_orientation = await driver.getOrientation();
98        expect(a_orientation).equal('PORTRAIT');
99
100    });
101
102    // it('airplane mode', async () => {
103        
104    //     await delay(2000);
105    //     await driver.openNotifications();
106    //     await delay(2000);
107    //     await driver.toggleAirplaneMode();      
108    //     await delay(3000);
109    //     let status_airplane_on = await (await sensorPage.Airplane_).getText()
110    //     expect(status_airplane_on).equal('On')
111    //     console.log('turnedon airplane mode')
112
113    //     await driver.toggleAirplaneMode();
114    //     await delay(3000);
115    //     let status_airplane_off = await (await sensorPage.Airplane_).getText()
116    //     expect(status_airplane_off).equal('Off')
117    //     console.log('turnedoff airplane mode')
118
119    // });
120
121    it(' data', async () => {
122           
123        // await delay(2000);
124        await driver.openNotifications();
125        await delay(3000);
126        await driver.toggleData();  
127        await delay(3000);
128        let status_Mdata_on = await (await sensorPage.Mdata_).getText()
129        expect(status_Mdata_on).equal('On')
130        console.log('turnedon data ')
131        await delay(3000);
132
133        await driver.toggleData();
134        await delay(3000);
135        let status_Mdata_off = await (await sensorPage.Mdata_).getText()
136        expect(status_Mdata_off).equal('Off')
137        console.log('turnedoff data ')
138        await driver.back()
139        
140    });
141
142    it(' location', async () => {
143
144        // await delay(2000);
145        await driver.openNotifications();
146        await delay(2000);
147        // await driver.toggleLocationServices();
148        await (await sensorPage.location_).click()
149        await delay(3000); 
150        let status_location_on = await (await sensorPage.location_).getText()
151        expect(status_location_on).equal('On')
152        console.log('turnedon data ')
153        await delay(3000);
154
155        // await driver.toggleLocationServices();
156        await (await sensorPage.location_).click()
157        await delay(3000);
158        let status_location_off = await (await sensorPage.location_).getText()
159        expect(status_location_off).equal('Off')
160        console.log('turnedoff data ')
161        await driver.back()
162        
163
164    });
165
166    
167
168
169    afterEach(() => {
170        driver.closeApp();
171    });
172
173    // after(() => {
174    //     driver.reset();
175    // });
176})
177
178
179
180
181
182
183
184
185
Full Screen

js-wdio.js

Source: js-wdio.js Github

copy
1import Framework from './framework';
2
3class JsWdIoFramework extends Framework {
4
5  get language () {
6    return 'js';
7  }
8
9  chainifyCode (code) {
10    return code
11      .replace(/let .+ = /g, '')
12      .replace(/(\n|^)(driver|el[0-9]+)\./g, '\n.')
13      .replace(/;\n/g, '\n');
14  }
15
16  wrapWithBoilerplate (code) {
17    let host = JSON.stringify(this.host);
18    let caps = JSON.stringify(this.caps);
19    let proto = JSON.stringify(this.scheme);
20    let path = JSON.stringify(this.path);
21    return `// Requires the webdriverio client library
22// (npm install webdriverio)
23// Then paste this into a .js file and run with Node:
24// node <file>.js
25
26const wdio = require('webdriverio');
27const caps = ${caps};
28const driver = wdio.remote({
29  protocol: ${proto},
30  host: ${host},
31  port: ${this.port},
32  path: ${path},
33  desiredCapabilities: caps
34});
35
36driver.init()
37${this.indent(this.chainifyCode(code), 2)}
38  .end();
39`;
40  }
41
42  codeFor_findAndAssign (strategy, locator, localVar, isArray) {
43    // wdio has its own way of indicating the strategy in the locator string
44    switch (strategy) {
45      case 'xpath': break; // xpath does not need to be updated
46      case 'accessibility id': locator = `~${locator}`; break;
47      case 'id': locator = `${locator}`; break;
48      case 'name': locator = `name=${locator}`; break;
49      case 'class name': locator = `${locator}`; break;
50      case '-android uiautomator': locator = `android=${locator}`; break;
51      case '-android datamatcher': locator = `android=${locator}`; break;
52      case '-android viewtag': locator = `android=unsupported`; break;
53      case '-ios predicate string': locator = `ios=${locator}`; break;
54      case '-ios class chain': locator = `ios=${locator}`; break; // TODO: Handle IOS class chain properly. Not all libs support it. Or take it out
55      default: throw new Error(`Can't handle strategy ${strategy}`);
56    }
57    if (isArray) {
58      return `let ${localVar} = driver.elements(${JSON.stringify(locator)});`;
59    } else {
60      return `let ${localVar} = driver.element(${JSON.stringify(locator)});`;
61    }
62  }
63
64  codeFor_click (varName, varIndex) {
65    return `${this.getVarName(varName, varIndex)}.click();`;
66  }
67
68  codeFor_clear (varName, varIndex) {
69    return `${this.getVarName(varName, varIndex)}.clearElement();`;
70  }
71
72  codeFor_sendKeys (varName, varIndex, text) {
73    return `${this.getVarName(varName, varIndex)}.setValue(${JSON.stringify(text)});`;
74  }
75
76  codeFor_back () {
77    return `driver.back();`;
78  }
79
80  codeFor_tap (varNameIgnore, varIndexIgnore, x, y) {
81    return `driver.touchAction({actions: 'tap', x: ${x}, y: ${y}})`;
82  }
83
84  codeFor_swipe (varNameIgnore, varIndexIgnore, x1, y1, x2, y2) {
85    return `driver.touchAction([
86  {action: 'press', x: ${x1}, y: ${y1}},
87  {action: 'moveTo', x: ${x2}, y: ${y2}},
88  'release'
89]);`;
90  }
91
92  codeFor_getCurrentActivity () {
93    return `let activityName = await driver.currentActivity();`;
94  }
95
96  codeFor_getCurrentPackage () {
97    return `let packageName = await driver.currentPackage();`;
98  }
99
100
101  codeFor_installAppOnDevice (varNameIgnore, varIndexIgnore, app) {
102    return `await driver.installApp('${app}');`;
103  }
104
105  codeFor_isAppInstalledOnDevice (varNameIgnore, varIndexIgnore, app) {
106    return `let isAppInstalled = await driver.isAppInstalled("${app}");`;
107  }
108
109  codeFor_launchApp () {
110    return `await driver.launch();`;
111  }
112
113  codeFor_backgroundApp (varNameIgnore, varIndexIgnore, timeout) {
114    return `await driver.background(${timeout});`;
115  }
116
117  codeFor_closeApp () {
118    return `await driver.close_app();`;
119  }
120
121  codeFor_resetApp () {
122    return `await driver.reset();`;
123  }
124
125  codeFor_removeAppFromDevice (varNameIgnore, varIndexIgnore, app) {
126    return `await driver.removeApp('${app}')`;
127  }
128
129  codeFor_getAppStrings (varNameIgnore, varIndexIgnore, language, stringFile) {
130    return `let appStrings = await driver.getAppStrings(${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''});`;
131  }
132
133  codeFor_getClipboard () {
134    return `let clipboardText = await driver.getClipboard();`;
135  }
136
137  codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {
138    return `await driver.setClipboard('${clipboardText}')`;
139  }
140
141  codeFor_pressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
142    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;
143  }
144
145  codeFor_longPressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
146    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;
147  }
148
149  codeFor_hideDeviceKeyboard () {
150    return `await driver.hideDeviceKeyboard();`;
151  }
152
153  codeFor_isKeyboardShown () {
154    return `//isKeyboardShown not supported`;
155  }
156
157  codeFor_pushFileToDevice (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {
158    return `await driver.pushFile('${pathToInstallTo}', '${fileContentString}');`;
159  }
160
161  codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {
162    return `let data = await driver.pullFile('${pathToPullFrom}');`;
163  }
164
165  codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {
166    return `let data = await driver.pullFolder('${folderToPullFrom}');`;
167  }
168
169  codeFor_toggleAirplaneMode () {
170    return `await driver.toggleAirplaneMode();`;
171  }
172
173  codeFor_toggleData () {
174    return `await driver.toggleData();`;
175  }
176
177  codeFor_toggleWiFi () {
178    return `await driver.toggleWiFi();`;
179  }
180
181  codeFor_toggleLocationServices () {
182    return `await driver.toggleLocationServices();`;
183  }
184
185  codeFor_sendSMS () {
186    return `// Not supported: sendSms;`;
187  }
188
189  codeFor_gsmCall () {
190    return `// Not supported: gsmCall`;
191  }
192
193  codeFor_gsmSignal () {
194    return `// Not supported: gsmSignal`;
195  }
196
197  codeFor_gsmVoice () {
198    return `// Not supported: gsmVoice`;
199  }
200
201  codeFor_shake () {
202    return `await driver.shake();`;
203  }
204
205  codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {
206    return `await driver.lock(${seconds});`;
207  }
208
209  codeFor_unlock () {
210    return `await driver.unlock();`;
211  }
212
213  codeFor_isLocked () {
214    return `let isLocked = await driver.isLocked();`;
215  }
216
217  codeFor_rotateDevice (varNameIgnore, varIndexIgnore, x, y, radius, rotation, touchCount, duration) {
218    return `driver.rotate(${x}, ${y}, ${radius}, ${rotation}, ${touchCount}, ${duration});`;
219  }
220
221  codeFor_getPerformanceData () {
222    return `// Not supported: getPerformanceData`;
223  }
224
225  codeFor_getSupportedPerformanceDataTypes () {
226    return `// Not supported: getSupportedPerformanceDataTypes`;
227  }
228
229  codeFor_performTouchId (varNameIgnore, varIndexIgnore, match) {
230    return `await driver.touchId(${match});`;
231  }
232
233  codeFor_toggleTouchIdEnrollment (varNameIgnore, varIndexIgnore, enroll) {
234    return `await driver.toggleTouchIdEnrollment(${enroll});`;
235  }
236
237  codeFor_openNotifications () {
238    return `await driver.openNotifications();`;
239  }
240
241  codeFor_getDeviceTime () {
242    return `let time = await driver.getDeviceTime();`;
243  }
244
245  codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {
246    return `await driver.fingerprint(${fingerprintId});`;
247  }
248
249  codeFor_sessionCapabilities () {
250    return `let caps = await driver.session('c8db88a0-47a6-47a1-802d-164d746c06aa');`;
251  }
252
253  codeFor_setPageLoadTimeout (varNameIgnore, varIndexIgnore, ms) {
254    return `await driver.timeouts('page load', ${ms})`;
255  }
256
257  codeFor_setAsyncScriptTimeout (varNameIgnore, varIndexIgnore, ms) {
258    return `await driver.timeouts('script', ${ms})`;
259  }
260
261  codeFor_setImplicitWaitTimeout (varNameIgnore, varIndexIgnore, ms) {
262    return `await driver.timeouts('implicit', ${ms})`;
263  }
264
265  codeFor_setCommandTimeout (varNameIgnore, varIndexIgnore, ms) {
266    return `await driver.timeouts('command', ${ms})`;
267  }
268
269  codeFor_getOrientation () {
270    return `let orientation = await driver.orientation();`;
271  }
272
273  codeFor_setOrientation (varNameIgnore, varIndexIgnore, orientation) {
274    return `driver.orientation("${orientation}");`;
275  }
276
277  codeFor_getGeoLocation () {
278    return `let location = await driver.location();`;
279  }
280
281  codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {
282    return `await driver.location({latitude: ${latitude}, longitude: ${longitude}, altitude: ${altitude}});`;
283  }
284
285  codeFor_logTypes () {
286    return `let logTypes = await driver.log();`;
287  }
288
289  codeFor_log (varNameIgnore, varIndexIgnore, logType) {
290    return `let logs = await driver.log('${logType}');`;
291  }
292
293  codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {
294    return `await driver.settings(${settingsJson});`;
295  }
296
297  codeFor_settings () {
298    return `let settings = await driver.settings();`;
299  }
300}
301
302JsWdIoFramework.readableName = 'JS - Webdriver.io';
303
304export default JsWdIoFramework;
305
Full Screen

js-oxygen.js

Source: js-oxygen.js Github

copy
1import Framework from './framework';
2
3class JsOxygenFramework extends Framework {
4
5  get language () {
6    return 'js';
7  }
8
9  wrapWithBoilerplate (code) {
10    let caps = JSON.stringify(this.caps);
11    let url = JSON.stringify(`${this.scheme}://${this.host}:${this.port}${this.path}`);
12    return `// Requires the Oxygen HQ client library
13// (npm install oxygen-cli -g)
14// Then paste this into a .js file and run with:
15// oxygen <file>.js
16
17mob.init(${caps}, ${url});
18
19${code}
20
21`;
22  }
23
24  codeFor_findAndAssign (strategy, locator, localVar, isArray) {
25    // wdio has its own way of indicating the strategy in the locator string
26    switch (strategy) {
27      case 'xpath': break; // xpath does not need to be updated
28      case 'accessibility id': locator = `~${locator}`; break;
29      case 'id': locator = `id=${locator}`; break;
30      case 'name': locator = `name=${locator}`; break;
31      case 'class name': locator = `css=${locator}`; break;
32      case '-android uiautomator': locator = `android=${locator}`; break;
33      case '-android datamatcher': locator = `android=${locator}`; break;
34      case '-ios predicate string': locator = `ios=${locator}`; break;
35      case '-ios class chain': locator = `ios=${locator}`; break; // TODO: Handle IOS class chain properly. Not all libs support it. Or take it out
36      default: throw new Error(`Can't handle strategy ${strategy}`);
37    }
38    if (isArray) {
39      return `let ${localVar} = mob.findElements(${JSON.stringify(locator)});`;
40    } else {
41      return `let ${localVar} = mob.findElement(${JSON.stringify(locator)});`;
42    }
43  }
44
45  codeFor_click (varName, varIndex) {
46    return `mob.click(${this.getVarName(varName, varIndex)});`;
47  }
48
49  codeFor_clear (varName, varIndex) {
50    return `mob.clear(${this.getVarName(varName, varIndex)});`;
51  }
52
53  codeFor_sendKeys (varName, varIndex, text) {
54    return `mob.type(${this.getVarName(varName, varIndex)}, ${JSON.stringify(text)});`;
55  }
56
57  codeFor_back () {
58    return `mob.back();`;
59  }
60
61  codeFor_tap (varNameIgnore, varIndexIgnore, x, y) {
62    return `mob.tap(${x}, ${y});`;
63  }
64
65  codeFor_swipe (varNameIgnore, varIndexIgnore, x1, y1, x2, y2) {
66    return `mob.swipeScreen(${x1}, ${y1}, ${x2}, ${y2});`;
67  }
68
69  codeFor_getCurrentActivity () {
70    return `let activityName = mob.getCurrentActivity();`;
71  }
72
73  codeFor_getCurrentPackage () {
74    return `let packageName = mob.getCurrentPackage();`;
75  }
76
77  codeFor_installAppOnDevice (varNameIgnore, varIndexIgnore, app) {
78    return `mob.installApp('${app}');`;
79  }
80
81  codeFor_isAppInstalledOnDevice (varNameIgnore, varIndexIgnore, app) {
82    return `let isAppInstalled = mob.isAppInstalled("${app}");`;
83  }
84
85  codeFor_launchApp () {
86    return `mob.launchApp();`;
87  }
88
89  codeFor_backgroundApp (varNameIgnore, varIndexIgnore, timeout) {
90    return `mob.driver().background(${timeout});`;
91  }
92
93  codeFor_closeApp () {
94    return `mob.closeApp();`;
95  }
96
97  codeFor_resetApp () {
98    return `mob.resetApp();`;
99  }
100
101  codeFor_removeAppFromDevice (varNameIgnore, varIndexIgnore, app) {
102    return `mob.removeApp('${app}')`;
103  }
104
105  codeFor_getAppStrings (varNameIgnore, varIndexIgnore, language, stringFile) {
106    return `let appStrings = mob.driver().getAppStrings(${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''});`;
107  }
108
109  codeFor_getClipboard () {
110    return `let clipboardText = mob.driver().getClipboard();`;
111  }
112
113  codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {
114    return `mob.driver().setClipboard('${clipboardText}')`;
115  }
116
117  codeFor_pressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
118    return `mob.driver().longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;
119  }
120
121  codeFor_longPressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
122    return `mob.driver().longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;
123  }
124
125  codeFor_hideDeviceKeyboard () {
126    return `mob.driver().hideKeyboard();`;
127  }
128
129  codeFor_isKeyboardShown () {
130    return `//isKeyboardShown not supported`;
131  }
132
133  codeFor_pushFileToDevice (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {
134    return `mob.driver().pushFile('${pathToInstallTo}', '${fileContentString}');`;
135  }
136
137  codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {
138    return `let data = mob.driver().pullFile('${pathToPullFrom}');`;
139  }
140
141  codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {
142    return `let data = mob.driver().pullFolder('${folderToPullFrom}');`;
143  }
144
145  codeFor_toggleAirplaneMode () {
146    return `mob.driver().toggleAirplaneMode();`;
147  }
148
149  codeFor_toggleData () {
150    return `mob.driver().toggleData();`;
151  }
152
153  codeFor_toggleWiFi () {
154    return `mob.driver().toggleWiFi();`;
155  }
156
157  codeFor_toggleLocationServices () {
158    return `mob.driver().toggleLocationServices();`;
159  }
160
161  codeFor_sendSMS () {
162    return `// Not supported: sendSms;`;
163  }
164
165  codeFor_gsmCall () {
166    return `// Not supported: gsmCall`;
167  }
168
169  codeFor_gsmSignal () {
170    return `// Not supported: gsmSignal`;
171  }
172
173  codeFor_gsmVoice () {
174    return `// Not supported: gsmVoice`;
175  }
176
177  codeFor_shake () {
178    return `mob.shake();`;
179  }
180
181  codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {
182    return `mob.driver().lock(${seconds});`;
183  }
184
185  codeFor_unlock () {
186    return `mob.driver().unlock();`;
187  }
188
189  codeFor_isLocked () {
190    return `let isLocked = mob.driver().isLocked();`;
191  }
192
193  codeFor_rotateDevice (varNameIgnore, varIndexIgnore, x, y, radius, rotation, touchCount, duration) {
194    return `mob.driver().rotateDevice(${x}, ${y}, ${radius}, ${rotation}, ${touchCount}, ${duration});`;
195  }
196
197  codeFor_getPerformanceData () {
198    return `// Not supported: getPerformanceData`;
199  }
200
201  codeFor_getSupportedPerformanceDataTypes () {
202    return `// Not supported: getSupportedPerformanceDataTypes`;
203  }
204
205  codeFor_performTouchId (varNameIgnore, varIndexIgnore, match) {
206    return `mob.driver().touchId(${match});`;
207  }
208
209  codeFor_toggleTouchIdEnrollment (varNameIgnore, varIndexIgnore, enroll) {
210    return `mob.driver().toggleEnrollTouchId(${enroll});`;
211  }
212
213  codeFor_openNotifications () {
214    return `mob.driver().openNotifications();`;
215  }
216
217  codeFor_getDeviceTime () {
218    return `let time = mob.getDeviceTime();`;
219  }
220
221  codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {
222    return `mob.driver().fingerPrint(${fingerprintId});`;
223  }
224
225  codeFor_sessionCapabilities () {
226    return `let caps = mob.driver().capabilities;`;
227  }
228
229  codeFor_setPageLoadTimeout (varNameIgnore, varIndexIgnore, ms) {
230    return `mob.driver().setTimeout({'pageLoad': ${ms}});`;
231  }
232
233  codeFor_setAsyncScriptTimeout (varNameIgnore, varIndexIgnore, ms) {
234    return `mob.driver().setTimeout({'script': ${ms}});`;
235  }
236
237  codeFor_setImplicitWaitTimeout (varNameIgnore, varIndexIgnore, ms) {
238    return `mob.driver().setTimeout({'implicit': ${ms}});`;
239  }
240
241  codeFor_setCommandTimeout () {
242    return `// Not supported: setCommandTimeout`;
243  }
244
245  codeFor_getOrientation () {
246    return `let orientation = mob.driver().getOrientation();`;
247  }
248
249  codeFor_setOrientation (varNameIgnore, varIndexIgnore, orientation) {
250    return `mob.driver().setOrientation("${orientation}");`;
251  }
252
253  codeFor_getGeoLocation () {
254    return `let location = mob.driver().getGeoLocation();`;
255  }
256
257  codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {
258    return `mob.driver().setGeoLocation({latitude: ${latitude}, longitude: ${longitude}, altitude: ${altitude}});`;
259  }
260
261  codeFor_logTypes () {
262    return `let logTypes = mob.driver().getLogTypes();`;
263  }
264
265  codeFor_log (varNameIgnore, varIndexIgnore, logType) {
266    return `let logs = mob.driver().getLogs('${logType}');`;
267  }
268
269  codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {
270    return `mob.driver().updateSettings(${settingsJson});`;
271  }
272
273  codeFor_settings () {
274    return `let settings = mob.driver().getSettings();`;
275  }
276}
277
278JsOxygenFramework.readableName = 'JS - Oxygen HQ';
279
280export default JsOxygenFramework;
281
Full Screen

js-wd.js

Source: js-wd.js Github

copy
1import Framework from './framework';
2
3class JsWdFramework extends Framework {
4
5  get language () {
6    return 'js';
7  }
8
9  wrapWithBoilerplate (code) {
10    let caps = JSON.stringify(this.caps);
11    return `// Requires the admc/wd client library
12// (npm install wd)
13// Then paste this into a .js file and run with Node 7.6+
14
15const wd = require('wd');
16const driver = wd.promiseChainRemote("${this.serverUrl}");
17const caps = ${caps};
18
19async function main () {
20  await driver.init(caps);
21${this.indent(code, 2)}
22  await driver.quit();
23}
24
25main().catch(console.log);
26`;
27  }
28
29  codeFor_findAndAssign (strategy, locator, localVar, isArray) {
30    let suffixMap = {
31      xpath: 'XPath',
32      'accessibility id': 'AccessibilityId',
33      'id': 'Id',
34      'name': 'Name',
35      'class name': 'ClassName',
36      '-android uiautomator': 'AndroidUIAutomator',
37      '-android datamatcher': 'AndroidDataMatcher',
38      '-android viewtag': 'unsupported',
39      '-ios predicate string': 'IosUIAutomation',
40      '-ios class chain': 'IosClassChain',
41    };
42    if (!suffixMap[strategy]) {
43      throw new Error(`Strategy ${strategy} can't be code-gened`);
44    }
45    if (isArray) {
46      return `let ${localVar} = await driver.elementsBy${suffixMap[strategy]}(${JSON.stringify(locator)});`;
47    } else {
48      return `let ${localVar} = await driver.elementBy${suffixMap[strategy]}(${JSON.stringify(locator)});`;
49    }
50  }
51
52  codeFor_click (varName, varIndex) {
53    return `await ${this.getVarName(varName, varIndex)}.click();`;
54  }
55
56  codeFor_clear (varName, varIndex) {
57    return `await ${this.getVarName(varName, varIndex)}.clear();`;
58  }
59
60  codeFor_sendKeys (varName, varIndex, text) {
61    return `await ${this.getVarName(varName, varIndex)}.sendKeys(${JSON.stringify(text)});`;
62  }
63
64  codeFor_back () {
65    return `await driver.back();`;
66  }
67
68  codeFor_tap (varNameIgnore, varIndexIgnore, x, y) {
69    return `await (new wd.TouchAction(driver))
70  .tap({x: ${x}, y: ${y}})
71  .perform()
72    `;
73  }
74
75  codeFor_swipe (varNameIgnore, varIndexIgnore, x1, y1, x2, y2) {
76    return `await (new wd.TouchAction(driver))
77  .press({x: ${x1}, y: ${y1}})
78  .moveTo({x: ${x2}, y: ${y2}})
79  .release()
80  .perform()
81    `;
82  }
83
84  codeFor_getCurrentActivity () {
85    return `let activityName = await driver.getCurrentActivity()`;
86  }
87
88  codeFor_getCurrentPackage () {
89    return `let packageName = await driver.getCurrentPackage()`;
90  }
91
92
93  codeFor_installAppOnDevice (varNameIgnore, varIndexIgnore, app) {
94    return `let isAppInstalled = await driver.installAppOnDevice('${app}');`;
95  }
96
97  codeFor_isAppInstalledOnDevice (varNameIgnore, varIndexIgnore, app) {
98    return `driver.isAppInstalled("${app}");`;
99  }
100
101  codeFor_launchApp () {
102    return `await driver.launchApp();`;
103  }
104
105  codeFor_backgroundApp (varNameIgnore, varIndexIgnore, timeout) {
106    return `await driver.backgroundApp(${timeout});`;
107  }
108
109  codeFor_closeApp () {
110    return `await driver.closeApp();`;
111  }
112
113  codeFor_resetApp () {
114    return `await driver.resetApp();`;
115  }
116
117  codeFor_removeAppFromDevice (varNameIgnore, varIndexIgnore, app) {
118    return `await driver.removeAppFromDevice('${app}');`;
119  }
120
121  codeFor_getAppStrings (varNameIgnore, varIndexIgnore, language, stringFile) {
122    return `let appStrings = await driver.getAppStrings(${language ? `${language}, ` : ''}${stringFile ? `"${stringFile}` : ''});`;
123  }
124
125  codeFor_getClipboard () {
126    return `let clipboardText = await driver.getClipboard();`;
127  }
128
129  codeFor_setClipboard (varNameIgnore, varIndexIgnore, clipboardText) {
130    return `await driver.setClipboard('${clipboardText}')`;
131  }
132
133  codeFor_pressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
134    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;
135  }
136
137  codeFor_longPressKeycode (varNameIgnore, varIndexIgnore, keyCode, metaState, flags) {
138    return `await driver.longPressKeyCode(${keyCode}, ${metaState}, ${flags});`;
139  }
140
141  codeFor_hideDeviceKeyboard () {
142    return `await driver.hideDeviceKeyboard();`;
143  }
144
145  codeFor_isKeyboardShown () {
146    return `await driver.isKeyboardShown();`;
147  }
148
149  codeFor_pushFileToDevice (varNameIgnore, varIndexIgnore, pathToInstallTo, fileContentString) {
150    return `await driver.pushFileToDevice('${pathToInstallTo}', '${fileContentString}');`;
151  }
152
153  codeFor_pullFile (varNameIgnore, varIndexIgnore, pathToPullFrom) {
154    return `let fileBase64 = await driver.pullFile('${pathToPullFrom}');`;
155  }
156
157  codeFor_pullFolder (varNameIgnore, varIndexIgnore, folderToPullFrom) {
158    return `let fileBase64 = await driver.pullFolder('${folderToPullFrom}');`;
159  }
160
161  codeFor_toggleAirplaneMode () {
162    return `await driver.toggleAirplaneMode();`;
163  }
164
165  codeFor_toggleData () {
166    return `await driver.toggleData();`;
167  }
168
169  codeFor_toggleWiFi () {
170    return `await driver.toggleWiFi();`;
171  }
172
173  codeFor_toggleLocationServices () {
174    return `await driver.toggleLocationServices();`;
175  }
176
177  codeFor_sendSMS (varNameIgnore, varIndexIgnore, phoneNumber, text) {
178    return `await driver.sendSms('${phoneNumber}', '${text}');`;
179  }
180
181  codeFor_gsmCall (varNameIgnore, varIndexIgnore, phoneNumber, action) {
182    return `await driver.gsmCall('${phoneNumber}', '${action}');`;
183  }
184
185  codeFor_gsmSignal (varNameIgnore, varIndexIgnore, signalStrength) {
186    return `await driver.gsmSignal(${signalStrength});`;
187  }
188
189  codeFor_gsmVoice (varNameIgnore, varIndexIgnore, state) {
190    return `await driver.gsmVoice('${state}');`;
191  }
192
193  codeFor_shake () {
194    return `await driver.shake();`;
195  }
196
197  codeFor_lock (varNameIgnore, varIndexIgnore, seconds) {
198    return `await driver.lock(${seconds})`;
199  }
200
201  codeFor_unlock () {
202    return `await driver.unlock()`;
203  }
204
205  codeFor_isLocked () {
206    return `let isLocked = await driver.isLocked();`;
207  }
208
209  codeFor_rotateDevice (varNameIgnore, varIndexIgnore, x, y, radius, rotation, touchCount, duration) {
210    return `driver.rotateDevice({x: ${x}, y: ${y}, duration: ${duration}, radius: ${radius}, rotation: ${rotation}, touchCount: ${touchCount}});`;
211  }
212
213  codeFor_getPerformanceData (varNameIgnore, varIndexIgnore, packageName, dataType, dataReadTimeout) {
214    return `let performanceData = await driver.getPerformanceData('${packageName}', '${dataType}', ${dataReadTimeout});`;
215  }
216
217  codeFor_getSupportedPerformanceDataTypes () {
218    return `let supportedPerformanceDataTypes = await driver.getSupportedPerformanceDataTypes();`;
219  }
220
221  codeFor_performTouchId (varNameIgnore, varIndexIgnore, match) {
222    return `await driver.touchId(${match});`;
223  }
224
225  codeFor_toggleTouchIdEnrollment (varNameIgnore, varIndexIgnore, enroll) {
226    return `await driver.toggleTouchIdEnrollment(${enroll});`;
227  }
228
229  codeFor_openNotifications () {
230    return `await driver.openNotifications();`;
231  }
232
233  codeFor_getDeviceTime () {
234    return `let time = await driver.getDeviceTime();`;
235  }
236
237  codeFor_fingerprint (varNameIgnore, varIndexIgnore, fingerprintId) {
238    return `await driver.fingerprint(${fingerprintId});`;
239  }
240
241  codeFor_sessionCapabilities () {
242    return `let caps = await driver.sessionCapabilities();`;
243  }
244
245  codeFor_setPageLoadTimeout (varNameIgnore, varIndexIgnore, ms) {
246    return `await setPageLoadTimeout(${ms})`;
247  }
248
249  codeFor_setAsyncScriptTimeout (varNameIgnore, varIndexIgnore, ms) {
250    return `await setAsyncScriptTimeout(${ms})`;
251  }
252
253  codeFor_setImplicitWaitTimeout (varNameIgnore, varIndexIgnore, ms) {
254    return `await setImplicitWaitTimeout(${ms})`;
255  }
256
257  codeFor_getOrientation () {
258    return `let orientation = await driver.getOrientation();`;
259  }
260
261  codeFor_setOrientation (varNameIgnore, varIndexIgnore, orientation) {
262    return `await driver.setOrientation('${orientation}');`;
263  }
264
265  codeFor_getGeoLocation () {
266    return `let location = await driver.getGeoLocation();`;
267  }
268
269  codeFor_setGeoLocation (varNameIgnore, varIndexIgnore, latitude, longitude, altitude) {
270    return `await driver.setGeoLocation(${latitude}, ${longitude}, ${altitude});`;
271  }
272
273  codeFor_logTypes () {
274    return `let logTypes = await driver.logTypes();`;
275  }
276
277  codeFor_log (varNameIgnore, varIndexIgnore, logType) {
278    return `let logs = await driver.log('${logType}');`;
279  }
280
281  codeFor_updateSettings (varNameIgnore, varIndexIgnore, settingsJson) {
282    return `await driver.updateSettings(${settingsJson});`;
283  }
284
285  codeFor_settings () {
286    return `let settings = await driver.settings();`;
287  }
288
289}
290
291JsWdFramework.readableName = 'JS - WD (Promise)';
292
293export default JsWdFramework;
294
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 Android 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)