From cb695ce0e31d691848ef8fed499a356dc642ea80 Mon Sep 17 00:00:00 2001 From: Alexander Sorokin Date: Mon, 17 Apr 2017 11:58:40 +0300 Subject: [PATCH] CB-12622: (android) Adapt Appium tests for Android 6 and 7 --- appium-tests/common/common.spec.js | 135 ++++++++++++++++------------- 1 file changed, 77 insertions(+), 58 deletions(-) diff --git a/appium-tests/common/common.spec.js b/appium-tests/common/common.spec.js index 9cb4c2b9..2849c234 100644 --- a/appium-tests/common/common.spec.js +++ b/appium-tests/common/common.spec.js @@ -61,14 +61,14 @@ describe('Contacts UI Automation Tests', function () { function getDriver() { driver = wdHelper.getDriver(PLATFORM); return wdHelper.getWebviewContext(driver, 2) - .then(function(context) { + .then(function (context) { webviewContext = context; return driver.context(webviewContext); }) - .then(function() { + .then(function () { return wdHelper.waitForDeviceReady(driver); }) - .then(function() { + .then(function () { return wdHelper.injectLibraries(driver); }); } @@ -79,15 +79,19 @@ describe('Contacts UI Automation Tests', function () { return driver .context(webviewContext) .setAsyncScriptTimeout(MINUTE) - .executeAsync(function(contactname, bday, callback) { + .executeAsync(function (contactname, bday, callback) { navigator.contacts.create({ 'displayName': contactname.formatted, 'name': contactname, 'note': 'DeleteMe', 'birthday': new Date(bday) - }).save(callback, callback); + }).save(function (successResult) { + callback(successResult); + }, function (failureResult) { + callback(failureResult); + }); }, [contactName, bdayString]) - .then(function(result) { + .then(function (result) { if (result && result.hasOwnProperty('code')) { throw result; } @@ -149,7 +153,7 @@ describe('Contacts UI Automation Tests', function () { obj.filter = oldname; obj.multiple = false; - navigator.contacts.find(['displayName', 'name'], function(contacts) { + navigator.contacts.find(['displayName', 'name'], function (contacts) { if (contacts.length === 0) { callback({ 'code': -35142 }); return; @@ -161,9 +165,11 @@ describe('Contacts UI Automation Tests', function () { name.familyName = newfamilyname; contact.name = name; contact.save(callback, callback); - }, callback, obj); + }, function (result) { + callback(result); + }, obj); }, [oldName, newGivenName, newFamilyName]) - .then(function(result) { + .then(function (result) { if (result && result.hasOwnProperty('code')) { if (result.code === -35142) { throw 'Couldn\'t find the contact "' + oldName + '"'; @@ -182,9 +188,9 @@ describe('Contacts UI Automation Tests', function () { var obj = new ContactFindOptions(); obj.filter = 'DeleteMe'; obj.multiple = true; - navigator.contacts.find(['note'], function(contacts) { + navigator.contacts.find(['note'], function (contacts) { var removes = []; - contacts.forEach(function(contact) { + contacts.forEach(function (contact) { removes.push(contact); }); if (removes.length === 0) { @@ -215,9 +221,11 @@ describe('Contacts UI Automation Tests', function () { }); } removeNext(nextToRemove); - }, callback, obj); + }, function (failureResult) { + callback(failureResult); + }, obj); }, []) - .then(function(result) { + .then(function (result) { if (typeof result !== 'undefined') { throw result; } @@ -244,7 +252,7 @@ describe('Contacts UI Automation Tests', function () { failedToStart = false; }, fail) .then(function () { - // on iOS, first interaction with contacts API will trigger the permission dialog. + // on iOS and Android >= 6, first interaction with contacts API will trigger the permission dialog. // We will attempt to bust it manually here, by triggering the contacts API // and waiting for the native dialog to show up, then dismissing the alert. // This only needs to be done once. @@ -252,54 +260,65 @@ describe('Contacts UI Automation Tests', function () { // (UIAutomation), Appium's autoAcceptAlerts capability handles this for us. This logic // is here as a transition between UIAutomation and XCUITest and is compatible with both. // More details in the comment below. - if (PLATFORM == 'ios') { - var promiseId = getNextPromiseId(); - var contactName = contactsHelper.getContactName('Permission', 'Buster'); - return driver - .context(webviewContext) - .execute(function (pID, contactname) { - navigator._appiumPromises[pID] = Q.defer(); - navigator.contacts.create({ - 'displayName': contactname.formatted, - 'name': contactname, - 'note': 'DeleteMe' - }).save(function (contact) { - navigator._appiumPromises[pID].resolve(contact); + var promiseId = getNextPromiseId(); + var contactName = contactsHelper.getContactName('Permission', 'Buster'); + return driver + .context(webviewContext) + .execute(function (pID, contactname) { + navigator._appiumPromises[pID] = Q.defer(); + navigator.contacts.create({ + 'displayName': contactname.formatted, + 'name': contactname, + 'note': 'DeleteMe' + }).save(function (contact) { + navigator._appiumPromises[pID].resolve(contact); + }, function (err) { + navigator._appiumPromises[pID].reject(err); + }); + }, [promiseId, contactName]) + .context('NATIVE_APP') + .then(function () { + // iOS + if (PLATFORM === 'ios') { + return driver.acceptAlert() + .then(function alertDismissed() { + // TODO: once we move to only XCUITest-based (which is force on you in either iOS 10+ or Xcode 8+) + // UI tests, we will have to: + // a) remove use of autoAcceptAlerts appium capability since it no longer functions in XCUITest + // b) can remove this entire then() clause, as we do not need to explicitly handle the acceptAlert + // failure callback, since we will be guaranteed to hit the permission dialog on startup. + }, function noAlert() { + // in case the contacts permission alert never showed up: no problem, don't freak out. + // This can happen if: + // a) The application-under-test already had contacts permissions granted to it + // b) Appium's autoAcceptAlerts capability is provided (and functioning) + }); + } + + // Android + return driver + .elementByXPath('//android.widget.Button[translate(@text, "alow", "ALOW")="ALLOW"]') + .click() + .fail(function noAlert() { }); + }) + .context(webviewContext) + .executeAsync(function (pID, cb) { + navigator._appiumPromises[pID].promise + .then(function (result) { + cb(result); }, function (err) { - navigator._appiumPromises[pID].reject(err); + cb('ERROR: ' + err); }); - }, [promiseId, contactName]) - .context('NATIVE_APP') - .acceptAlert() - .then(function alertDismissed() { - // TODO: once we move to only XCUITest-based (which is force on you in either iOS 10+ or Xcode 8+) - // UI tests, we will have to: - // a) remove use of autoAcceptAlerts appium capability since it no longer functions in XCUITest - // b) can remove this entire then() clause, as we do not need to explicitly handle the acceptAlert - // failure callback, since we will be guaranteed to hit the permission dialog on startup. - }, function noAlert() { - // in case the contacts permission alert never showed up: no problem, don't freak out. - // This can happen if: - // a) The application-under-test already had contacts permissions granted to it - // b) Appium's autoAcceptAlerts capability is provided (and functioning) - }) - .context(webviewContext) - .executeAsync(function (pID, cb) { - navigator._appiumPromises[pID].promise - .then(cb, function (err) { - cb('ERROR: ' + err); - }); - }, [promiseId]) - .then(function (result) { - if (typeof result === 'string' && result.indexOf('ERROR:') === 0) { - throw result; - } - return result; - }); - } + }, [promiseId]) + .then(function (result) { + if (typeof result === 'string' && result.indexOf('ERROR:') === 0) { + throw result; + } + return result; + }); }) .done(done); - }, 5 * MINUTE); + }, 10 * MINUTE); describe('Picking contacts', function () { afterEach(function (done) {