protractor icon indicating copy to clipboard operation
protractor copied to clipboard

browser.takeScreenshot() does not work in a hybrid android app

Open MarvKaiser opened this issue 9 years ago • 7 comments

Hi,

I am developing a hybrid android app based on ionic and cordova. When I am testing the .apk, the browser. takeScreenshot() function does not work. Also the protractor-jasmine2-screenshot-reporter returns an error, when I try to screenshot the page after a failed test. Other tests like clicking buttons or check for elements are working fine. ONLY the browser.takeSreenshot() does not work.

For testing on my real Android device, I am using Appium Version 1.4.16.1.

My code:

    it('should be able to display Google and take a screenshot', function (done) {
        browser.get('http://www.google.com');
        browser.sleep(2000);
        browser.takeScreenshot().then(function (png) {
            console.log('browser.takeScreenshot()');
            var stream = fs.createWriteStream('screenshot.png');
            stream.write(new Buffer(png, 'base64'));
            stream.end();
            done();
        });
    });

The console output is:

> C:\Projekte\WifiWizardTestApp>protractor protractor.config.js --specs tests/browse.spec.js
> Report destination:   tests\results\screenshots\failed-tests-report.html
> [16:39:17] I/hosted - Using the selenium server at http://localhost:4723/wd/hub
> [16:39:17] I/launcher - Running 1 instances of WebDriver
> Started
> FA Jasmine spec timed out. Resetting the WebDriver Control Flow.
> F[16:40:57] E/launcher - this.expectationResultFactory is not a function
> [16:40:57] E/launcher - TypeError: this.expectationResultFactory is not a function
>     at Suite.onException (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:2219:48)
>     at QueueRunner.onException (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:2330:28)
>     at onException (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1924:12)
>     at Timeout._onTimeout (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1910:11)
>     at ontimeout (timers.js:365:14)
>     at tryOnTimeout (timers.js:237:5)
>     at Timer.listOnTimeout (timers.js:207:5)
> [16:40:57] E/launcher - Process exited with error code 199

My protractor.config.js

var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');

var reporter = new HtmlScreenshotReporter({
    dest: './tests/results/screenshots',
    filename: 'failed-tests-report.html',
    reportOnlyFailedSpecs: true,
    captureOnlyFailedSpecs: true,
    reportTitle: 'Failed Tests Report'
});

exports.config = {

    seleniumAddress : 'http://localhost:4723/wd/hub',

    // Reference: https://github.com/appium/sample-code/blob/master/sample-code/examples/node/helpers/caps.js
    capabilities : {
        // needed by local appium
        platformVersion : '',
        platformName : '',
        deviceName : '',
        // needed by protractor
        browserName : '',
        autoWebview : true,
        // CHANGE THIS TO YOUR ABSOLUTE PATH OR SET IT IN APPIUM CLIENT
        app : 'C:/Projekte/WifiWizardTestApp/platforms/android/build/outputs/apk/android-debug.apk',
        newCommandTimeout : 60
    },

    // configuring wd in onPrepare
    // wdBridge helps to bridge wd driver with other selenium clients
    // See https://github.com/sebv/wd-bridge/blob/master/README.md
    onPrepare : function () {
        // HtmlReporter: Assign the test reporter to each running instance
        jasmine.getEnv().addReporter(reporter);
    },

    //HtmlScreenshotsReporter Section

    // Setup the report before any tests start
    beforeLaunch: function() {
        return new Promise(function(resolve){
            reporter.beforeLaunch(resolve);
        });
    },

    // Close the report after all tests finish
    afterLaunch: function(exitCode) {
        return new Promise(function(resolve){
            reporter.afterLaunch(resolve.bind(this, exitCode));
        });
    }
};

Specs

  • Node Version: v6.9.1
  • Protractor Version: 4.0.14
  • Angular Version: 1.5.3
  • Appium Version: 1.4.16.1
  • Browser(s): Cordova Webview (Chromium(?))
  • Operating System and Version [Test Device] Android 6.0.1 - Galaxy S6
  • Operating System and Version [Development Device] Windows 10 Enterprise 64 Bit
  • Steps to reproduce the bug

My guess and further information

Intresstingly, when I change browserName : '' to browserName : 'Chrome', then the Google Chrome browser on my device opens up and www.google.com is displayed. A screenshot is taken from the code (browser.takeScreenshot().then...) AND from the protractor-jasmine2-screenshot-reporter. So this means, the code is working perfectly in the mobile browser, but not in the hybrid app.

My guess is, that there is a bug in the .takeScreenshot implementation, or (hopefully) I just miss something important. I searched a whole day for a similar issue, but did not find any.

Please help me, because protractor is awesome for testing my hybrid app. 👍

MarvKaiser avatar Jan 23 '17 16:01 MarvKaiser

Hi

There is a possibility in the hybrid app to disable screenshots (cordova I think), please check the specs for that.

Secondly I think this is a third party software you are using, so maybe this is not the correct platform to ask the question / issue (don't get me wrong)

Good luck with it

Grtz

Wim

wswebcreation avatar Jan 23 '17 19:01 wswebcreation

Thanks for your quick response 😄 ,

I have not disabled the possibility to take screenshots. I can make screenshots with my mobile device. I also tested to screenshot an other blank ionic app with the same result, that I can not take a screenshot with protractor using this code:

browser.takeScreenshot().then(function (png) {
            console.log('browser.takeScreenshot()');
            var stream = fs.createWriteStream('screenshot.png');
            stream.write(new Buffer(png, 'base64'));
            stream.end();
            done();
        });

The third party tool I mentioned did I just use, to make sure, that the code above is really working (and it does work ironically but not in my hybrid android app). So I removed the 3rd party tool, but still the same result...

Current Code and Steps to Reproduce the Bug

The blank App has no Plugins installed or something similar, which could prevent the ability to take a screenshot.

config.js:

exports.config = {

    seleniumAddress : 'http://localhost:4723/wd/hub',

    // Reference: https://github.com/appium/sample-code/blob/master/sample-code/examples/node/helpers/caps.js
    capabilities : {
        // needed by local appium
        platformVersion : '',
        platformName : '',
        deviceName : '',
        // needed by protractor
        browserName : '',
        autoWebview : true,
        // CHANGE THIS TO YOUR ABSOLUTE PATH OR SET IT IN APPIUM CLIENT
        app : 'C:/Projekte/WifiWizardTestApp/platforms/android/build/outputs/apk/android-debug.apk',
        newCommandTimeout : 60
    },

    //needed for local appium
    baseUrl : 'http://localhost:8080',

    //configuring wd in onPrepare
    //wdBridge helps to bridge wd driver with other selenium clients
    //See https://github.com/sebv/wd-bridge/blob/master/README.md
    onPrepare : function () {
        var wd = require('wd');
        var protractor = require('protractor');
        var wdBridge = require('wd-bridge')(protractor, wd);
        wdBridge.initFromProtractor(exports.config);
    },
};

spec.js

var fs = require('fs');

describe('Testing the browse state', function () {

    it('should be able to take a screenshot', function (done) {
        browser.sleep(2000);
        browser.takeScreenshot().then(function (png) {
            console.log('browser.takeScreenshot()');
            var stream = fs.createWriteStream('screenshot.png');
            stream.write(new Buffer(png, 'base64'));
            stream.end();
            done();
        });
    });

    it('should be able to take an other screenshot', function () {
        browser.takeScreenshot().then(function (png) {
            console.log('browser.takeScreenshot()');
            var stream = fs.createWriteStream('screenshot2.png');
            stream.write(new Buffer(png, 'base64'));
            stream.end();
        });
    });
});

I tried to call the done(); function in the first test (suggested by this StackOverflow Post). But that did not work either.

Console Output:

C:\Projekte\WifiWizardTestApp>protractor protractor.config.js --specs tests/browse.spec.js
[09:27:44] I/hosted - Using the selenium server at http://localhost:4723/wd/hub
[09:27:44] I/launcher - Running 1 instances of WebDriver
Started
A Jasmine spec timed out. Resetting the WebDriver Control Flow.
FA Jasmine spec timed out. Resetting the WebDriver Control Flow.
F

Failures:
1) Testing the browse state should be able to take a screenshot
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        at ontimeout (timers.js:365:14)
        at tryOnTimeout (timers.js:237:5)
        at Timer.listOnTimeout (timers.js:207:5)

2) Testing the browse state should be able to take an other screenshot
  Message:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
  Stack:
    Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
        at ontimeout (timers.js:365:14)
        at tryOnTimeout (timers.js:237:5)
        at Timer.listOnTimeout (timers.js:207:5)

2 specs, 2 failures
Finished in 60.04 seconds

I have still no clue, what prevents the screenshot...

Additional Specs

Cordova: 6.4.0 Ionic: 2.1.13

MarvKaiser avatar Jan 24 '17 08:01 MarvKaiser

Could also be Appium that blocks it, protractor is connecting to the device through Appium.

Try the following, change context to 'NATIVE' and create a screenshot with the wd-api.

Still I do wonder if this is the platform to ask this question / post this problem.

wswebcreation avatar Jan 24 '17 10:01 wswebcreation

I'm using the Appium desktop client and when I open a browser on my Android device and then take a screenshot of any page it works perfectly. All other tests work fine in my app, only the screenshot does not work.

I updated my chromedriver from Appium and tested again without success. The 'NATIVE' context did not work either. Looks like the wd-api does not return the screenshot when inside the hybrid app.

Looks like a selenium bug maybe.

Update

I installed my blank app on an older android device (Android 4.4.2) and tested the same code with the same result, that I can not make a screenshot. But I got a more precise error message at least:

C:\Projekte\WifiWizardTestApp>protractor protractor.config.js --specs tests/browse.spec.js
[15:15:49] I/hosted - Using the selenium server at http://localhost:4723/wd/hub
[15:15:49] I/launcher - Running 1 instances of WebDriver
Started
FF

Failures:
1) Testing the browse state should be able to take a screenshot
  Message:
    Failed: unknown error: unhandled inspector error: {"code":-32603,"message":"Unable to capture screenshot"}
      (Session info: webview=30.0.0.0)
      (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64)
  Stack:
    WebDriverError: unknown error: unhandled inspector error: {"code":-32603,"message":"Unable to capture screenshot"}
      (Session info: webview=30.0.0.0)
      (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64)
        at WebDriverError (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:27:5)
        at Object.checkLegacyResponse (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:505:15)
        at parseHttpResponse (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:509:13)
        at doSend.then.response (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:440:13)
        at process._tickCallback (internal/process/next_tick.js:103:7)
    From: Task: WebDriver.takeScreenshot()
        at WebDriver.schedule (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:816:17)
        at WebDriver.takeScreenshot (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:1092:17)
        at ProtractorBrowser.to.(anonymous function) [as takeScreenshot] (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\lib\browser.ts:94:25)
        at Object.<anonymous> (C:\Projekte\WifiWizardTestApp\tests\browse.spec.js:14:17)
        at C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:98:15
        at new ManagedPromise (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1067:7)
        at controlFlowExecute (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:87:18)
        at TaskQueue.execute_ (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2970:14)
        at TaskQueue.executeNext_ (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2953:27)
        at asyncRun (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2813:27)
    From: Task: Run it("should be able to take a screenshot") in control flow
        at Object.<anonymous> (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:86:14)
    From asynchronous test:
    Error
        at Suite.<anonymous> (C:\Projekte\WifiWizardTestApp\tests\browse.spec.js:8:5)
        at Object.<anonymous> (C:\Projekte\WifiWizardTestApp\tests\browse.spec.js:4:1)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)

2) Testing the browse state should be able to take an other screenshot
  Message:
    Failed: unknown error: unhandled inspector error: {"code":-32603,"message":"Unable to capture screenshot"}
      (Session info: webview=30.0.0.0)
      (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64)
  Stack:
    WebDriverError: unknown error: unhandled inspector error: {"code":-32603,"message":"Unable to capture screenshot"}
      (Session info: webview=30.0.0.0)
      (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.10586 x86_64)
        at WebDriverError (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:27:5)
        at Object.checkLegacyResponse (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\error.js:505:15)
        at parseHttpResponse (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:509:13)
        at doSend.then.response (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\http.js:440:13)
        at process._tickCallback (internal/process/next_tick.js:103:7)
    From: Task: WebDriver.takeScreenshot()
        at WebDriver.schedule (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:816:17)
        at WebDriver.takeScreenshot (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\webdriver.js:1092:17)
        at ProtractorBrowser.to.(anonymous function) [as takeScreenshot] (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\lib\browser.ts:94:25)
        at Object.<anonymous> (C:\Projekte\WifiWizardTestApp\tests\browse.spec.js:26:17)
        at C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:102:25
        at new ManagedPromise (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1067:7)
        at controlFlowExecute (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:87:18)
        at TaskQueue.execute_ (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2970:14)
        at TaskQueue.executeNext_ (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2953:27)
        at asyncRun (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2860:25)
    From: Task: Run it("should be able to take an other screenshot") in control flow
        at Object.<anonymous> (C:\Users\mkaiser\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:86:14)
    From asynchronous test:
    Error
        at Suite.<anonymous> (C:\Projekte\WifiWizardTestApp\tests\browse.spec.js:24:5)
        at Object.<anonymous> (C:\Projekte\WifiWizardTestApp\tests\browse.spec.js:4:1)
        at Module._compile (module.js:570:32)
        at Object.Module._extensions..js (module.js:579:10)
        at Module.load (module.js:487:32)
        at tryModuleLoad (module.js:446:12)

2 specs, 2 failures
Finished in 2.258 seconds
[15:17:49] I/launcher - 0 instance(s) of WebDriver still running
[15:17:49] I/launcher -  #01 failed 2 test(s)
[15:17:49] I/launcher - overall: 2 failed spec(s)
[15:17:49] E/launcher - Process exited with error code 1

Futher steps

I think, I'll open a StackOverflow question as well, because I think it is not a protractor bug, more likely a webdriver or appium bug... Thanks for your help.

When you come up with an other idea what the problem could be, no matter how unlikly it is, please comment it here...

MarvKaiser avatar Jan 24 '17 14:01 MarvKaiser

You're welcome. I think it's also better to close the issue then.

Grtz

Wim

wswebcreation avatar Jan 24 '17 15:01 wswebcreation

Okay, I opened a StackOverflow Question and close the issue.

MarvKaiser avatar Jan 24 '17 16:01 MarvKaiser

👍

wswebcreation avatar Jan 24 '17 16:01 wswebcreation