我正在使用 befor 挂钩登录网站然后我启动测试用例所以我的问题是在执行之前挂钩然后首先它阻止运行并且在首先运行之后它阻止然后屏幕设置为默认空白页面并且没有执行第二个它块。
我在 describe first before hook 中编写了一段代码,然后在 describe 之前写了两个 it block 并尝试在 describe 之上启动 befor hook 但它没有用。我需要登录一次然后执行其他所有测试用例。
describe('VerifyLoginFunctionality',() => {
const dashbord = new Dashboard()
const loginpage = new LoginPage()
const createtask = new Createtask()
before (() => {
cy.viewport(1280,800) // mackbook
cy.fixture('example').then(function(data)
{
// use "This" keyword to access globaly for example.json file
this.data1=data
cy.wait(2000)
// cy.visit("https://app-redesign.taskbuddy.se")
cy.visit(Cypress.env('login_url'))
//Verify the title of the page
cy.title().should('eq',this.data1.Pagetitle)
loginpage.SigninMannuallyButton().click()
loginpage.TeamSpace().type(this.data1.TeamspaceName)
loginpage.NextButton().click()
loginpage.Email().type(this.data1.email)
loginpage.Password().type(this.data1.Password)
loginpage.SigninButton().click()
cy.wait(3000)
})
})
it('Verify the user profile',() => {
dashbord.UserProfileButton().click()
cy.wait(2000)
dashbord.UserProfilePopupMiddleLayer().should('be.visible')
dashbord.Firstname()
.invoke('val') // for input or textarea, .invoke('val')
.then(text => {
const someText = text;
cy.log("aaa> "+someText)
// BDD style
expect(someText).to.equal("Toyota")
//TDD style
assert.equal(someText,"Toyota")
})
cy.wait(3000)
dashbord.Givenname().clear()
dashbord.Givenname().type("jjjjjjj")
dashbord.Firstname().should('have.value',"jjjjjjj")
cy.wait(1500)
dashbord.CloseIconOnProfile().click()
})
it('Verify the create task',() => {
// const dashbord = new Dashbord()
createtask.CreateNewTaskButton().click()
})
一种方法是在
testIsolation: false
文件中设置 cypress.config.js
。
但这不是最佳实践,如果状态超出测试边界,您可能会在第二、第三次测试中导致误报结果。
更好的做法是使用
cy.session()
来缓存登录。它只调用一次登录,但在 beforeEach()
中调用以允许缓存为每个测试重新设置凭据。
beforeEach(() => {
cy.session('login', () => {
// login code here, is only ever called once!
cy.visit(Cypress.env('login_url'))
cy.title().should('eq',this.data1.Pagetitle)
loginpage.SigninMannuallyButton().click()
loginpage.TeamSpace().type(this.data1.TeamspaceName)
... complete login code
})
})
it('test1', () => {
// is logged in
})
it('test2', () => {
// is logged in
})
it('test3', () => {
// is logged in
})