cordova-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dblotsky <...@git.apache.org>
Subject [GitHub] cordova-plugin-contacts pull request: CB-10399 Added Appium tests
Date Fri, 11 Mar 2016 02:02:23 GMT
Github user dblotsky commented on a diff in the pull request:

    https://github.com/apache/cordova-plugin-contacts/pull/101#discussion_r55780842
  
    --- Diff: appium-tests/android/android.spec.js ---
    @@ -0,0 +1,338 @@
    +/*jshint node: true, jasmine: 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.
    + *
    +*/
    +
    +// 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 = require('../helpers/wdHelper');
    +var screenshotHelper = require('../helpers/screenshotHelper');
    +var contactsHelper = require('../helpers/contactsHelper');
    +
    +var STARTING_MESSAGE = 'Ready for action!';
    +var MINUTE = 60 * 1000;
    +var DEFAULT_WEBVIEW_CONTEXT = 'WEBVIEW';
    +
    +describe('Contacts Android', function () {
    +    var driver;
    +    // the name of webview context, it will be changed to match needed context if there
are named ones:
    +    var webviewContext = DEFAULT_WEBVIEW_CONTEXT;
    +    // this indicates that there was a critical error and we should try to recover:
    +    var errorFlag = false;
    +    // this indicates that we couldn't restore Appium session and should fail fast:
    +    var stopFlag = false;
    +
    +    function win() {
    +        expect(true).toBe(true);
    +    }
    +
    +    function fail(error) {
    +        if (error === 'rethrow') {
    +            throw error;
    +        }
    +        if (error && error.message) {
    +            console.log('An error occured: ' + error.message);
    +            if (error.stack) {
    +                console.log(error.stack);
    +            }
    +            expect(true).toFailWithMessage(error.message);
    +        } else if (error && typeof error === 'string') {
    +            console.log('Failed expectation: ' + error);
    +            expect(true).toFailWithMessage(error);
    +        } else {
    +            // no message provided :(
    +            expect(true).toBe(false);
    +        }
    +        return screenshotHelper
    +            .saveScreenshot(driver)
    +            .finally(function() {
    +                throw 'rethrow';
    +            });
    +    }
    +
    +    function enterTest() {
    +        return driver
    +            // trying to determine where we are
    +            .context(webviewContext)
    +            .fail(function (error) {
    +                return fail(error);
    +            })
    +            .elementById('info')
    +            .then(function () {
    +                return driver; //we're already on the test screen
    +            }, function () {
    +                return driver
    +                    .elementById('middle')
    +                    .then(function () {
    +                        return driver
    +                            // we're on autotests page, we should go to start page
    +                            .execute('window.location = "../index.html"')
    +                            .sleep(5000)
    +                            .fail(function () {
    +                                errorFlag = true;
    +                                throw 'Couldn\'t find start page.';
    +                            });
    +                    }, function () {
    +                        return; // no-op
    +                    })
    +                    // unknown starting page: no 'info' div
    +                    // adding it manually
    +                    .execute('var info = document.createElement("div"); ' +
    +                             'info.id = "info"; ' +
    +                             'document.body.appendChild(info);');
    +            })
    +            .sleep(5000);
    +    }
    +
    +    function getDriver() {
    +        driver = wdHelper.getDriver('Android');
    +        return driver;
    +    }
    +
    +    function checkStopFlag() {
    +        if (stopFlag) {
    +            fail('Something went wrong: the stopFlag is on. Please see the log for more
details.');
    +        }
    +        return stopFlag;
    +    }
    +
    +    beforeEach(function () {
    +        jasmine.addMatchers({
    +            toFailWithMessage: function () {
    +                return {
    +                    compare: function (actual, msg) {
    +                        console.log('Failing with message: ' + msg);
    +                        var result = {
    +                            pass: false,
    +                            message: msg
    +                        };
    +                        // status 6 means that we've lost the session
    +                        // status 7 means that Appium couldn't find an element
    +                        // both these statuses mean that the test has failed but
    +                        // we should try to recreate the session for the following tests
    +                        if (msg.indexOf('Error response status: 6') >= 0 ||
    +                            msg.indexOf('Error response status: 7') >= 0) {
    +                            errorFlag = true;
    +                        }
    +                        return result;
    +                    }
    +                };
    +            },
    +            toContain: function () {
    +                return {
    +                    compare: function (actual, expected) {
    +                        var result = {
    +                            pass: true,
    +                            message: ''
    +                        };
    +                        if (actual.indexOf(expected) < 0)  {
    +                            result.pass = false;
    +                            result.message = 'Expected ' + actual + ' to contain ' +
expected;
    +                        }
    +                        return result;
    +                    }
    +                };
    +            }
    +        });
    +    });
    +
    +    it('camera.ui.util configuring driver and starting a session', function (done) {
    +        stopFlag = true; // just in case of timeout
    +        getDriver().then(function () {
    +            stopFlag = false;
    +        }, function (error) {
    +            return fail(error);
    +        })
    +        .finally(done);
    +    }, 5 * MINUTE);
    +
    +    it('camera.ui.util determine webview context name', function (done) {
    +        var i = 0;
    +        return driver
    +            .contexts(function (err, contexts) {
    +                if (err) {
    +                    console.log(err);
    +                }
    +                for (i = 0; i < contexts.length; i++) {
    +                    if (contexts[i].indexOf('mobilespec') >= 0) {
    +                        webviewContext = contexts[i];
    +                    }
    +                }
    +                done();
    +            });
    +    }, MINUTE);
    +
    +    describe("tests", function () {
    +        beforeEach(function (done) {
    +            // prepare the app for the test
    +            if (!stopFlag) {
    +                return driver
    +                    .context(webviewContext)
    +                    .then(function () {
    +                        return driver; // no-op
    +                    }, function (error) {
    +                        expect(true).toFailWithMessage(error);
    +                    })
    +                    .execute('document.getElementById("info").innerHTML = "' + STARTING_MESSAGE
+ '";')
    +                    .finally(done);
    +            }
    +            done();
    +        }, 3 * MINUTE);
    +
    +        afterEach(function (done) {
    +            if (!errorFlag || stopFlag) {
    +                // either there's no error or we've failed irrecoverably
    +                // nothing to worry about!
    +                done();
    +                return;
    +            }
    +            // recreate the session if there was a critical error in a previous spec
    +            stopFlag = true; // we're going to set this to false if we're able to restore
the session
    +            return driver
    +                .quit()
    +                .then(function () {
    +                    return getDriver()
    +                        .then(function () {
    +                            errorFlag = false;
    +                            stopFlag = false;
    +                        }, function (error) {
    +                            return fail(error);
    +                            stopFlag = true;
    +                        });
    +                }, function (error) {
    +                    return fail(error);
    +                    stopFlag = true;
    +                })
    +                .finally(done);
    +        }, 3 * MINUTE);
    +
    +        afterEach(function (done) {
    +            // clean all created contacts
    +            driver
    +                .context(webviewContext)
    +                .execute(contactsHelper.getRemoveTestContactsCode())
    +                .sleep(5000)
    +                .then(win, fail)
    +                .finally(done);
    +        }, MINUTE);
    +
    +        it('contacts.ui.spec.1 Ensuring pick correctness', function (done) {
    +            var contactName = contactsHelper.getContactName('Test', 'Contact');
    +            enterTest()
    +                .execute(contactsHelper.getAddContactCode(contactName.formatted, contactName,
"8 800 5553535"))
    +                .sleep(5000)
    +                .execute(contactsHelper.getGetContactsCode())
    +                .sleep(5000)
    +                .elementById('info')
    +                .getAttribute('innerHTML')
    +                .then(function (html) {
    +                    expect(html).toContain(contactName.formatted);
    +                })
    +                .execute(contactsHelper.getPickContactCode())
    +                .sleep(7000)
    +                .context('NATIVE_APP')
    +                .elementByXPath('//android.widget.TextView[@text="' + contactName.formatted
+ '"]')
    +                .click()
    +                .sleep(5000)
    +                .context(webviewContext)
    +                .elementById('info')
    +                .getAttribute('innerHTML')
    +                .then(function (html) {
    +                    expect(html).toContain('"formatted": "' + contactName.formatted +
'"');
    +                })
    +                .then(win, fail)
    +                .finally(done);
    +        }, 5 * MINUTE);
    +
    +        it('contacts.ui.spec.2 Update an existing contact', function (done) {
    +            var oldContactName = contactsHelper.getContactName('Dooney', 'Evans');
    +            var newContactName = contactsHelper.getContactName('Urist', 'McContact');
    +            enterTest()
    +                .execute(contactsHelper.getAddContactCode(oldContactName.formatted, oldContactName,
"8 800 5553535"))
    +                .sleep(5000)
    +                .execute(contactsHelper.getRenameContactCode(oldContactName.formatted,
newContactName.formatted, newContactName))
    +                .sleep(5000)
    +                .execute(contactsHelper.getPickContactCode())
    +                .sleep(7000)
    +                .context('NATIVE_APP')
    +                .elementByXPath('//android.widget.TextView[@text="' + newContactName.formatted
+ '"]')
    +                .click()
    +                .sleep(5000)
    +                .context(webviewContext)
    +                .elementById('info')
    +                .getAttribute('innerHTML')
    +                .then(function (html) {
    +                    expect(html).toContain('"formatted": "' + newContactName.formatted
+ '"');
    +                })
    +                .then(win, fail)
    +                .finally(done);
    +        }, 5 * MINUTE);
    +
    +        it('contacts.ui.spec.3 Create a contact with no info', function (done) {
    +            var contactName = contactsHelper.getContactName();
    +            enterTest()
    +                .execute(contactsHelper.getAddContactCode())
    +                .sleep(5000)
    +                .execute(contactsHelper.getPickContactCode())
    +                .sleep(7000)
    +                .context('NATIVE_APP')
    +                .elementByXPath('//android.widget.TextView[@text="(No name)"]')
    +                .click()
    +                .sleep(5000)
    +                .context(webviewContext)
    +                .elementById('info')
    +                .getAttribute('innerHTML')
    +                .then(function (html) {
    +                    expect(html).toContain('"displayName": null');
    +                })
    +                .then(win, fail)
    +                .finally(done);
    +        }, 5* MINUTE);
    +
    +        it('contacts.ui.spec.4 Special characters and escape sequences', function (done)
{
    +            var contactName = contactsHelper.getContactName('Н€йромонах',
'ФеофаЊ');
    +            enterTest()
    +                .execute(contactsHelper.getAddContactCode(contactName.formatted, contactName,
'4 5 6789'))
    --- End diff --
    
    I don't know if this string in particular occurs in other places, but please check if
any literals like it can be factored out into constants.


---
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


Mime
View raw message