Github user alsorokin commented on a diff in the pull request:

    
https://github.com/apache/cordova-plugin-contacts/pull/101#discussion_r57571624
  
    --- 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);
    +                });
    +            }, [callbackId])
    +            .context('NATIVE_APP')
    +            .then(function () {
    +                switch (PLATFORM) {
    +                    case 'ios':
    +                        return 
driver.waitForElementByXPath(UNORM.nfd('//UIAStaticText[@label="' + name + 
'"]'), MINUTE);
    +                    case 'android':
    +                        return 
driver.waitForElementByXPath('//android.widget.TextView[@text="' + name + '"]', 
MINUTE);
    +                }
    +            })
    +            .click()
    +            .context(webviewContext)
    +            .waitForElementById(callbackId)
    +            .getAttribute('innerHTML')
    +            .then(function (result) {
    +                if (result && typeof result === 'string' && 
result.indexOf('ERROR: ') === 0) {
    +                    throw result.slice(7);
    +                }
    +                return JSON.parse(result);
    +            });
    +    }
    +
    +    function renameContact(oldName, newGivenName, newFamilyName) {
    +        return driver
    +            .context(webviewContext)
    +            .setAsyncScriptTimeout(MINUTE)
    +            .executeAsync(function (oldname, newgivenname, newfamilyname, 
callback) {
    +                var obj = new ContactFindOptions();
    +                obj.filter = oldname;
    +                obj.multiple = false;
    +
    +                navigator.contacts.find(['displayName', 'name'], 
function(contacts) {
    +                    if (contacts.length === 0) {
    +                        return;
    +                    }
    +                    var contact = contacts[0];
    +                    contact.displayName = newgivenname + ' ' + 
newfamilyname;
    +                    var name = new ContactName();
    +                    name.givenName = newgivenname;
    +                    name.familyName = newfamilyname;
    +                    contact.name = name;
    +                    contact.save(callback, callback);
    +                }, callback, obj);
    +            }, [oldName, newGivenName, newFamilyName])
    +            .then(function(result) {
    +                if (result && result.hasOwnProperty('code')) {
    +                    throw result;
    +                }
    +                return result;
    +            });
    +    }
    +
    +    function removeTestContacts() {
    +        return driver
    +            .context(webviewContext)
    +            .setAsyncScriptTimeout(MINUTE)
    +            .executeAsync(function (callback) {
    +                var obj = new ContactFindOptions();
    +                obj.filter = 'DeleteMe';
    +                obj.multiple = true;
    +                navigator.contacts.find(['note'], function(contacts) {
    +                    var removes = [];
    +                    contacts.forEach(function(contact) {
    +                        removes.push(contact);
    +                    });
    +                    if (removes.length === 0) {
    +                        return;
    +                    }
    +
    +                   var nextToRemove;
    +                   if (removes.length > 0) {
    +                        nextToRemove = removes.shift();
    +                    }
    +
    +                    function removeNext(item) {
    +                        if (typeof item === 'undefined') {
    +                            callback();
    +                            return;
    +                        }
    +
    +                        if (removes.length > 0) {
    +                            nextToRemove = removes.shift();
    +                        } else {
    +                            nextToRemove = undefined;
    +                        }
    +
    +                        item.remove(function removeSucceeded() {
    +                            removeNext(nextToRemove);
    +                        }, function removeFailed() {
    +                            removeNext(nextToRemove);
    +                        });
    +                    }
    +                    removeNext(nextToRemove);
    +                }, callback, obj);
    +            }, [])
    +            .then(function(result) {
    +                if (typeof result !== 'undefined') {
    +                    throw result;
    +                }
    +            });
    +    }
    +
    +    it('contacts.ui.util configuring driver and starting a session', 
function (done) {
    +        getDriver()
    +            .fail(fail)
    +            .finally(done);
    +    }, 5 * MINUTE);
    +
    +    describe('Picking contacts', function () {
    +        afterEach(function (done) {
    +            removeTestContacts()
    +                .finally(done);
    +        }, MINUTE);
    +
    +        it('contacts.ui.spec.1 Pick a contact', function (done) {
    +            driver
    +                .then(function () {
    +                    return addContact('Test', 'Contact');
    +                })
    +                .then(function () {
    +                    return pickContact('Test Contact');
    +                })
    +                .then(function (contact) {
    +                    expect(contact.name.givenName).toBe('Test');
    +                    expect(contact.name.familyName).toBe('Contact');
    +                })
    +                .fail(saveScreenshotAndFail)
    +                .finally(done);
    +        }, 5 * MINUTE);
    +
    +        it('contacts.ui.spec.2 Update an existing contact', function 
(done) {
    +            driver
    +                .then(function () {
    +                    return addContact('Dooney', 'Evans');
    +                })
    +                .then(function () {
    +                    return renameContact('Dooney Evans', 'Urist', 
'McContact');
    +                })
    +                .then(function (contact) {
    +                    expect(contact.name.givenName).toBe('Urist');
    +                    expect(contact.name.familyName).toBe('McContact');
    +                })
    +                .then(function () {
    +                    return pickContact('Urist McContact');
    +                })
    +                .then(function (contact) {
    +                    expect(contact.name.givenName).toBe('Urist');
    +                    expect(contact.name.familyName).toBe('McContact');
    +                })
    +                .fail(saveScreenshotAndFail)
    --- End diff --
    
    Taking screenshot should be a part of a promise chain. In fact, some of our 
current failures (camera plugin) on the CI are there because in that old 
version of camera tests we take screenshots without respect to the promise 
chain:
    
https://github.com/apache/cordova-plugin-camera/blob/master/appium-tests/android/android.spec.js#L62


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@cordova.apache.org
For additional commands, e-mail: dev-h...@cordova.apache.org

Reply via email to