Skip to content

Commit 5e07686

Browse files
committed
fix(appium): replace deprecated setNetworkConnection/getNetworkConnection with mobile: setConnectivity/getConnectivity
WebdriverIO 9.27.2+ emits deprecation warnings for the legacy Android network connection protocol commands. Appium's recommended replacement is the mobile: setConnectivity / mobile: getConnectivity execute commands. grabNetworkConnection() keeps its original return shape (value/inAirplaneMode/hasWifi/hasData) by re-encoding the new {wifi, data, airplaneMode} response into the legacy bitmask, so existing test code isn't broken. Fixes #5619
1 parent 5b2517c commit 5e07686

2 files changed

Lines changed: 94 additions & 6 deletions

File tree

lib/helper/Appium.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -832,12 +832,12 @@ class Appium extends Webdriver {
832832
*/
833833
async grabNetworkConnection() {
834834
onlyForApps.call(this, supportedPlatform.android)
835-
const res = await this.browser.getNetworkConnection()
835+
const res = await this.browser.execute('mobile: getConnectivity')
836836
return {
837-
value: res,
838-
inAirplaneMode: res.inAirplaneMode,
839-
hasWifi: res.hasWifi,
840-
hasData: res.hasData,
837+
value: (res.airplaneMode ? 1 : 0) | (res.wifi ? 2 : 0) | (res.data ? 4 : 0),
838+
inAirplaneMode: res.airplaneMode,
839+
hasWifi: res.wifi,
840+
hasData: res.data,
841841
}
842842
}
843843

@@ -978,7 +978,11 @@ class Appium extends Webdriver {
978978
*/
979979
async setNetworkConnection(value) {
980980
onlyForApps.call(this, supportedPlatform.android)
981-
return this.browser.setNetworkConnection(value)
981+
return this.browser.execute('mobile: setConnectivity', {
982+
airplaneMode: !!(value & 1),
983+
wifi: !!(value & 2),
984+
data: !!(value & 4),
985+
})
982986
}
983987

984988
/**
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { expect } from 'chai'
2+
import sinon from 'sinon'
3+
import Appium from '../../../lib/helper/Appium.js'
4+
5+
function createApp() {
6+
const app = new Appium({
7+
platform: 'Android',
8+
desiredCapabilities: {
9+
platformName: 'Android',
10+
},
11+
})
12+
app.browser = { execute: sinon.stub() }
13+
return app
14+
}
15+
16+
describe('Appium #setNetworkConnection, #grabNetworkConnection', () => {
17+
it('should call mobile: setConnectivity with airplane mode only for value 1', async () => {
18+
const app = createApp()
19+
await app.setNetworkConnection(1)
20+
expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: true, wifi: false, data: false })).to.be.true
21+
})
22+
23+
it('should call mobile: setConnectivity with wifi only for value 2', async () => {
24+
const app = createApp()
25+
await app.setNetworkConnection(2)
26+
expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: true, data: false })).to.be.true
27+
})
28+
29+
it('should call mobile: setConnectivity with data only for value 4', async () => {
30+
const app = createApp()
31+
await app.setNetworkConnection(4)
32+
expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: false, data: true })).to.be.true
33+
})
34+
35+
it('should call mobile: setConnectivity with wifi and data for value 6', async () => {
36+
const app = createApp()
37+
await app.setNetworkConnection(6)
38+
expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: true, data: true })).to.be.true
39+
})
40+
41+
it('should call mobile: setConnectivity with everything off for value 0', async () => {
42+
const app = createApp()
43+
await app.setNetworkConnection(0)
44+
expect(app.browser.execute.calledWith('mobile: setConnectivity', { airplaneMode: false, wifi: false, data: false })).to.be.true
45+
})
46+
47+
it('should grab network connection using mobile: getConnectivity and map to legacy bitmask shape', async () => {
48+
const app = createApp()
49+
app.browser.execute.resolves({ airplaneMode: false, wifi: false, data: true })
50+
const val = await app.grabNetworkConnection()
51+
expect(app.browser.execute.calledWith('mobile: getConnectivity')).to.be.true
52+
expect(val.value).to.equal(4)
53+
expect(val.inAirplaneMode).to.equal(false)
54+
expect(val.hasWifi).to.equal(false)
55+
expect(val.hasData).to.equal(true)
56+
})
57+
58+
it('should grab network connection with wifi and airplane mode combined', async () => {
59+
const app = createApp()
60+
app.browser.execute.resolves({ airplaneMode: true, wifi: true, data: false })
61+
const val = await app.grabNetworkConnection()
62+
expect(val.value).to.equal(3)
63+
expect(val.inAirplaneMode).to.equal(true)
64+
expect(val.hasWifi).to.equal(true)
65+
expect(val.hasData).to.equal(false)
66+
})
67+
68+
it('should throw when used outside android platform', async () => {
69+
const app = new Appium({
70+
platform: 'iOS',
71+
desiredCapabilities: {
72+
platformName: 'iOS',
73+
},
74+
})
75+
app.browser = { execute: sinon.stub() }
76+
let error
77+
try {
78+
await app.setNetworkConnection(1)
79+
} catch (err) {
80+
error = err
81+
}
82+
expect(error).to.be.instanceOf(Error)
83+
})
84+
})

0 commit comments

Comments
 (0)