ios.spec.js 19.1 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
/*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 Paramedic test runner
// you can find it here: https://github.com/apache/cordova-paramedic/
// it is not necessary to do a full CI setup to run these tests
// just run "node cordova-paramedic/main.js --platform ios --plugin cordova-plugin-camera"

'use strict';

var wdHelper = global.WD_HELPER;
var screenshotHelper = global.SCREENSHOT_HELPER;
var isDevice = global.DEVICE;
var cameraConstants = require('../../www/CameraConstants');
var cameraHelper = require('../helpers/cameraHelper');

var MINUTE = 60 * 1000;
var DEFAULT_WEBVIEW_CONTEXT = 'WEBVIEW_1';
var PROMISE_PREFIX = 'appium_camera_promise_';
var CONTEXT_NATIVE_APP = 'NATIVE_APP';

describe('Camera tests iOS.', function () {
    var driver;
    var webviewContext = DEFAULT_WEBVIEW_CONTEXT;
    // promise count to use in promise ID
    var promiseCount = 0;
    // going to set this to false if session is created successfully
    var failedToStart = true;
    // points out which UI automation to use
    var isXCUI = false;
    // spec counter to restart the session
    var specsRun = 0;

    function getNextPromiseId() {
        promiseCount += 1;
        return getCurrentPromiseId();
    }

    function getCurrentPromiseId() {
        return PROMISE_PREFIX + promiseCount;
    }

    function gracefullyFail(error) {
        fail(error);
        return driver
            .quit()
            .then(function () {
                return getDriver();
            });
    }

    // generates test specs by combining all the specified options
    // you can add more options to test more scenarios
    function generateOptions() {
        var sourceTypes = cameraConstants.PictureSourceType;
        var destinationTypes = cameraConstants.DestinationType;
        var encodingTypes = cameraConstants.EncodingType;
        var allowEditOptions = [ true, false ];
        var correctOrientationOptions = [ true, false ];

        return cameraHelper.generateSpecs(sourceTypes, destinationTypes, encodingTypes, allowEditOptions, correctOrientationOptions);
    }

    function usePicture() {
        return driver
            .elementByXPath('//*[@label="Use"]')
            .click()
            .fail(function () {
                if (isXCUI) {
                    return driver
                        .waitForElementByAccessibilityId('Choose', MINUTE / 3)
                        .click();
                }
                // For some reason "Choose" element is not clickable by standard Appium methods on iOS <= 9
                return wdHelper.tapElementByXPath('//UIAButton[@label="Choose"]', driver);
            });
    }

    function clickPhoto() {
        if (isXCUI) {
            // iOS >=10
            return driver
                .context(CONTEXT_NATIVE_APP)
                .elementsByXPath('//XCUIElementTypeCell')
                .then(function(photos) {
                    if (photos.length == 0) {
                        return driver
                            .sleep(0) // driver.source is not a function o.O
                            .source()
                            .then(function (src) {
                                console.log(src);
                                gracefullyFail('Couldn\'t find an image to click');
                            });
                    }
                    // intentionally clicking the second photo here
                    // the first one is not clickable for some reason
                    return photos[1].click();
                });
        }
        // iOS <10
        return driver
            .elementByXPath('//UIACollectionCell')
            .click();
    }

    function getPicture(options, cancelCamera, skipUiInteractions) {
        var promiseId = getNextPromiseId();
        if (!options) {
            options = {};
        }

        return driver
            .context(webviewContext)
            .execute(cameraHelper.getPicture, [options, promiseId])
            .context(CONTEXT_NATIVE_APP)
            .then(function () {
                if (skipUiInteractions) {
                    return;
                }
                if (options.hasOwnProperty('sourceType') && options.sourceType === cameraConstants.PictureSourceType.PHOTOLIBRARY) {
                    return driver
                        .waitForElementByAccessibilityId('Camera Roll', MINUTE / 2)
                        .click()
                        .then(function () {
                            return clickPhoto();
                        })
                        .then(function () {
                            if (!options.allowEdit) {
                                return driver;
                            }
                            return usePicture();
                        });
                }
                if (options.hasOwnProperty('sourceType') && options.sourceType === cameraConstants.PictureSourceType.SAVEDPHOTOALBUM) {
                    return clickPhoto()
                        .then(function () {
                            if (!options.allowEdit) {
                                return driver;
                            }
                            return usePicture();
                        });
                }
                if (cancelCamera) {
                    return driver
                        .waitForElementByAccessibilityId('Cancel', MINUTE / 2)
                        .click();
                }
                return driver
                    .waitForElementByAccessibilityId('Take Picture', MINUTE / 2)
                    .click()
                    .waitForElementByAccessibilityId('Use Photo', MINUTE / 2)
                    .click();
            })
            .fail(fail);
    }

    // checks if the picture was successfully taken
    // if shouldLoad is falsy, ensures that the error callback was called
    function checkPicture(shouldLoad, options) {
        if (!options) {
            options = {};
        }
        return driver
            .context(webviewContext)
            .setAsyncScriptTimeout(MINUTE / 2)
            .executeAsync(cameraHelper.checkPicture, [getCurrentPromiseId(), options])
            .then(function (result) {
                if (shouldLoad) {
                    if (result !== 'OK') {
                        fail(result);
                    }
                } else if (result.indexOf('ERROR') === -1) {
                    throw 'Unexpected success callback with result: ' + result;
                }
            });
    }

    // takes a picture with the specified options
    // and then verifies it
    function runSpec(options, done, pending) {
        if (options.sourceType === cameraConstants.PictureSourceType.CAMERA && !isDevice) {
            pending('Camera is not available on iOS simulator');
        }
        checkSession(done);
        specsRun += 1;
        return driver
            .then(function () {
                return getPicture(options);
            })
            .then(function () {
                return checkPicture(true, options);
            })
            .fail(gracefullyFail);
    }

    function getDriver() {
        failedToStart = true;
        driver = wdHelper.getDriver('iOS');
        return wdHelper.getWebviewContext(driver)
            .then(function(context) {
                webviewContext = context;
                return driver.context(webviewContext);
            })
            .then(function () {
                return wdHelper.waitForDeviceReady(driver);
            })
            .then(function () {
                return wdHelper.injectLibraries(driver);
            })
            .sessionCapabilities()
            .then(function (caps) {
                var platformVersion = parseFloat(caps.platformVersion);
                isXCUI = platformVersion >= 10.0;
            })
            .then(function () {
                var options = {
                    quality: 50,
                    allowEdit: false,
                    sourceType: cameraConstants.PictureSourceType.SAVEDPHOTOALBUM,
                    saveToPhotoAlbum: false,
                    targetWidth: 210,
                    targetHeight: 210
                };
                return driver
                    .then(function () { return getPicture(options, false, true); })
                    .context(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 applications-under-test already had photos permissions granted to it
                        // b) Appium's autoAcceptAlerts capability is provided (and functioning)
                    })
                    .elementByAccessibilityId('Cancel', 10000)
                    .click();
            })
            .then(function () {
                failedToStart = false;
            });
    }

    function checkSession(done) {
        if (failedToStart) {
            fail('Failed to start a session');
            done();
        }
    }

    it('camera.ui.util configure driver and start a session', function (done) {
        getDriver()
            .fail(fail)
            .done(done);
    }, 15 * MINUTE);

    describe('Specs.', function () {
        afterEach(function (done) {
            if (specsRun >= 15) {
                specsRun = 0;
                // we need to restart the session regularly because for some reason
                // when running against iOS 10 simulator on SauceLabs,
                // Appium cannot handle more than ~20 specs at one session
                // the error would be as follows:
                // "Could not proxy command to remote server. Original error: Error: connect ECONNREFUSED 127.0.0.1:8100"
                checkSession(done);
                return driver
                    .quit()
                    .then(function () {
                        return getDriver();
                    })
                    .done(done);
            } else {
                done();
            }
        }, 15 * MINUTE);

        // getPicture() with mediaType: VIDEO, sourceType: PHOTOLIBRARY
        it('camera.ui.spec.1 Selecting only videos', function (done) {
            checkSession(done);
            specsRun += 1;
            var options = { sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
                            mediaType: cameraConstants.MediaType.VIDEO };
            driver
                // skip ui unteractions
                .then(function () { return getPicture(options, false, true); })
                .waitForElementByXPath('//*[contains(@label,"Videos")]', MINUTE / 2)
                .elementByAccessibilityId('Cancel')
                .click()
                .fail(gracefullyFail)
                .done(done);
        }, 7 * MINUTE);

        // getPicture(), then dismiss
        // wait for the error callback to be called
        it('camera.ui.spec.2 Dismissing the camera', function (done) {
            checkSession(done);
            if (!isDevice) {
                pending('Camera is not available on iOS simulator');
            }
            specsRun += 1;
            var options = { sourceType: cameraConstants.PictureSourceType.CAMERA,
                            saveToPhotoAlbum: false };
            driver
                .then(function () {
                    return getPicture(options, true);
                })
                .then(function () {
                    return checkPicture(false);
                })
                .fail(gracefullyFail)
                .done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.3 Verifying target image size, sourceType=CAMERA', function (done) {
            var options = {
                quality: 50,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.CAMERA,
                saveToPhotoAlbum: false,
                targetWidth: 210,
                targetHeight: 210
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.4 Verifying target image size, sourceType=SAVEDPHOTOALBUM', function (done) {
            var options = {
                quality: 50,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.SAVEDPHOTOALBUM,
                saveToPhotoAlbum: false,
                targetWidth: 210,
                targetHeight: 210
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.5 Verifying target image size, sourceType=PHOTOLIBRARY', function (done) {
            var options = {
                quality: 50,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
                saveToPhotoAlbum: false,
                targetWidth: 210,
                targetHeight: 210
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.6 Verifying target image size, sourceType=CAMERA, destinationType=FILE_URL', function (done) {
            // remove this line if you don't mind the tests leaving a photo saved on device
            pending('Cannot prevent iOS from saving the picture to photo library');

            var options = {
                quality: 50,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.CAMERA,
                destinationType: cameraConstants.DestinationType.FILE_URL,
                saveToPhotoAlbum: false,
                targetWidth: 210,
                targetHeight: 210
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.7 Verifying target image size, sourceType=SAVEDPHOTOALBUM, destinationType=FILE_URL', function (done) {
            var options = {
                quality: 50,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.SAVEDPHOTOALBUM,
                destinationType: cameraConstants.DestinationType.FILE_URL,
                saveToPhotoAlbum: false,
                targetWidth: 210,
                targetHeight: 210
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.8 Verifying target image size, sourceType=PHOTOLIBRARY, destinationType=FILE_URL', function (done) {
            var options = {
                quality: 50,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
                destinationType: cameraConstants.DestinationType.FILE_URL,
                saveToPhotoAlbum: false,
                targetWidth: 210,
                targetHeight: 210
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.9 Verifying target image size, sourceType=CAMERA, destinationType=FILE_URL, quality=100', function (done) {
            // remove this line if you don't mind the tests leaving a photo saved on device
            pending('Cannot prevent iOS from saving the picture to photo library');

            var options = {
                quality: 100,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.CAMERA,
                destinationType: cameraConstants.DestinationType.FILE_URL,
                saveToPhotoAlbum: false,
                targetWidth: 305,
                targetHeight: 305
            };
            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.10 Verifying target image size, sourceType=SAVEDPHOTOALBUM, destinationType=FILE_URL, quality=100', function (done) {
            var options = {
                quality: 100,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.SAVEDPHOTOALBUM,
                destinationType: cameraConstants.DestinationType.FILE_URL,
                saveToPhotoAlbum: false,
                targetWidth: 305,
                targetHeight: 305
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        it('camera.ui.spec.11 Verifying target image size, sourceType=PHOTOLIBRARY, destinationType=FILE_URL, quality=100', function (done) {
            var options = {
                quality: 100,
                allowEdit: false,
                sourceType: cameraConstants.PictureSourceType.PHOTOLIBRARY,
                destinationType: cameraConstants.DestinationType.FILE_URL,
                saveToPhotoAlbum: false,
                targetWidth: 305,
                targetHeight: 305
            };

            runSpec(options, done, pending).done(done);
        }, 7 * MINUTE);

        // combine various options for getPicture()
        generateOptions().forEach(function (spec) {
            it('camera.ui.spec.12.' + spec.id + ' Combining options. ' + spec.description, function (done) {
                // remove this check if you don't mind the tests leaving a photo saved on device
                if (spec.options.sourceType === cameraConstants.PictureSourceType.CAMERA &&
                    spec.options.destinationType === cameraConstants.DestinationType.NATIVE_URI) {
                    pending('Skipping: cannot prevent iOS from saving the picture to photo library and cannot delete it. ' +
                        'For more info, see iOS quirks here: https://github.com/apache/cordova-plugin-camera#ios-quirks-1');
                }

                runSpec(spec.options, done, pending).done(done);
            }, 7 * MINUTE);
        });

    });

    it('camera.ui.util Destroy the session', function (done) {
        checkSession(done);
        driver
            .quit()
            .done(done);
    }, 5 * MINUTE);
});