gpt4 book ai didi

javascript - AfterAll Hook 错误 - 使用 cucumber-js 和 selenium 时 'this' 不是指我的世界构造函数

转载 作者:行者123 更新时间:2023-11-30 20:06:43 27 4
gpt4 key购买 nike

在测试结束时尝试关闭浏览器时出现“AfterAll hook errored”(使用 cucumber-js 和 selenium)。 '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()this 或使用胖数组表示法:

AfterAll(async () => {
await this.browser.close();
});

关于javascript - AfterAll Hook 错误 - 使用 cucumber-js 和 selenium 时 'this' 不是指我的世界构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52840769/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com