Protractor Console output
As protractor is built on top of webdriver JS, we should resolve promise while printing to console output.
so below statement doesn’t work
console.log("Browser title :- " + browser.gettitle());
instead, you can use
browser.getTitle().then(function(txt){
console.log("Browser Title :- " + txt);
});
watch details here
Protractor log to file
As protractor is a node package, there are several loggers are available, in which log4js is one of them
log4js.json
{ "appenders": { "console" : { "type": "console", "category": "console" }, "file" : { "category": "test-file-appender", "type": "file", "filename": "log_file.log", "maxLogSize": 10240, "backups": 3, "pattern": "%d{dd/MM hh:mm} %-5p %m" } }, "categories": { "default" :{"appenders": ["console", "file"], "level": "DEBUG"}, "file" : {"appenders": ["file"], "level": "DEBUG"} } }
log4jsconfig.ts
export class log4jsconfig{ static Log(): any { var log4js = require('log4js'); log4js.configure('./config/log4js.json'); //var logger = log4js.getLogger(); //for both console and file let log = log4js.getLogger("default"); return log; } }
test script
import { browser, element, by } from "protractor"; import { log4jsconfig } from '../config/log4jsconfig' describe("Calculator test", function(){ beforeEach(function(){ browser.get("https://juliemr.github.io/protractor-demo/"); }) it("Launch url check", function(){ expect(browser.getTitle()).toContain("Super"); //console.log("Browser Title :-" + browser.getTitle()); let browserTitle = browser.getTitle(); browserTitle.then(function(txt){ console.log("Browser Title :-" + txt); log4jsconfig.Log().debug("Browser Title :- " + txt); }); }) it("Add 2 numbers", function(){ element(by.model("first")).sendKeys("12"); element(by.model("second")).sendKeys("13"); element(by.id("gobutton")).click(); browser.sleep(3000); expect<any>(element(by.xpath("//table/tbody/tr[1]/td[3]")).getText()).toEqual('25'); }) })
thanks for the details. it really very helpful however i’m getting below syntax error
SyntaxError: D:\2.0\Jest_With_Puppeteer\classsed-jest-part2\Config\log4jsconfig.test.js: Unexpected token, expected “{” (2:16)
Could you pls advise.