赛普拉斯的条件案例
Conditional cases in cypress
我在为条件案例编写测试时遇到了问题。其中一项测试使用 api in 'before' (beforeAll) 函数创建一个对象,然后在测试中创建的对象有时不会显示在搜索结果中。我以前用过木偶师。我可以让页面重新加载,直到对象显示在搜索结果中。但是,我没有办法做同样的事情。我正在考虑使用 cy.get 然后检查响应。例如,(cy.get('sth').then(s1 => {do something like cy.reload()})).
然后,我发现s1在reload后一直保持不变。所以,我被困住了。希望有人能帮助我。如果描述不清楚,请在下面我的另一个post。谢谢
根据我从您 post 那里收集到的信息,我认为这样的事情可能会有所帮助:
cy.get('some.selector').then(elem => {
// ...
});
cy.reload();
cy.get('some.selector').then(elem => {
// run code on element after reloading...
});
如果这不能回答您的问题,请考虑制作一个minimal, complete and verifiable example。
我的错。我没有很好地解释我的问题。问题不在 before() 函数中。在 before() 函数中,我调用 post API 来创建人物。
然后,我将在我的每个测试中使用在 before() 函数中创建的那些人。
代码如下所示:
before(()=> {createPerson(); cy.visit('mywebsite); login();} )
it('search person I created by calling api', () => {
cy.get('.search')
.type('person's name{enter}');
cy.get(':nth-child(1) > resulttable').click();
问题来了。我在搜索结果中找不到这个人,因为数据需要时间才能传递。然后,测试失败。
所以,我需要通过调用重新加载页面(该页面是搜索结果页面)
cy.reload();
但是不知道要在上面调用多少reload才能让这个人出现在搜索结果中
我目前使用的解决方案是cy.wait(30000)
。等待 30 秒。
所以,我想知道我现在要怎么做。
抱歉,问题出在您的 before 函数中。只有在为 运行 测试准备好环境后才能启动它。
const createPerson = (params, done)=>{
cy.request({// create people here}).then(({people})=>{
done(undefined, {people});
})
}
before((done)=> {
createPerson({}, (err, {people})=>{
cy.visit('mywebsite);
login({}, done); // this should be async as well
});
})
it('search person I created by calling api', () => {
//if you data is not cached, at this point you will have the people populated in your screen.
cy.get('.search')
.type('person's name{enter}');
cy.get(':nth-child(1) > resulttable').click();
我在为条件案例编写测试时遇到了问题。其中一项测试使用 api in 'before' (beforeAll) 函数创建一个对象,然后在测试中创建的对象有时不会显示在搜索结果中。我以前用过木偶师。我可以让页面重新加载,直到对象显示在搜索结果中。但是,我没有办法做同样的事情。我正在考虑使用 cy.get 然后检查响应。例如,(cy.get('sth').then(s1 => {do something like cy.reload()})).
然后,我发现s1在reload后一直保持不变。所以,我被困住了。希望有人能帮助我。如果描述不清楚,请在下面我的另一个post。谢谢
根据我从您 post 那里收集到的信息,我认为这样的事情可能会有所帮助:
cy.get('some.selector').then(elem => {
// ...
});
cy.reload();
cy.get('some.selector').then(elem => {
// run code on element after reloading...
});
如果这不能回答您的问题,请考虑制作一个minimal, complete and verifiable example。
我的错。我没有很好地解释我的问题。问题不在 before() 函数中。在 before() 函数中,我调用 post API 来创建人物。 然后,我将在我的每个测试中使用在 before() 函数中创建的那些人。 代码如下所示:
before(()=> {createPerson(); cy.visit('mywebsite); login();} )
it('search person I created by calling api', () => {
cy.get('.search')
.type('person's name{enter}');
cy.get(':nth-child(1) > resulttable').click();
问题来了。我在搜索结果中找不到这个人,因为数据需要时间才能传递。然后,测试失败。
所以,我需要通过调用重新加载页面(该页面是搜索结果页面)
cy.reload();
但是不知道要在上面调用多少reload才能让这个人出现在搜索结果中
我目前使用的解决方案是cy.wait(30000)
。等待 30 秒。
所以,我想知道我现在要怎么做。
抱歉,问题出在您的 before 函数中。只有在为 运行 测试准备好环境后才能启动它。
const createPerson = (params, done)=>{
cy.request({// create people here}).then(({people})=>{
done(undefined, {people});
})
}
before((done)=> {
createPerson({}, (err, {people})=>{
cy.visit('mywebsite);
login({}, done); // this should be async as well
});
})
it('search person I created by calling api', () => {
//if you data is not cached, at this point you will have the people populated in your screen.
cy.get('.search')
.type('person's name{enter}');
cy.get(':nth-child(1) > resulttable').click();