量角器 afterEach 将 browser.manage().logs() 提取到辅助函数
Protractor afterEach extract browser.manage().logs() to a helper function
我的总体目标是将 browser.manage().logs() 提取到辅助函数中。我正在通过在测试中的 afterEach
调用中包含 browser.manage().logs()
功能来实现这一目标。
我知道我的测试抛出一个控制台错误。
browser.manage().logs()
的内联版本将打印错误。 afterEach
和 checkConsole
都不会打印日志。
我需要做什么来解决这个问题?
内联 browser.manage().logs()
是否正在检索错误并擦除日志?我已经尝试了将它们全部注释掉的所有组合,唯一有效的是内联版本。
我还配置了 protractor-screenshoter-plugin
,它生成的报告显示 Show browsers logs
按钮下的控制台错误。这是不是以某种方式抓住错误并阻止 browser.manage().logs()
得到它?
conf.ts
import { browser, Config } from 'protractor';
export let config: Config = {
plugins: [{
package: 'protractor-screenshoter-plugin',
screenshotPath: './protractor/screenshots',
screenshotOnExpect: 'failure+success',
screenshotOnSpec: 'none',
withLogs: true,
writeReportFreq: 'asap',
imageToAscii: 'none',
clearFoldersBeforeTest: true
}],
capabilities: {
'browserName': 'chrome',
chromeOptions: {
args: [ '--disable-gpu', '--window-size=1440x900', '--headless'],
},
'moz:firefoxOptions': {
args: [ '-headless' ],
},
'loggingPrefs' : {"driver": "ALL", "server": "ALL", "browser": "ALL"}
},
directConnect: true,
framework: 'jasmine2',
baseUrl: 'https://localhost/',
rootElement: 'app',
getPageTimeout: 10000,
allScriptsTimeout: 11000,
jasmineNodeOpts: {
defaultTimeoutInterval: 52000,
onComplete: null,
showColors: true,
includeStackTrace: true,
},
onPrepare: function() {
// returning the promise makes protractor wait for the reporter config before executing tests
return browser.getProcessedConfig().then(function(pconfig) {
// console.log("pconfig", pconfig);
});
}
};
console_helper.ts
import { browser } from 'protractor';
export let checkConsole = () => {
browser.manage().logs().get('browser').then((browserLog: any) => {
// this also fails to print the error message
console.log("browserLog", browserLog);
});
}
project.ts
import { browser, element, by, ExpectedConditions } from 'protractor';
const EC = ExpectedConditions;
import { checkConsole } from "./helpers/console_helper";
afterEach(function() {
browser.manage().logs().get('browser').then(function(browserLogs) {
// this fails to print the error message
console.log("browserLogs", browserLogs);
});
});
it("should click on the button and redirect", () => {
browser.get('/url-it-needs');
browser.wait(EC.urlContains('/url-it-needs'), 5000);
let button = element(by.cssContainingText('button', 'BUTTON TEXT'));
browser.wait(EC.visibilityOf(button), 5000);
browser.wait(EC.elementToBeClickable(button), 5000);
button.click();
browser.sleep(500); // wait for the error to be returned from the backend
/*
// this will print the proper error message
browser.manage().logs().get('browser').then(function(browserLogs) {
console.log("browserlogs Inline", browserLogs);
}); */
// this times out
browser.wait(browser.getCurrentUrl().then((url: string) => {
console.log("URL after click", url);
expect(url).toMatch("/properUrl");
}, 5000);
});
我们使用类似的机制在测试失败时读取控制台。
我们的解决方案看起来像这样:
export function printConsole () {
return browser.manage().logs().get('browser').then(browserLog => {
browserLog.forEach(log => console.error(log.message));
})
.catch(() => console.log('Could not get browser log.'));
};
稍微修改它以实际 return 日志可能是有意义的:
export function getConsole () {
return browser.manage().logs().get('browser')
.catch(() => console.log('Could not get browser log.'));
};
export function printConsole () {
return getConsole.then(browserLog => {
browserLog.forEach(log => console.error(log.message));
});
};
这样您也可以将其用于断言,即检查 length === 0
以断言不存在控制台错误。
每次在您的测试用例中阅读 browser.manage().logs()
之后。日志将是空的。因此,您在 afterEach
.
中没有日志
您可以尝试在您的测试用例中删除 browser.manage().logs()
并仅将其保留在 afterEach
.
我的总体目标是将 browser.manage().logs() 提取到辅助函数中。我正在通过在测试中的 afterEach
调用中包含 browser.manage().logs()
功能来实现这一目标。
我知道我的测试抛出一个控制台错误。
browser.manage().logs()
的内联版本将打印错误。 afterEach
和 checkConsole
都不会打印日志。
我需要做什么来解决这个问题?
内联 browser.manage().logs()
是否正在检索错误并擦除日志?我已经尝试了将它们全部注释掉的所有组合,唯一有效的是内联版本。
我还配置了 protractor-screenshoter-plugin
,它生成的报告显示 Show browsers logs
按钮下的控制台错误。这是不是以某种方式抓住错误并阻止 browser.manage().logs()
得到它?
conf.ts
import { browser, Config } from 'protractor';
export let config: Config = {
plugins: [{
package: 'protractor-screenshoter-plugin',
screenshotPath: './protractor/screenshots',
screenshotOnExpect: 'failure+success',
screenshotOnSpec: 'none',
withLogs: true,
writeReportFreq: 'asap',
imageToAscii: 'none',
clearFoldersBeforeTest: true
}],
capabilities: {
'browserName': 'chrome',
chromeOptions: {
args: [ '--disable-gpu', '--window-size=1440x900', '--headless'],
},
'moz:firefoxOptions': {
args: [ '-headless' ],
},
'loggingPrefs' : {"driver": "ALL", "server": "ALL", "browser": "ALL"}
},
directConnect: true,
framework: 'jasmine2',
baseUrl: 'https://localhost/',
rootElement: 'app',
getPageTimeout: 10000,
allScriptsTimeout: 11000,
jasmineNodeOpts: {
defaultTimeoutInterval: 52000,
onComplete: null,
showColors: true,
includeStackTrace: true,
},
onPrepare: function() {
// returning the promise makes protractor wait for the reporter config before executing tests
return browser.getProcessedConfig().then(function(pconfig) {
// console.log("pconfig", pconfig);
});
}
};
console_helper.ts
import { browser } from 'protractor';
export let checkConsole = () => {
browser.manage().logs().get('browser').then((browserLog: any) => {
// this also fails to print the error message
console.log("browserLog", browserLog);
});
}
project.ts
import { browser, element, by, ExpectedConditions } from 'protractor';
const EC = ExpectedConditions;
import { checkConsole } from "./helpers/console_helper";
afterEach(function() {
browser.manage().logs().get('browser').then(function(browserLogs) {
// this fails to print the error message
console.log("browserLogs", browserLogs);
});
});
it("should click on the button and redirect", () => {
browser.get('/url-it-needs');
browser.wait(EC.urlContains('/url-it-needs'), 5000);
let button = element(by.cssContainingText('button', 'BUTTON TEXT'));
browser.wait(EC.visibilityOf(button), 5000);
browser.wait(EC.elementToBeClickable(button), 5000);
button.click();
browser.sleep(500); // wait for the error to be returned from the backend
/*
// this will print the proper error message
browser.manage().logs().get('browser').then(function(browserLogs) {
console.log("browserlogs Inline", browserLogs);
}); */
// this times out
browser.wait(browser.getCurrentUrl().then((url: string) => {
console.log("URL after click", url);
expect(url).toMatch("/properUrl");
}, 5000);
});
我们使用类似的机制在测试失败时读取控制台。 我们的解决方案看起来像这样:
export function printConsole () {
return browser.manage().logs().get('browser').then(browserLog => {
browserLog.forEach(log => console.error(log.message));
})
.catch(() => console.log('Could not get browser log.'));
};
稍微修改它以实际 return 日志可能是有意义的:
export function getConsole () {
return browser.manage().logs().get('browser')
.catch(() => console.log('Could not get browser log.'));
};
export function printConsole () {
return getConsole.then(browserLog => {
browserLog.forEach(log => console.error(log.message));
});
};
这样您也可以将其用于断言,即检查 length === 0
以断言不存在控制台错误。
每次在您的测试用例中阅读 browser.manage().logs()
之后。日志将是空的。因此,您在 afterEach
.
您可以尝试在您的测试用例中删除 browser.manage().logs()
并仅将其保留在 afterEach
.