jeudi 25 février 2016

How to run scripts after deviceready event?

I've faced with a problem when running jasmine tests for Cordova application: I have an html page (Jasmine 2.4 SpecRunner.html) with scripts in a <head> to be loaded (plugins, services, controllers, etc) and the last included file in a <head> is a test file. In a body I have a deviceready event listener. But when I run tests against android emulator, my tests is always executed before deviceready, and it doesn't see plugins(i.e. error "device is not defined").

So the question is: how to delay execution of test files exactly after deviceready event?

My SpecRunner.html:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <meta name="msapplication-tap-highlight" content="no" />
        <meta http-equiv="Content-Security-Policy" content="default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'">
        <title>SportsWellness</title>


        <script src="scripts/winstore-jscompat.js"></script>

    <link rel="shortcut icon" type="image/png" href="jasmine/lib/jasmine-2.4.1/jasmine_favicon.png">
    <link rel="stylesheet" href="jasmine/lib/jasmine-2.4.1/jasmine.css">

    <!-- Jasmine Modules -->

    <script src="jasmine/lib/jasmine-2.4.1/jasmine.js"></script>
    <script src="jasmine/lib/jasmine-2.4.1/jasmine-html.js"></script>
    <script src="jasmine/lib/jasmine-2.4.1/boot.js"></script>

    <!-- SportsWellness references -->
    <link rel="stylesheet" href="css/app.css" />

    <!-- AngularJS modules -->
    <script src="libs/angular.min.js"></script>
    <script src="libs/angular-resource.min.js"></script>
    <script src="libs/angular-ui-router.js"></script>
    <script src="libs/angular-touch.min.js"></script>
    <script src="libs/angular-animate.min.js"></script>
    <script src="libs/angular-mocks.js"></script>

    <!-- Cordova reference, this is added to app when it's built. -->
    <script src="cordova.js"></script>

    <!-- Include Application scripts -->
    <script type="text/javascript" src="js/app.js"></script>
    <script type="text/javascript" src="js/router.js"></script>
    <script type="text/javascript" src="js/platformOverrides.js"></script>

    <!-- Include Controllers-->
    <!-- Script tags  -->

    <!-- Include Services -->
    <!-- Script tags  -->

    <!-- Include Directives -->
    <!-- Script tags  -->

    <!-- Include Configuration Scripts -->
    <!-- Script tags  -->

    <!-- Include specs to run -->
    <script src="tests/ServicesTests/ReminderService.spec.js"></script>

</head>
    <body>    
    <script type="application/javascript">
        document.addEventListener('deviceready', function () {
            navigator.splashscreen.hide();
        }, false);
    </body>
    </html>

Aucun commentaire:

Enregistrer un commentaire