From e0c4106d22b955dd388922f6ff3d46ed2f6a4c96 Mon Sep 17 00:00:00 2001 From: "Isaac A. Murchie" Date: Thu, 3 Jan 2019 09:40:53 -0500 Subject: [PATCH] Lint and update JS sample code (#11927) * Lint and update JS wd sample code * Lint and update JS webdriverio sample code * Add JS sample code to greenkeeper --- .eslintignore | 2 +- .travis.yml | 1 + package.json | 13 ++++++++++ sample-code/javascript-wd/.babelrc | 13 ++++++++-- sample-code/javascript-wd/README.md | 10 +++---- sample-code/javascript-wd/package.json | 16 +++++++----- .../basic/android-basic-interactions.test.js | 5 ++-- .../test/basic/android-create-session.test.js | 4 +-- .../basic/android-create-web-session.test.js | 17 +++++++----- .../test/basic/ios-selectors.test.js | 4 +-- .../javascript-wd/test/helpers/apps.js | 12 ++++++--- .../javascript-wd/test/helpers/caps.js | 12 ++++----- sample-code/javascript-wd/test/mocha.opts | 3 +-- sample-code/javascript-webdriverio/README.md | 8 +++--- .../javascript-webdriverio/helpers/caps.js | 6 ++--- .../javascript-webdriverio/package.json | 3 ++- .../test/basic/android-create-session.test.js | 20 +++++++------- .../basic/android-create-web-session.test.js | 11 ++++---- .../test/basic/ios-basic-interactions.test.js | 26 ++++++++++--------- .../test/basic/ios-create-session.test.js | 16 +++++++----- .../test/basic/ios-create-web-session.test.js | 8 +++--- 21 files changed, 124 insertions(+), 86 deletions(-) diff --git a/.eslintignore b/.eslintignore index 59f237fc7..62562b74a 100644 --- a/.eslintignore +++ b/.eslintignore @@ -1,2 +1,2 @@ -sample-code coverage +node_modules diff --git a/.travis.yml b/.travis.yml index af7e01776..0af80a602 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,6 +15,7 @@ jobs: os: linux script: npm run test && npm run e2e-test node_js: "11" + after_script: npm run coverage - stage: os: linux script: npm run test && npm run e2e-test diff --git a/package.json b/package.json index 1b341c081..8da6453d7 100644 --- a/package.json +++ b/package.json @@ -122,6 +122,19 @@ "heapdump": "0.x" }, "greenkeeper": { + "groups": { + "appium": { + "packages": [ + "package.json" + ] + }, + "sample-code": { + "packages": [ + "sample-code/javascript-wd/package.json", + "sample-code/javascript-webdriverio/package.json" + ] + } + }, "ignore": [] } } diff --git a/sample-code/javascript-wd/.babelrc b/sample-code/javascript-wd/.babelrc index bcb6ee8de..2547b5b18 100644 --- a/sample-code/javascript-wd/.babelrc +++ b/sample-code/javascript-wd/.babelrc @@ -1,3 +1,12 @@ { - "presets": ["es2015", "stage-0"] -} \ No newline at end of file + "presets": [ + [ + "@babel/preset-env", + { + "targets": { + "node": "6" + } + } + ] + ] +} diff --git a/sample-code/javascript-wd/README.md b/sample-code/javascript-wd/README.md index 9d9e8542c..a4485e3b3 100644 --- a/sample-code/javascript-wd/README.md +++ b/sample-code/javascript-wd/README.md @@ -2,7 +2,7 @@ ## Setup -* Must have NodeJS and NPM installed (https://nodejs.org/en/) +* Must have NodeJS (6+) and NPM installed (https://nodejs.org/en/) * Install dependencies by running `npm install` ## Running Tests @@ -12,7 +12,7 @@ ## Troubleshooting -* ```Original error: '11.1' does not exist in the list of simctl SDKs. Only the following Simulator SDK versions are available on your system: x.y``` - * By default, these example tests expect IOS version 11.1 - * If 11.1 isn't available on your system, set the version by setting environment variable `IOS_PLATFORM_VERSION` or install with Xcode - (e.g.: `IOS_PLATFORM_VERSION=11.2 $(npm bin)/mocha -t 6000000 test/path/to/test.js`) +* ```Original error: '12.1' does not exist in the list of simctl SDKs. Only the following Simulator SDK versions are available on your system: x.y``` + * By default, these example tests expect IOS version 12.1 + * If 12.1 isn't available on your system, set the version by setting environment variable `IOS_PLATFORM_VERSION` + (e.g., `IOS_PLATFORM_VERSION=11.2 $(npm bin)/mocha -t 6000000 test/path/to/test.js`), or install the iOS 12.1 SDK with Xcode. diff --git a/sample-code/javascript-wd/package.json b/sample-code/javascript-wd/package.json index 7730797e5..31c2fd5f6 100644 --- a/sample-code/javascript-wd/package.json +++ b/sample-code/javascript-wd/package.json @@ -3,17 +3,21 @@ "version": "1.0.0", "description": "Appium sample code using 'wd'", "scripts": { - "test": "mocha test/**/*.test.js" + "test": "mocha test/**/*.test.js", + "clean": "rm -rf node_modules && rm -f package-lock.json && npm install" }, "author": "Dan Graham", "license": "Apache-2.0", + "engines": { + "node": ">=6", + "npm": ">=6" + }, "devDependencies": { - "babel-core": "^6.26.0", - "babel-polyfill": "^6.26.0", - "babel-preset-es2015": "^6.24.1", - "babel-preset-stage-0": "^6.24.1", + "@babel/register": "^7.0.0", + "@babel/core": "^7.0.0", + "@babel/preset-env": "^7.0.0", "chai": "^4.1.2", - "mocha": "^4.1.0", + "mocha": "^5.2.0", "wd": "^1.5.0" } } diff --git a/sample-code/javascript-wd/test/basic/android-basic-interactions.test.js b/sample-code/javascript-wd/test/basic/android-basic-interactions.test.js index 67268027e..8309b6733 100644 --- a/sample-code/javascript-wd/test/basic/android-basic-interactions.test.js +++ b/sample-code/javascript-wd/test/basic/android-basic-interactions.test.js @@ -1,15 +1,14 @@ import wd from 'wd'; import chai from 'chai'; import { androidCaps, serverConfig } from '../helpers/caps'; - const {assert} = chai; + const PACKAGE = 'io.appium.android.apis'; const SEARCH_ACTIVITY = '.app.SearchInvoke'; const ALERT_DIALOG_ACTIVITY = '.app.AlertDialogSamples'; describe('Basic Android interactions', function () { - let driver; before(async function () { @@ -60,4 +59,4 @@ describe('Basic Android interactions', function () { // Close the dialog await closeDialogButton.click(); }); -}); \ No newline at end of file +}); diff --git a/sample-code/javascript-wd/test/basic/android-create-session.test.js b/sample-code/javascript-wd/test/basic/android-create-session.test.js index 8fb7a4955..19be9d0eb 100644 --- a/sample-code/javascript-wd/test/basic/android-create-session.test.js +++ b/sample-code/javascript-wd/test/basic/android-create-session.test.js @@ -1,6 +1,6 @@ import wd from 'wd'; import chai from 'chai'; -import { iosCaps, androidCaps, serverConfig } from '../helpers/caps'; +import { androidCaps, serverConfig } from '../helpers/caps'; const {assert} = chai; @@ -23,4 +23,4 @@ describe('Create Android session', function () { // Quit the session await driver.quit(); }); -}); \ No newline at end of file +}); diff --git a/sample-code/javascript-wd/test/basic/android-create-web-session.test.js b/sample-code/javascript-wd/test/basic/android-create-web-session.test.js index cbcded3b7..c67f7abf2 100644 --- a/sample-code/javascript-wd/test/basic/android-create-web-session.test.js +++ b/sample-code/javascript-wd/test/basic/android-create-web-session.test.js @@ -5,24 +5,27 @@ import { androidCaps, serverConfig } from '../helpers/caps'; const {assert} = chai; describe('Create Chrome web session', function () { - it('should create and destroy Android browser session', async function () { + let driver; + before(async function () { // Connect to Appium server - const driver = await wd.promiseChainRemote(serverConfig); + driver = await wd.promiseChainRemote(serverConfig); // Start the session await driver.init({ ...androidCaps, browserName: 'Chrome' }); - + }); + after(async function () { + // Quit the session + await driver.quit(); + }); + it('should create and destroy Android browser session', async function () { // Navigate to google.com await driver.get('https://www.google.com'); // Test that it was successful by checking the document title const pageTitle = await driver.title(); assert.equal(pageTitle, 'Google'); - - // Quit the session - await driver.quit(); }); -}); \ No newline at end of file +}); diff --git a/sample-code/javascript-wd/test/basic/ios-selectors.test.js b/sample-code/javascript-wd/test/basic/ios-selectors.test.js index 6e97adf74..bdefcfed0 100644 --- a/sample-code/javascript-wd/test/basic/ios-selectors.test.js +++ b/sample-code/javascript-wd/test/basic/ios-selectors.test.js @@ -44,7 +44,7 @@ describe('Basic IOS selectors', function () { it('should find elements by class chain', async function () { // This is also an IOS-specific selector strategy. Similar to XPath. This is recommended over XPath. - const windowElement = await driver.elements('-ios class chain', 'XCUIElementTypeWindow[1]/*[2]'); + const windowElement = await driver.elements('-ios class chain', 'XCUIElementTypeWindow[1]/*'); assert.equal(windowElement.length, 1); }); @@ -54,4 +54,4 @@ describe('Basic IOS selectors', function () { const buttons = await driver.elementsByXPath('//XCUIElementTypeWindow//XCUIElementTypeButton'); assert.isAbove(buttons.length, 1, 'Should have more than one button'); }); -}); \ No newline at end of file +}); diff --git a/sample-code/javascript-wd/test/helpers/apps.js b/sample-code/javascript-wd/test/helpers/apps.js index e12b3984e..d8895a200 100644 --- a/sample-code/javascript-wd/test/helpers/apps.js +++ b/sample-code/javascript-wd/test/helpers/apps.js @@ -1,10 +1,14 @@ import path from 'path'; + +const githubAssetBase = "http://appium.github.io/appium/assets"; +const localAssetBase = path.resolve(__dirname, "..", "..", "..", "apps"); + if (process.env.SAUCE_LABS) { // TODO: Change thes URL's to updated locations - exports.iosTestApp = "http://appium.github.io/appium/assets/TestApp7.1.app.zip"; - exports.androidApiDemos = "http://appium.github.io/appium/assets/ApiDemos-debug.apk"; + exports.iosTestApp = `${githubAssetBase}/TestApp7.1.app.zip`; + exports.androidApiDemos = `${githubAssetBase}/ApiDemos-debug.apk`; } else { - exports.iosTestApp = path.resolve(__dirname, "..", "..", "apps", "TestApp.app.zip"); - exports.androidApiDemos = path.resolve(__dirname, "..", "..", "apps", "ApiDemos-debug.apk"); + exports.iosTestApp = path.resolve(localAssetBase, "TestApp.app.zip"); + exports.androidApiDemos = path.resolve(localAssetBase, "ApiDemos-debug.apk"); } diff --git a/sample-code/javascript-wd/test/helpers/caps.js b/sample-code/javascript-wd/test/helpers/caps.js index 862281906..a7b53d454 100644 --- a/sample-code/javascript-wd/test/helpers/caps.js +++ b/sample-code/javascript-wd/test/helpers/caps.js @@ -1,15 +1,15 @@ -import path from 'path'; - const iosCaps = { platformName: 'iOS', automationName: 'XCUITest', - deviceName: process.env.IOS_DEVICE_NAME || 'iPhone 6s', - platformVersion: process.env.IOS_PLATFORM_VERSION || '11.1', + deviceName: process.env.IOS_DEVICE_NAME || 'iPhone 6s', + platformVersion: process.env.IOS_PLATFORM_VERSION || '12.1', app: undefined, // Will be added in tests }; -// Leave the Android platformVersion blank and set deviceName to a random string (Android deviceName is ignored by Appium but is still required) -// If we're using SauceLabs, set the Android deviceName and platformVersion to the latest supported SauceLabs device and version +// Leave the Android platformVersion blank and set deviceName to a random string +// (Android deviceName is ignored by Appium but is still required) +// If we're using SauceLabs, set the Android deviceName and platformVersion to +// the latest supported SauceLabs device and version const DEFAULT_ANDROID_DEVICE_NAME = process.env.SAUCE_LABS ? 'Android GoogleAPI Emulator' : 'My Android Device'; const DEFAULT_ANDROID_PLATFORM_VERSION = process.env.SAUCE_LABS ? '7.1' : null; diff --git a/sample-code/javascript-wd/test/mocha.opts b/sample-code/javascript-wd/test/mocha.opts index 9b7802596..8910807b8 100644 --- a/sample-code/javascript-wd/test/mocha.opts +++ b/sample-code/javascript-wd/test/mocha.opts @@ -1,3 +1,2 @@ ---compilers babel-core/register ---require babel-polyfill +--require @babel/register --timeout 1800000 diff --git a/sample-code/javascript-webdriverio/README.md b/sample-code/javascript-webdriverio/README.md index 4efa5c65c..2fcb4fa7d 100644 --- a/sample-code/javascript-webdriverio/README.md +++ b/sample-code/javascript-webdriverio/README.md @@ -12,7 +12,7 @@ ## Troubleshooting -* ```Original error: '11.1' does not exist in the list of simctl SDKs. Only the following Simulator SDK versions are available on your system: x.y``` - * By default, these example tests expect IOS version 11.1 - * If 11.1 isn't available on your system, set the version by setting environment variable `IOS_PLATFORM_VERSION` or install with Xcode - (e.g.: `IOS_PLATFORM_VERSION=11.2 $(npm bin)/mocha -t 6000000 test/path/to/test.js`) +* ```Original error: '12.1' does not exist in the list of simctl SDKs. Only the following Simulator SDK versions are available on your system: x.y``` + * By default, these example tests expect IOS version 12.1 + * If 12.1 isn't available on your system, set the version by setting environment variable `IOS_PLATFORM_VERSION` + (e.g., `IOS_PLATFORM_VERSION=11.2 $(npm bin)/mocha -t 6000000 test/path/to/test.js`), or install the iOS 12.1 SDK with Xcode. diff --git a/sample-code/javascript-webdriverio/helpers/caps.js b/sample-code/javascript-webdriverio/helpers/caps.js index f977466af..b4784b305 100644 --- a/sample-code/javascript-webdriverio/helpers/caps.js +++ b/sample-code/javascript-webdriverio/helpers/caps.js @@ -1,10 +1,8 @@ -const path = require("path"); - const iosCaps = { platformName: "iOS", automationName: "XCUITest", deviceName: process.env.IOS_DEVICE_NAME || "iPhone 6s", - platformVersion: process.env.IOS_PLATFORM_VERSION || "11.1", + platformVersion: process.env.IOS_PLATFORM_VERSION || "12.1", app: undefined // Will be added in tests }; @@ -12,7 +10,7 @@ const iosWebCaps = { platformName: "iOS", automationName: "XCUITest", deviceName: process.env.IOS_DEVICE_NAME || "iPhone 6s", - platformVersion: process.env.IOS_PLATFORM_VERSION || "11.1", + platformVersion: process.env.IOS_PLATFORM_VERSION || "12.1", browserName: "Safari" }; diff --git a/sample-code/javascript-webdriverio/package.json b/sample-code/javascript-webdriverio/package.json index ce601cc00..31a927997 100644 --- a/sample-code/javascript-webdriverio/package.json +++ b/sample-code/javascript-webdriverio/package.json @@ -4,7 +4,8 @@ "description": "Sample tests using webdriverio", "main": "index.js", "scripts": { - "test": "mocha --timeout 6000000 test/basic" + "test": "mocha test/basic", + "clean": "rm -rf node_modules && rm -f package-lock.json && npm install" }, "author": "Daniel Graham", "license": "Apache-2.0", diff --git a/sample-code/javascript-webdriverio/test/basic/android-create-session.test.js b/sample-code/javascript-webdriverio/test/basic/android-create-session.test.js index dc13ab0ca..8874e2f7c 100644 --- a/sample-code/javascript-webdriverio/test/basic/android-create-session.test.js +++ b/sample-code/javascript-webdriverio/test/basic/android-create-session.test.js @@ -5,19 +5,21 @@ const assert = require("chai").assert; androidOptions.capabilities.app = app; -describe("Create Android session", function() { +describe("Create Android session", function () { let client; - before(async() => { + before(async function () { client = await webdriverio.remote(androidOptions); }); - it("should create and destroy a session", async function() { - const res = await client.status(); - assert.isObject(res.build); - const current_package = await client.getCurrentPackage(); - assert.equal(current_package, "io.appium.android.apis"); - const delete_session = await client.deleteSession(); - assert.isNull(delete_session); + it("should create and destroy a session", async function () { + const res = await client.status(); + assert.isObject(res.build); + + const current_package = await client.getCurrentPackage(); + assert.equal(current_package, "io.appium.android.apis"); + + const delete_session = await client.deleteSession(); + assert.isNull(delete_session); }); }); diff --git a/sample-code/javascript-webdriverio/test/basic/android-create-web-session.test.js b/sample-code/javascript-webdriverio/test/basic/android-create-web-session.test.js index f2ab94060..b33995917 100644 --- a/sample-code/javascript-webdriverio/test/basic/android-create-web-session.test.js +++ b/sample-code/javascript-webdriverio/test/basic/android-create-web-session.test.js @@ -2,21 +2,22 @@ const webdriverio = require("webdriverio"); const androidOptions = require("../../helpers/caps").androidWebOptions; const assert = require("chai").assert; -describe("Create Chrome web session", function() { +describe("Create Chrome web session", function () { let client; - before(async function() { + before(async function () { client = await webdriverio.remote(androidOptions); }); - after(async function() { + after(async function () { return await client.deleteSession(); }); - it("should create and destroy Android browser session", async function() { + it("should create and destroy Android browser session", async function () { // Navigate to google.com - let client = await webdriverio.remote(iosOptions); + const client = await webdriverio.remote(androidOptions); await client.url("https://www.google.com"); + const title = await client.getTitle(); assert.equal(title, "Google"); }); diff --git a/sample-code/javascript-webdriverio/test/basic/ios-basic-interactions.test.js b/sample-code/javascript-webdriverio/test/basic/ios-basic-interactions.test.js index 6c7d30db0..442f609d0 100644 --- a/sample-code/javascript-webdriverio/test/basic/ios-basic-interactions.test.js +++ b/sample-code/javascript-webdriverio/test/basic/ios-basic-interactions.test.js @@ -5,29 +5,31 @@ const assert = require("chai").assert; iosOptions.capabilities.app = app; -describe("Basic IOS interactions", function() { +describe("Basic IOS interactions", function () { let client; - beforeEach(async function() { + beforeEach(async function () { client = await webdriverio.remote(iosOptions); }); - afterEach(async function() { - await client.deleteSession(); + afterEach(async function () { + await client.deleteSession(); }); - it("should send keys to inputs", async function() { - const elementId = await client.findElement("accessibility id","TextField1"); + it("should send keys to inputs", async function () { + const elementId = await client.findElement("accessibility id", "TextField1"); client.elementSendKeys(elementId.ELEMENT, "Hello World!"); - const elementValue = await client.findElement("accessibility id","TextField1"); - await client.getElementAttribute(elementValue.ELEMENT,"value").then((attr) => { - assert.equal(attr,"Hello World!"); + + const elementValue = await client.findElement("accessibility id", "TextField1"); + await client.getElementAttribute(elementValue.ELEMENT, "value").then((attr) => { + assert.equal(attr, "Hello World!"); }); }); - it("should click a button that opens an alert", async function() { - const element = await client.findElement("accessibility id","show alert"); + it("should click a button that opens an alert", async function () { + const element = await client.findElement("accessibility id", "show alert"); await client.elementClick(element.ELEMENT); - assert.equal(await client.getAlertText(),"Cool title\nthis alert is so cool."); + + assert.equal(await client.getAlertText(), "Cool title\nthis alert is so cool."); }); }); diff --git a/sample-code/javascript-webdriverio/test/basic/ios-create-session.test.js b/sample-code/javascript-webdriverio/test/basic/ios-create-session.test.js index 3456c0429..b02db512c 100644 --- a/sample-code/javascript-webdriverio/test/basic/ios-create-session.test.js +++ b/sample-code/javascript-webdriverio/test/basic/ios-create-session.test.js @@ -5,20 +5,22 @@ const assert = require("chai").assert; iosOptions.capabilities.app = app; -describe("Create session", function() { +describe("Create session", function () { let client; - beforeEach(async function() { + beforeEach(async function () { client = await webdriverio.remote(iosOptions); }); - it("should create and destroy IOS sessions", async function() { + it("should create and destroy IOS sessions", async function () { const res = await client.status(); assert.isObject(res.build); - const element = await client.findElement("class name","XCUIElementTypeApplication"); - client.getElementAttribute(element.ELEMENT,"name").then((attr) => { - assert.equal(attr,"TestApp"); - }); + + const element = await client.findElement("class name", "XCUIElementTypeApplication"); + await client.getElementAttribute(element.ELEMENT, "name").then((attr) => { + assert.equal(attr, "TestApp"); + }); + const destroySession = await client.deleteSession(); assert.isNull(destroySession); }); diff --git a/sample-code/javascript-webdriverio/test/basic/ios-create-web-session.test.js b/sample-code/javascript-webdriverio/test/basic/ios-create-web-session.test.js index 4d39bf0b1..7ab5eff52 100644 --- a/sample-code/javascript-webdriverio/test/basic/ios-create-web-session.test.js +++ b/sample-code/javascript-webdriverio/test/basic/ios-create-web-session.test.js @@ -1,11 +1,11 @@ const webdriverio = require("webdriverio"); const iosOptions = require("../../helpers/caps").iosWebOptions; -const app = require("../../helpers/apps").iosTestApp; const assert = require("chai").assert; -describe("Create Safari session", function() { - it("should create and destroy IOS Safari session", async function() { - let client = await webdriverio.remote(iosOptions); + +describe("Create Safari session", function () { + it("should create and destroy IOS Safari session", async function () { + const client = await webdriverio.remote(iosOptions); await client.url("https://www.google.com"); const title = await client.getTitle(); assert.equal(title, "Google");