Return-Path: X-Original-To: apmail-cordova-dev-archive@www.apache.org Delivered-To: apmail-cordova-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C7F90187BD for ; Wed, 16 Mar 2016 10:29:27 +0000 (UTC) Received: (qmail 7090 invoked by uid 500); 16 Mar 2016 10:29:27 -0000 Delivered-To: apmail-cordova-dev-archive@cordova.apache.org Received: (qmail 7048 invoked by uid 500); 16 Mar 2016 10:29:27 -0000 Mailing-List: contact dev-help@cordova.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cordova.apache.org Delivered-To: mailing list dev@cordova.apache.org Received: (qmail 7032 invoked by uid 99); 16 Mar 2016 10:29:27 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 16 Mar 2016 10:29:27 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 12B8ADFA44; Wed, 16 Mar 2016 10:29:27 +0000 (UTC) From: sgrebnov To: dev@cordova.apache.org Reply-To: dev@cordova.apache.org References: In-Reply-To: Subject: [GitHub] cordova-plugin-contacts pull request: CB-10399 Added Appium tests Content-Type: text/plain Message-Id: <20160316102927.12B8ADFA44@git1-us-west.apache.org> Date: Wed, 16 Mar 2016 10:29:27 +0000 (UTC) Github user sgrebnov commented on a diff in the pull request: https://github.com/apache/cordova-plugin-contacts/pull/101#discussion_r56311100 --- Diff: appium-tests/helpers/contactsHelper.js --- @@ -0,0 +1,222 @@ +/* jshint node: true */ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * +*/ + +'use strict'; + +function prepare(item) { + if (typeof item === 'object') { + return JSON.stringify(item); + } + if (typeof item === 'string') { + return '"' + item + '"'; + } + return undefined; +} + +module.exports.getContactName = function (firstName, lastName) { + return { + formatted: firstName == lastName === undefined ? null : firstName + ' ' + lastName, + familyName: lastName, + givenName: firstName, + middleName: '' + }; +}; + +module.exports.getAddContactCode = function (displayName, contactName, phoneNumber) { + var preparedDisplayName = prepare(displayName); + console.log('preparedDisplayName is ' + preparedDisplayName); + var preparedContactName = prepare(contactName); + var preparedPhoneNumber = prepare(phoneNumber); + + var result = + 'try {\n' + + ' var results = document.getElementById("info");\n' + + ' var contact = navigator.contacts.create({ "displayName": ' + preparedDisplayName + ', "name": ' + preparedContactName + ', "note": "DeleteMe" });\n' + + + ' var phoneNumbers = [1];\n' + + ' phoneNumbers[0] = new ContactField("work", ' + preparedPhoneNumber + ', true);\n' + + ' contact.phoneNumbers = phoneNumbers;\n' + + + ' contact.save(function() {\n' + + ' results.innerHTML = "' + (displayName || 'Nameless contact') + ' saved.";\n' + + ' }, function(e) {\n' + + ' if (e.code === ContactError.NOT_SUPPORTED_ERROR) {\n' + + ' results.innerHTML = "Saving contacts not supported.";\n' + + ' } else {\n' + + ' results.innerHTML = "Contact save failed: error " + e.code;\n' + + ' }\n' + + ' });\n' + + '} catch (e) {\n' + + ' var results = document.getElementById("info");\n' + + ' results.innerHTML = "ERROR: " + e.code;\n' + + '}\n'; + + return result; +}; + +module.exports.getGetContactsCode = function (filter) { + var preparedFilter = prepare(filter); + + var result = + 'var results = document.getElementById("info");\n' + + 'var obj = new ContactFindOptions();\n' + + 'if (' + preparedFilter + ') {\n' + + ' obj.filter = ' + preparedFilter + ';\n' + + '}\n' + + 'obj.multiple = true;\n' + + 'navigator.contacts.find(["displayName", "name", "phoneNumbers", "emails", "urls", "note"], function(contacts) {\n' + + ' var s = "";\n' + + ' if (contacts.length === 0) {\n' + + ' s = "No contacts found";\n' + + ' } else {\n' + + ' s = "Number of contacts: " + contacts.length + "
";\n' + + ' for (var i = 0; i < contacts.length; i++) {\n' + + ' var contact = contacts[i];\n' + + ' var contactNameTag = contact.name ? "";\n' + + ' }\n' + + ' s = s + "
NamePhoneEmail
" + contact.name.formatted + "" : "
(No Name)";\n' + + ' s = s + contactNameTag;\n' + + ' if (contact.phoneNumbers && contact.phoneNumbers.length > 0) {\n' + + ' s = s + contact.phoneNumbers[0].value;\n' + + ' }\n' + + ' s = s + "";\n' + + ' if (contact.emails && contact.emails.length > 0) {\n' + + ' s = s + contact.emails[0].value;\n' + + ' }\n' + + ' s = s + "
";\n' + + ' }\n' + + ' results.innerHTML = s;\n' + + '}, function(e) {\n' + + ' results.innerHTML = "Search failed: error " + e.code;\n' + + '}, obj);'; + + return result; +}; + +module.exports.getPickContactCode = function () { + var result = + 'var results = document.getElementById("info");\n' + + 'navigator.contacts.pickContact(\n' + + ' function (contact) {\n' + + ' results.innerHTML = contact ?\n' + + ' "Picked contact:
" + JSON.stringify(contact, null, 4) + "
" :\n' + + ' "No contacts found";\n' + + ' },\n' + + ' function (e) {\n' + + ' results.innerHTML = e && e.code === ContactError.OPERATION_CANCELLED_ERROR ?\n' + + ' "Pick cancelled" :\n' + + ' "Pick failed: " + (e && e.code);\n' + + ' }\n' + + ');'; + + return result; +}; + +module.exports.getRenameContactCode = function (oldDisplayName, newDisplayName, newName) { + // these default values are copied from manual contacts tests + if (!oldDisplayName) { + oldDisplayName = 'Dooney Evans'; + } + if (!newDisplayName) { + newDisplayName = 'Urist McContact'; + } + if (!newName) { + newName = module.exports.getContactName('Urist', 'McContact'); + } + var preparedOldDisplayName = prepare(oldDisplayName); + var preparedNewDisplayName = prepare(newDisplayName); + var preparedNewName = prepare(newName); + + var result = + 'var results = document.getElementById("info");\n' + + 'var obj = new ContactFindOptions();\n' + + 'obj.filter = ' + preparedOldDisplayName + ';\n' + + 'obj.multiple = false;\n' + + + 'navigator.contacts.find(["displayName", "name"], function(contacts) {\n' + + ' if (contacts.length === 0) {\n' + + ' results.innerHTML = "No contacts to update.";\n' + + ' return;\n' + + ' }\n' + + ' var contact = contacts[0];\n' + + ' contact.displayName = ' + preparedNewDisplayName + ';\n' + + ' contact.name = ' + preparedNewName + ';\n' + + ' contact.save(function(updated) {\n' + + ' results.innerHTML = "Contact updated.";\n' + + ' },function(e) {\n' + + ' results.innerHTML = "Update failed: error " + e.code;\n' + + ' });\n' + + '}, function(e) {\n' + + ' results.innerHTML = "Search failed: error " + e.code;\n' + + '}, obj);'; + + return result; +}; + +module.exports.getRemoveTestContactsCode = function () { + var result = + 'var results = document.getElementById("info");\n' + + 'results.innerHTML = "";\n' + + 'var obj = new ContactFindOptions();\n' + + 'obj.filter = "DeleteMe";\n' + + 'obj.multiple = true;\n' + + 'navigator.contacts.find(["note"], function(contacts) {\n' + + ' var removes = [];\n' + + ' contacts.forEach(function(contact) {\n' + + ' removes.push(contact);\n' + + ' });\n' + + ' if (removes.length === 0) {\n' + + ' results.innerHTML = "No contacts to remove";\n' + + ' return;\n' + + ' }\n' + + + ' var nextToRemove;\n' + + ' if (removes.length > 0) {\n' + + ' nextToRemove = removes.shift();\n' + + ' }\n' + + + ' function removeNext(item) {\n' + + ' if (typeof item === "undefined") {\n' + + ' return;\n' + + ' }\n' + + + ' if (removes.length > 0) {\n' + + ' nextToRemove = removes.shift();\n' + + ' } else {\n' + + ' nextToRemove = undefined;\n' + + ' }\n' + + + ' item.remove(function removeSucceeded() {\n' + + ' results.innerHTML += "Removed a contact with ID " + item.id + "
";\n' + + ' removeNext(nextToRemove);\n' + + ' }, function removeFailed() {\n' + + ' results.innerHTML += "Failed to remove a contact with ID " + item.id + "
";\n' + + ' removeNext(nextToRemove);\n' + + ' });\n' + + ' }\n' + + ' removeNext(nextToRemove);\n' + + '}, function(e) {\n' + + ' results.innerHTML = "Search failed: error " + e.code;\n' + + '}, obj);'; + + return result; +}; --- End diff -- writing code in strings is a bad pattern indeed. But I don't like the idea to rely on buttons/divs from manual tests. I propose the following: 1. Step1. Add helper function to appium which is based on execute, but takes single PARAMETERLESS function as argument repreneting function to be executed on app side. So you will be able to do `return execute(fn.toString())` ti run js code on app side, for example ``` .execute(function() { navigator.contacts.create('someParams', winCallback, falseCallback) }) ``` 2. Step2. Inject gloabal win and fail functions to be used on app context as a first step before running tests, so you will be able to pass them in all functions, for example `__appiumWinCallback and __appiumFailCallback` So you will be able to run the following code ``` .execute(function() { navigator.contacts.create('someParams', __appiumWinCallback, __appiumFailCallback) }) ``` 3. Step2. Inject extra function to return last execution results (should be based on __appiumWinCallback and __appiumFailCallback implementations), sample usage (appium side) ``` var result = execute('_appiumLastTestResult') result.win <- raw result from __appiumWinCallback callback (if called) result.fail <- raw result from __appiumFailCallback fail ``` --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@cordova.apache.org For additional commands, e-mail: dev-help@cordova.apache.org