我需要相对于其他步骤放置一些步骤定义,因此如果上下文编写不正确,我将使用上下文警告测试编写者。
黄瓜
Scenario: doesn't need fixture
Given I am "admin"
And I am logged in
Scenario: uses fixture
Given I am "admin"
And I use the fixture "foo.json"
And I am logged in
js
beforeEach(function () {
this.isLoggedIn = false;
cy.fixture('fixtures.json').as('fixtures');
});
given('I am {string}', function(username) {
this.username = username;
});
given('I am logged in', function () {
cy.request('/auth/login');
this.isLoggedIn = true;
// etc...
});
given('I use the fixture {string}', function(fixtureName) {
if (this.isLoggedIn) throw new Error('You should declare fixtures before logging in');
this.data = this.fixtures[fixtureName];
});
如果我在beforeEach
钩子中进行一些控制台日志记录,我会发现在每次测试之前this.isLoggedIn
都设置为false
。但是,如果我在步骤定义中记录了相同的内容,则上下文似乎没有改变。
as
is async,所以我认为可能是某些竞争条件阻止了上下文的设置,但是试图等待cy.fixture
像这样的钩子完成
beforeEach(function() {
return Promise.all([
cy.fixture('fixtures'),
cy.fixture('users')
]).then((fixtures, users) => {
this.fixtures = fixtures;
this.users = users;
this.isLoggedIn = false;
})
});
然后柏树告诉我can't use a promise in a hook ...
如何正确重置挂钩中的上下文?