[
https://issues.apache.org/jira/browse/CB-10399?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15209269#comment-15209269
]
ASF GitHub Bot commented on CB-10399:
-------------------------------------
Github user dblotsky commented on a diff in the pull request:
https://github.com/apache/cordova-plugin-contacts/pull/101#discussion_r57245776
--- Diff: appium-tests/common/common.spec.js ---
@@ -0,0 +1,321 @@
+/*jshint node: true, jasmine: true, browser: true */
+/*global ContactFindOptions, ContactName*/
+
+/*
+ *
+ * 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.
+ *
+*/
+
+// these tests are meant to be executed by Cordova Medic Appium runner
+// you can find it here: https://github.com/apache/cordova-medic/
+// it is not necessary to do a full CI setup to run these tests, just run:
+// node cordova-medic/medic/medic.js appium --platform android --plugins
cordova-plugin-contacts
+
+'use strict';
+
+var wdHelper = global.WD_HELPER;
+var screenshotHelper = global.SCREENSHOT_HELPER;
+var contactsHelper = require('../helpers/contactsHelper');
+
+var MINUTE = 60 * 1000;
+var PLATFORM = global.PLATFORM;
+var UNORM = global.UNORM;
+
+describe('Contacts Android', function () {
+ var driver;
+ var webviewContext;
+ var callbackCount = 0;
+
+ function getNextCallbackId() {
+ return 'appium_callback_' + callbackCount++;
+ }
+
+ function saveScreenshotAndFail(error) {
+ fail(error);
+ return screenshotHelper
+ .saveScreenshot(driver)
+ .quit()
+ .then(function () {
+ return getDriver();
+ });
+ }
+
+ function getDriver() {
+ var getWebviewContext = function () {
+ return driver
+ .contexts()
+ .then(function (contexts) {
+ var found = false;
+ // take the last webview context
+ for (var i = 0; i < contexts.length ; i++) {
+ if (contexts[i].indexOf('WEBVIEW') >= 0) {
+ webviewContext = contexts[i];
+ found = true;
+ }
+ }
+ if (!found) {
+ // no webview context, the app is still loading
+ return driver
+ .sleep(10000)
+ .then(getWebviewContext);
+ }
+ });
+ };
+ driver = wdHelper.getDriver(PLATFORM);
+ return getWebviewContext();
+ }
+
+ function addContact(firstName, lastName) {
+ var contactName = contactsHelper.getContactName(firstName,
lastName);
+ return driver
+ .context(webviewContext)
+ .setAsyncScriptTimeout(MINUTE)
+ .executeAsync(function(contactname, callback) {
+ navigator.contacts
+ .create({ 'displayName': contactname.formatted,
'name': contactname, 'note': 'DeleteMe' })
+ .save(callback, callback);
+ }, [contactName])
+ .then(function(result) {
+ if (result && result.hasOwnProperty('code')) {
+ throw result;
+ }
+ return result;
+ });
+ }
+
+ function pickContact(name) {
+ var callbackId = getNextCallbackId();
+ return driver
+ .context(webviewContext)
+ .execute(function (cbId) {
+ var cbEl = document.createElement('div');
+ cbEl.id = cbId;
+ cbEl.style.display = 'none';
+ navigator.contacts.pickContact(function (contact) {
+ cbEl.innerHTML = JSON.stringify(contact);
+ document.body.appendChild(cbEl);
+ }, function (err) {
+ cbEl.innerHTML = 'ERROR: ' + err;
+ document.body.appendChild(cbEl);
+ });
--- End diff --
@riknoll and I thought about this approach and realized that this can be
done in a cleaner way by using promises. Instead of creating a DOM element to
store the result, you can wrap the `pickContact` call in a promise and save the
promise on the `navigator` object. Then you can just call `.done()` on it using
`execAsync` instead of using `waitForElementById`.
> Implement Appium tests for Contacts plugin
> ------------------------------------------
>
> Key: CB-10399
> URL: https://issues.apache.org/jira/browse/CB-10399
> Project: Apache Cordova
> Issue Type: Task
> Components: Medic, Plugin Contacts
> Reporter: Alexander Sorokin
> Assignee: Alexander Sorokin
>
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]