Skip to content

Commit

Permalink
Down with indexOf (#830)
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie authored Nov 19, 2018
1 parent 9a3001f commit 30d8cef
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/commands/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ commands.getAttribute = async function (attribute, el) {
// otherwise let WDA handle attribute requests
let value = await this.proxyCommand(`/element/${el}/attribute/${attribute}`, 'GET');
// Transform the result for the case when WDA returns an integer representation for a boolean value
if ([0, 1].indexOf(value) !== -1) {
if ([0, 1].includes(value)) {
value = !!value;
}
// The returned value must be of type string according to https://www.w3.org/TR/webdriver/#get-element-attribute
Expand Down
8 changes: 4 additions & 4 deletions lib/commands/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ helpers.findNativeElementOrElements = async function (strategy, selector, mult,
function stripViewFromSelector (selector) {
// Don't strip it out if it's one of these 4 element types
// (see https://github.com/facebook/WebDriverAgent/blob/master/WebDriverAgentLib/Utilities/FBElementTypeTransformer.m for reference)
let keepView = [
const keepView = [
'XCUIElementTypeScrollView',
'XCUIElementTypeCollectionView',
'XCUIElementTypeTextView',
'XCUIElementTypeWebView',
].indexOf(selector) >= 0;
].includes(selector);

if (!keepView && selector.indexOf('View') === selector.length - 4) {
return selector.substr(0, selector.length - 4);
Expand All @@ -55,11 +55,11 @@ helpers.findNativeElementOrElements = async function (strategy, selector, mult,
if (strategy === 'class name') {
// XCUITest classes have `XCUIElementType` prepended
// first check if there is the old `UIA` prefix
if (selector.indexOf('UIA') === 0) {
if (selector.startsWith('UIA')) {
selector = selector.substring(3);
}
// now check if we need to add `XCUIElementType`
if (selector.indexOf('XCUIElementType') !== 0) {
if (!selector.startsWith('XCUIElementType')) {
selector = stripViewFromSelector(`XCUIElementType${selector}`);
rewroteSelector = true;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ commands.getWindowRect = async function () {
};

commands.hideKeyboard = async function (strategy, ...possibleKeys) {
if ((this.opts.deviceName || '').indexOf('iPhone') === -1) {
if (!(this.opts.deviceName || '').includes('iPhone')) {
// TODO: once WDA can handle dismissing keyboard for iphone, take away conditional
try {
await this.proxyCommand('/wda/keyboard/dismiss', 'POST');
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/gesture.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ helpers.mobileScroll = async function (opts = {}, swipe = false) {
if (opts.name && !swipe) {
params.name = opts.name;
} else if (opts.direction) {
if (['up', 'down', 'left', 'right'].indexOf(opts.direction.toLowerCase()) < 0) {
if (!['up', 'down', 'left', 'right'].includes(opts.direction.toLowerCase())) {
let msg = 'Direction must be up, down, left or right';
log.errorAndThrow(msg);
}
Expand Down Expand Up @@ -242,8 +242,8 @@ helpers.mobileSelectPickerWheelValue = async function (opts = {}) {
if (!opts.element) {
log.errorAndThrow('Element id is expected to be set for selectPickerWheelValue method');
}
if (!_.isString(opts.order) || ['next', 'previous'].indexOf(opts.order.toLowerCase()) === -1) {
log.errorAndThrow(`The mandatory "order" parameter is expected to be equal either to 'next' or 'previous'. ` +
if (!_.isString(opts.order) || !['next', 'previous'].includes(opts.order.toLowerCase())) {
log.errorAndThrow(`The mandatory 'order' parameter is expected to be equal either to 'next' or 'previous'. ` +
`'${opts.order}' is given instead`);
}
const el = opts.element.ELEMENT || opts.element;
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/proxy-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ helpers.proxyCommand = async function (endpoint, method, body, isSessionCommand

if (!endpoint) {
log.errorAndThrow('Proxying requires an endpoint');
} else if (SUPPORTED_METHODS.indexOf(method) === -1) {
} else if (!SUPPORTED_METHODS.includes(method)) {
log.errorAndThrow(`Proxying only works for the following requests: ${SUPPORTED_METHODS.join(', ')}`);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ class XCUITestDriver extends BaseDriver {
// make sure it is a connected device. If not, the udid passed in is invalid
const devices = await getConnectedDevices();
log.debug(`Available devices: ${devices.join(', ')}`);
if (devices.indexOf(this.opts.udid) === -1) {
if (!devices.includes(this.opts.udid)) {
// check for a particular simulator
if (await simExists(this.opts.udid)) {
const device = await getSimulator(this.opts.udid);
Expand Down
2 changes: 1 addition & 1 deletion lib/ios-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class IOSDeploy {
let isInstalled = [`--exists`, `--id`, this.udid, `--bundle_id`, bundleid];
try {
let {stdout} = await exec(this.cmd, isInstalled);
return (stdout && (stdout.indexOf("true") > -1));
return (stdout && (stdout.includes('true')));
} catch (err) {
// error 255 is just ios-deploy's way of saying it is not installed
if (err.code !== 255) {
Expand Down
4 changes: 2 additions & 2 deletions lib/real-device-management.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ async function getConnectedDevices () {
let {stdout} = await exec('idevice_id', ['-l']);
return stdout.trim().split('\n');
} catch (err) {
if (err.message.indexOf(`Command 'idevice_id' not found`) === -1) {
if (!err.message.includes(`Command 'idevice_id' not found`)) {
throw err;
}
let msg = `The 'idevice_id' program is not installed. If you are running ` +
const msg = `The 'idevice_id' program is not installed. If you are running ` +
`a real device test it is necessary. Install with 'brew install ` +
`libimobiledevice --HEAD'`;
log.warn(msg);
Expand Down
11 changes: 5 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,17 @@ async function checkAppPresent (app) {
}

async function getDriverInfo () {
let stat = await fs.stat(path.resolve(__dirname, '..'));
let built = stat.mtime.getTime();
const stat = await fs.stat(path.resolve(__dirname, '..'));
const built = stat.mtime.getTime();

// get the package.json and the version from it
let pkg = require(__filename.indexOf('build/lib/utils') !== -1 ? '../../package.json' : '../package.json');
let version = pkg.version;
const pkg = require(__filename.includes('build/lib/utils') ? '../../package.json' : '../package.json');
const version = pkg.version;

let info = {
return {
built,
version,
};
return info;
}

function normalizeCommandTimeouts (value) {
Expand Down
2 changes: 1 addition & 1 deletion lib/wda/iproxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class iProxy {
continue;
}

if (line.indexOf('Resource temporarily unavailable') !== -1) {
if (line.includes('Resource temporarily unavailable')) {
// this generally happens when WDA does not respond,
// so print a more useful message
log.debug('Connection to WDA timed out');
Expand Down
4 changes: 2 additions & 2 deletions lib/wda/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,15 @@ async function killProcess (name, proc) {
try {
await proc.stop('SIGTERM', 1000);
} catch (err) {
if (err.message.indexOf(`Process didn't end after`) === -1) {
if (!err.message.includes(`Process didn't end after`)) {
throw err;
}
log.debug(`${name} process did not end in a timely fashion: '${err.message}'. ` +
`Sending 'SIGKILL'...`);
try {
await proc.stop('SIGKILL');
} catch (err) {
if (err.message.indexOf('not currently running') !== -1) {
if (err.message.includes('not currently running')) {
// the process ended but for some reason we were not informed
return;
}
Expand Down
14 changes: 7 additions & 7 deletions lib/wda/xcodebuild.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,19 +239,19 @@ class XcodeBuild {
let out = stdout || stderr;
// we want to pull out the log file that is created, and highlight it
// for diagnostic purposes
if (out.indexOf('Writing diagnostic log for test session to') !== -1) {
if (out.includes('Writing diagnostic log for test session to')) {
// pull out the first line that begins with the path separator
// which *should* be the line indicating the log file generated
xcodebuild.logLocation = _.first(_.remove(out.trim().split('\n'), (v) => v.indexOf(path.sep) === 0));
xcodebuild.logLocation = _.first(_.remove(out.trim().split('\n'), (v) => v.startsWith(path.sep)));
log.debug(`Log file for xcodebuild test: ${xcodebuild.logLocation}`);
}

// if we have an error we want to output the logs
// otherwise the failure is inscrutible
// but do not log permission errors from trying to write to attachments folder
if (out.indexOf('Error Domain=') !== -1 &&
out.indexOf('Error writing attachment data to file') === -1 &&
out.indexOf('Failed to remove screenshot at path') === -1) {
if (out.includes('Error Domain=') &&
!out.includes('Error writing attachment data to file') &&
!out.includes('Failed to remove screenshot at path')) {
logXcodeOutput = true;

// terrible hack to handle case where xcode return 0 but is failing
Expand All @@ -260,8 +260,8 @@ class XcodeBuild {

if (logXcodeOutput) {
// do not log permission errors from trying to write to attachments folder
if (out.indexOf('Error writing attachment data to file') === -1) {
for (let line of out.split('\n')) {
if (!out.includes('Error writing attachment data to file')) {
for (const line of out.split('\n')) {
xcodeLog.info(line);
}
}
Expand Down

0 comments on commit 30d8cef

Please sign in to comment.