我对量角器很陌生,所以我希望有人知道这个问题的答案:
问题 如何设置它,以便在测试失败时,报告日志显示失败的行?
示例 Nightwatch终端日志将显示以下内容:
testing command1
testing command2
Error sharedFunctions.js (5:23) // 5-line, 23-character where the error occured
componentHelper.js (236:32) // place where sharedFunctions was called
test.js (56:12) // place in test where componentHelper was called
可以用量角器做类似的事情吗?
答案 0 :(得分:0)
使用量角器运行测试直接显示了这一点。例如:
➜ MyApp git:(master) ✗ protractor conf.js
[08:59:29] I/launcher - Running 1 instances of WebDriver
[08:59:29] I/local - Starting selenium standalone server...
[08:59:29] I/local - Selenium standalone server started at http://192.168.157.128:52662/wd/hub
Started
F
F.
Failures:
1) My tests Loads and login
Message:
Expected 'My variable' to equal 'Welcome!'.
Stack:
Error: Failed expectation
at UserContext.<anonymous> (/MyApp/ConvertedJSFiles/mytests_1.js:65:39)
at step (/MyApp/ConvertedJSFiles/mytests_1.js:32:23)
at Object.next (/MyApp/ConvertedJSFiles/mytests_1.js:13:53)
at fulfilled (/MyApp/ConvertedJSFiles/mytests_1.js:4:58)
at process._tickCallback (internal/process/next_tick.js:68:7)
2) My Second Test
Message:
Expected 0 to be 4.
Stack:
Error: Failed expectation
at UserContext.<anonymous> (/MyApp/ConvertedJSFiles/mytests_1.js:91:37)
at step (/MyApp/ConvertedJSFiles/mytests_1.js:32:23)
at Object.next (/MyApp/ConvertedJSFiles/mytests_1.js:13:53)
at fulfilled (/MyApp/ConvertedJSFiles/mytests_1.js:4:58)
at process._tickCallback (internal/process/next_tick.js:68:7)
3 specs, 2 failures
Finished in 5.991 seconds
[08:59:37] I/local - Shutting down selenium standalone server.
[08:59:37] I/launcher - 0 instance(s) of WebDriver still running
[08:59:37] I/launcher - chrome #01 failed 2 test(s)
[08:59:37] I/launcher - overall: 2 failed spec(s)
[08:59:37] E/launcher - Process exited with error code 1
更新:
由于我不允许透露代码,因此以下是相关部分:
// shared-helper.ts:
export class SharedHelper {
// ...
public static isPresentAndVisible(el: ElementFinder, expectedResult: boolean, nameUponFail = 'Element'): void {
el.isPresent().then(present => {
if (present !== expectedResult) {
// tslint:disable-next-line:max-line-length
global.console.log('[isPresentAndVisible] ERROR: [', nameUponFail, ']\'s presence was expected to be [', expectedResult, '] and it was not');
expect(present).toEqual(expectedResult);
} else if (expectedResult) {
el.isDisplayed().then(displayed => {
if (displayed !== expectedResult) {
// tslint:disable-next-line:max-line-length
global.console.log('[isPresentAndVisible] ERROR: [', nameUponFail, ']\'s visibility was expected to be [', expectedResult, '] and it was not. Presence was confirmed though');
}
expect(displayed).toEqual(expectedResult);
});
} else {
expect(present).toEqual(expectedResult);
}
});
}
// ...
}
// m-helper.ts:
export class MHelper {
// ...
public static checkM(someParameters): void {
// ...
SharedHelper.isPresentAndVisible(someArguments);
// ...
}
}
// test.ts:
// ...
it('Test', () => {
// ...
MHelper.checkM(someArguments);
// ...
});
//protractor.conf.js:
const {
SpecReporter
} = require('jasmine-spec-reporter');
var screenShotReporter = require('./e2e/reporter/screenshot-reporter');
exports.config = {
allScriptsTimeout: 90000, // original value: 11000
getPageTimeout: 30000, // original value: 20000
suites: {
...
regression: './e2e/src/regression/.test.ts',
...
},
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
prefs: {
'profile.managed_default_content_settings.notifications': 2
}
}
},
directConnect: true,
baseUrl: 'http://localhost:4200',
framework: 'jasmine',
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 50000,
print: function () {}
},
onPrepare() {
require('ts-node').register({
project: 'e2e/tsconfig.e2e.json'
});
jasmine.getEnv().addReporter(new SpecReporter({
suite: {
displayNumber: true
},
spec: {
displayStacktrace: true,
displayDuration: true
}
}));
jasmine.getEnv().addReporter(screenShotReporter);
setTimeout(function() {
browser.driver.executeScript(function() {
return {
width: window.screen.availWidth,
height: window.screen.availHeight
};
}).then(function(result) {
browser.driver.manage().window().setSize(result.width, result.height);
});
});
}
};
isPresentAndVisible失败;这是日志:
[12:10:58] I/direct - Using ChromeDriver directly...
Jasmine started
[isPresentAndVisible] ERROR: [ xBtn ]'s presence was expected to be [ true ] and it was not
1 DescName
1.1
✗ Test (17 secs)
- Expected false to equal true.
at /home/zzzz/zzzz/projName/zzzz/e2e/src/utils/helpers/shared.ts:478:21
at ManagedPromise.invokeCallback_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:1376:14)
at TaskQueue.execute_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:3084:14)
at TaskQueue.executeNext_ (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:3067:27)
at asyncRun (/usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:2927:27)
at /usr/lib/node_modules/protractor/node_modules/selenium-webdriver/lib/promise.js:668:7
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
**************************************************
* Failures *
**************************************************
1) ...Test
- Expected false to equal true.
Executed 1 of 803 specs (1 FAILED) (802 SKIPPED) in 24 secs.
[12:11:24] I/launcher - 0 instance(s) of WebDriver still running
[12:11:24] I/launcher - chrome #01 failed 1 test(s)
[12:11:24] I/launcher - overall: 1 failed spec(s)
[12:11:24] E/launcher - Process exited with error code 1
*/
如您所见,在这里,我已经收到有关sharedHelper失败的信息,但是没有在isPresentAndVisible函数的调用位置(多次使用)。 另外,当browser.wait(EC.visibilityOf(btn));时,信息甚至更少(日志中没有引用我创建的文件)。被称为;