在测试结束时尝试关闭浏览器时(使用 cucumber-js 和 selenium),我收到“AfterAll hook errored”。 “this”并不像我的 step.js 文件中那样指的是世界
这是我的 hooks.js 文件:
const { AfterAll, Before } = require("cucumber");
AfterAll(async function() {
await this.browser.close();
});
这是我的 world.js 文件:
const driver = require("selenium-webdriver");
require("chromedriver");
const browser = new driver.Builder().forBrowser("chrome").build();
const { setWorldConstructor, setDefaultTimeout } = require("cucumber");
class CustomWorld {
constructor() {
this.driver = driver;
this.browser = browser;
this.setDefaultTimeout = setDefaultTimeout(60 * 1000);
}
}
setWorldConstructor(CustomWorld);
最佳答案
在 JS 闭包中,this
被隐式设置为任何外部上下文(通常是全局的)。您需要将您的函数 .bind()
绑定(bind)到 this
或使用胖数组表示法:
AfterAll(async () => {
await this.browser.close();
});
关于javascript - 毕竟 Hook 错误 - 使用 cucumber-js 和 selenium 时 'this' 并未引用我的世界构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52840769/