automated testing – Screenshot not found error in protractor

I am getting broken image for screenshot. Unable to take screenshot in protractor framework. Please anyone help on this.

My code:
testconfig.js file

var fs = require('fs-extra');
var today = new Date(),
    timeStamp = today.getMonth() + 1 + '-' + today.getDate() + '-' + today.getFullYear() + '-' + today.getHours() + 'h-' + today.getMinutes() + 'm';
console.log(timeStamp)

exports.config = {
    directConnect: true,
    // seleniumAddress: 'http://localhost:4444/wd/hub',
    capabilities: {
        'browserName': 'chrome',//firefox
    },

       suites: {
        regression: '../specs/sample.js',

    },
    allScriptsTimeout: 50000,

    jasmineNodeOpts: {
        onComplete: null,
        isVerbose: false,
        showColors: true,
        includeStackTrace: true,
        defaultTimeoutInterval: 999999,
    },
    framework: 'jasmine2',

    onPrepare: function () {
        browser.manage().window().maximize();
        browser.waitForAngularEnabled(false);
        // var ChercherTechJasmineReporter = reporter.ChercherTechJasmineReporter;
        // jasmine.getEnv().addReporter(new ChercherTechJasmineReporter({
        //     screenshotOnFail :false,
        //     showSkipped:false,
        //     browser:browser,
        //     showLineChart:true
        // }));
        const SpecReporter = require('jasmine-spec-reporter').SpecReporter;
        jasmine.getEnv().addReporter(new SpecReporter({
            spec: {
                // displayStacktrace: true
                displayStacktrace: false
            }
        }));
        //Getting XML report
        var jasmineReporters = require('jasmine-reporters');
        jasmine.getEnv().addReporter(new jasmineReporters.JUnitXmlReporter({
            consolidateAll: true,
            filePrefix: 'guitest-xmloutput',
            savePath: '.'
        }));

        fs.emptyDir("https://sqa.stackexchange.com/", function (err) {
            console.log("Error message=",err);
        });

        jasmine.getEnv().addReporter({
            specDone: function(result) {
                // if (result.status == 'failed') {
                browser.getCapabilities().then(function (caps) {
                    var browserName = caps.get('browserName');
                    browser.takeScreenshot().then(function (png) {
                        // var stream = fs.createWriteStream('screenshots/' + browserName + '-' + result.fullName+ '.png');
                        var stream = fs.createWriteStream('./' + browserName + '-' + result.fullName+ '.png');
                        // var stream = fs.createWriteStream('./' + '-' + result.fullName+ '.png');
                        stream.write(new Buffer(png, 'base64'));
                        stream.end();
                    });
                });
                // }
            }
        });
    },
    // onComplete:function(exitcode){
    //     reporter.write_consolidated()
    // },
    //
    // afterLaunch:function(exitcode){
    //     return new Promise(function(resolve){
    //         reporter.processResults()
    //         return 0
    //     })
    // },
    onComplete: function() {
        var browserName, browserVersion;
        var capsPromise = browser.getCapabilities();

        capsPromise.then(function (caps) {
            browserName = caps.get('browserName');
            browserVersion = caps.get('version');
            platform = caps.get('platform');

            var HTMLReport = require('protractor-html-reporter-2');

            testConfig = {
                reportTitle: 'TAP Test Execution Report',
                // outputPath: './new/test/scripts/TestReports',
                outputPath: './',
                outputFilename: 'ProtractorTestReport'+timeStamp,
                screenshotPath: './',
                testBrowser: browserName,
                browserVersion: browserVersion,
                modifiedSuiteName: false,
                screenshotsOnlyOnFailure: false,
                testPlatform: platform
            };
            new HTMLReport().from('guitest-xmloutput.xml', testConfig);
        });
    },

    SELENIUM_PROMISE_MANAGER: true
};

spec-sample.js file

describe('Protractor - takeScreenshot()', function () {
    it('Code script to use takeScreenshot() in protractor', function () {
        browser.get("https://keeplearners.blogspot.com/2018/03/Angular-elements.html");
        var textField = element(by.model('name'));
        textField.sendKeys("Entered sample text")      
    });
} )

output:

enter image description here

Leave a Comment