io.appium.settings
Version:
App for dealing with Android settings
56 lines • 2.13 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.setWifiState = setWifiState;
exports.setDataState = setDataState;
const constants_1 = require("../constants");
/**
* Change the state of WiFi on the device under test.
*
* @param on - True to enable and false to disable it
* @param isEmulator - Set it to true if the device under test is an emulator rather than a real device
*/
async function setWifiState(on, isEmulator = false) {
if (isEmulator) {
// The svc command does not require to be root since API 26
await this.adb.shell(['svc', 'wifi', on ? 'enable' : 'disable']);
return;
}
if ((await this.adb.getApiLevel()) < 30) {
// Android below API 30 does not have a dedicated adb command
// to manipulate wifi connection state, so try to do it via Settings app
// as a workaround
await this.checkBroadcast([
'-a',
constants_1.WIFI_CONNECTION_SETTING_ACTION,
'-n',
constants_1.WIFI_CONNECTION_SETTING_RECEIVER,
'--es',
'setstatus',
on ? 'enable' : 'disable',
], `${on ? 'enable' : 'disable'} WiFi`);
return;
}
await this.adb.shell(['cmd', '-w', 'wifi', 'set-wifi-enabled', on ? 'enabled' : 'disabled']);
}
/**
* Change the state of Data transfer on the device under test.
*
* @param on - True to enable and false to disable it
* @param isEmulator - Set it to true if the device under test is an emulator rather than a real device
* @throws {Error} If the data state cannot be changed
*/
async function setDataState(on, isEmulator = false) {
if (isEmulator) {
// The svc command does not require to be root since API 26
await this.adb.shell(['svc', 'data', on ? 'enable' : 'disable']);
}
else {
try {
await this.adb.shell(['cmd', 'phone', 'data', on ? 'enable' : 'disable']);
}
catch (e) {
throw new Error(`Cannot change the data state. Original error: ${e.stderr || e.message}`);
}
}
}
//# sourceMappingURL=network.js.map