我正在使用量角器。以下解决方案有效,但我收到此警告:
this.currentTest.state
-错误TS2532:对象可能是“未定义”
(属性)Mocha.Context.currentTest ?: Mocha.Test |未定义
如何解决此警告?
测试文件:
const helper = new HelperClass();
afterEach(async ()=> {
const state = this.currentTest.state;
await helper.getSource(state);
});
类文件
import { browser, } from 'protractor';
export class HelperClass {
public getSource(state:any) {
if (state === 'failed') {
browser.driver.getPageSource().then(function (res) {
console.log(res);
});
}
}
}
答案 0 :(得分:0)
我认为发生错误是因为对this.currentTest.state
的访问发生在另一个函数内部:传递给afterEach
的箭头函数-流分析不会跨越函数边界。尝试简单地将函数外的那条线拉出:
const helper = new HelperClass();
afterEach(async ()=> {
const state = this.!currentTest.state;
await helper.getSource(state);
});
这有什么改变吗?