Using Protractor - contexts() returns {state : pending}

Hi there,

I’m actually trying to test an hybrid application (using protractor and appium). It’s working well on Chrome, iOS safari but i’m facing some problems on iOS native execution.

Our website has a system of public/private sites. User has to login on public to access the private website. The transition between public and private is handled by the wrapper.
By using “‘autoWebview’: true” on protractor’s conf, there is a mismatch when switching between public and private. My login test fills login/password with success, but isn’t able to send more data on private side.

So I’m trying to switch to webview manually, and that’s where a new problem happens. I’m trying to use contexts() to retrieve APP_NATIVE/WEBVIEW but using wdBrowser.contexts() or wdBrowser.currentContext() doesn’t return something.
For example, this code :

    wdBrowser.currentContext().then(function(context) {
           console.log('#context');
           console.log(context);
    });

is never reached. Logging contexts() prints { state: 'pending' } wether I’m using autoWebview or not.
console.log(wdBrowser.contexts());

I can’t see any call tosession/:sessionId/contexts in my appium logs. By manually browsing this URL with chrome’ I’me able to see both contexts listed, APP_NATIVE and WEBVIEW.

Am I doing something wrong ? Why is contexts() is telling me pending ? Is the application initializing or something else ?

Thanks for any suggestion/direction,

Paqman

Is it using WD.js?

Please provide the whole test, but looks like you need to consume the promise.

I’m using wdbridge which uses wd.

exports.config = {
    onPrepare: function() {
        // Handle multiple devices execution
        return browser.getCapabilities().then(function(cap) {
            // iOS uses platformName on simulator
            var platformName = cap.caps_.platformName || cap.caps_.platform;
            var desiredPlatform = cap.caps_.desired || {};

            // Execute tests on mobile real devices
            if (/(android|ios)/i.test(platformName)) {
                browser.params.mobile = desiredPlatform.isMobile !== undefined ? desiredPlatform.isMobile : true;
                browser.params.isNative = true;
                browser.params.iOS = /ios/i.test(platformName);

                var wd = require('wd'),
                    protractor = require('protractor'),
                    wdBridge = require('wd-bridge')(protractor, wd);
                wdBridge.initFromProtractor(exports.config);

                console.log(wdBrowser.contexts());
                // prints  { state: 'pending' }

                wdBrowser.contexts().then(function(context) {
                    // This code is never reached
                    console.log('#context');
                    console.log(context);
                });


            } else {
                browser.params.mobile = false;
                browser.params.isNative = false;
                require('./config/breakpoint').adaptBrowserSize();
            }
        });
    }
}

So you need to return the promise, please refer to any promise tutorial, should be clear once you’ve understand the principle.

Well, I think I’m pretty aware of promises but whatever.

While returning the promise (I now execute tests with a light configuration), I’m stuck on an error during the execution.

onPrepare: function() {
    var wd = require('wd'),
        protractor = require('protractor'),
        wdBridge = require('wd-bridge')(protractor, wd);
    wdBridge.initFromProtractor(exports.config);

    browser.params.mobile = true;
    browser.params.isNative = true;
    browser.params.iOS = true;

    return wdBrowser.contexts().then(function(contexts) {
        console.log('#contexts');
        console.log(contexts);
    });
}

Throw this error on Protractor’s console

Using the selenium server at http://localhost:4723/wd/hub
[launcher] Running 1 instances of WebDriver
[launcher] Error: Error: connect ECONNREFUSED
    at exports._errnoException (util.js:746:11)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1000:19)
[launcher] Process exited with error code 100

On Appium side, I can’t see any weird error or something else. Here is an extract :

info: --> GET /wd/hub/session/1fdd24c5-94e9-4142-ae7e-3945f9495d18 {}
info: [debug] Responding to client with success: {"status":0,"value":{"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"","platform":"MAC","javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,"warnings":{},"desired":{"browserName":"","appium-version":"1.4.0","platformName":"iOS","platformVersion":"8.1.3","deviceName":"MACIT’s iPad","seleniumAddress":"http://localhost:4723/wd/hub","isMobile":true,"isNative":true,"app":"/Users/nudo/Work/bla/PIC/workspace/mybla/test/e2e/native/blaiOSWrapper.zip","udid":"d271dd9020ed0bd356a8d0a5ee70bf102b4893d6","count":1},"appium-version":"1.4.0","platformName":"iOS","platformVersion":"8.1.3","deviceName":"MACIT’s iPad","seleniumAddress":"http://localhost:4723/wd/hub","isMobile":true,"isNative":true,"app":"/Users/nudo/Work/bla/PIC/workspace/mybla/test/e2e/native/blaiOSWrapper.zip","udid":"d271dd9020ed0bd356a8d0a5ee70bf102b4893d6","count":1},"sessionId":"1fdd24c5-94e9-4142-ae7e-3945f9495d18"}
info: <-- GET /wd/hub/session/1fdd24c5-94e9-4142-ae7e-3945f9495d18 200 1.076 ms - 964 {"status":0,"value":{"webStorageEnabled":false,"locationContextEnabled":false,"browserName":"","platform":"MAC","javascriptEnabled":true,"databaseEnabled":false,"takesScreenshot":true,"networkConnectionEnabled":false,"warnings":{},"desired":{"browserName":"","appium-version":"1.4.0","platformName":"iOS","platformVersion":"8.1.3","deviceName":"MACIT’s iPad","seleniumAddress":"http://localhost:4723/wd/hub","isMobile":true,"isNative":true,"app":"/Users/nudo/Work/bla/PIC/workspace/mybla/test/e2e/native/blaiOSWrapper.zip","udid":"d271dd9020ed0bd356a8d0a5ee70bf102b4893d6","count":1},"appium-version":"1.4.0","platformName":"iOS","platformVersion":"8.1.3","deviceName":"MACIT’s iPad","seleniumAddress":"http://localhost:4723/wd/hub","isMobile":true,"isNative":true,"app":"/Users/nudo/Work/bla/PIC/workspace/mybla/test/e2e/native/blaiOSWrapper.zip","udid":"d271dd9020ed0bd356a8d0a5ee70bf102b4893d6","count":1},"sessionId":"1fdd24c5-94e9-4142-ae7e-3945f9495d18"}
info: --> POST /wd/hub/session/1fdd24c5-94e9-4142-ae7e-3945f9495d18/timeouts/async_script {"ms":11000}
info: [debug] Set iOS async script timeout to 11000ms
info: [debug] Responding to client with success: {"status":0,"value":null,"sessionId":"1fdd24c5-94e9-4142-ae7e-3945f9495d18"}
info: <-- POST /wd/hub/session/1fdd24c5-94e9-4142-ae7e-3945f9495d18/timeouts/async_script 200 1.133 ms - 76 {"status":0,"value":null,"sessionId":"1fdd24c5-94e9-4142-ae7e-3945f9495d18"}
info: --> DELETE /wd/hub/session/1fdd24c5-94e9-4142-ae7e-3945f9495d18 {}
info: Shutting down appium session
info: [debug] Stopping ios
info: [debug] Destroying instruments client socket.
info: [debug] Closing socket server.
info: [debug] Instruments socket server was closed
info: [debug] Sending sigterm to instruments
info: [debug] [INST] 2015-06-05 12:46:44 +0000 Stopped: Script was stopped by the user
info: [debug] [INST STDERR] 2015-06-05 14:46:44.572 instruments[84399:4053340] Attempting to set event horizon when core is not engaged, request ignored
info: [debug] [INST STDERR] 2015-06-05 14:46:44.573 instruments[84399:4053340] Attempting to set event horizon when core is not engaged, request ignored
info: [debug] [INST] Instruments Trace Complete (Duration : 7.409178s; Output : /tmp/appium-instruments/instrumentscli0.trace)
info: [debug] [INSTSERVER] Instruments exited with code 0
info: [debug] Cleaning up after instruments exit
info: [debug] Stopping iOS log capture
info: [debug] Running ios sim reset flow
info: [debug] Killing the simulator process
info: [debug] Killing any other simulator daemons
info: [debug] Killall iOS Simulator

I tried to call contexts() on wdBrowser, but also currentContext() and wdBrowser.setAsyncScriptTimeout(12000) but the’yre all throwing the error.

You need to figure out what called the DELETE.

Any solution to resolve?