我正在赛普拉斯创建一些集成测试。
我目前的注册流程已经关闭。-这是一个文件测试。
registration.spec.js
现在,我想运行一个login.spec.js测试。如何使用在registration.spec.js中创建的动态电子邮件,并在“ login.spec.js”中使用该电子邮件?
registration.spec.js
----> create a dynamic email address
----> do some tests
----> end registration test
login.spec.js
---> login with email/pw I created in the registration.spec.js file?
Run Code Online (Sandbox Code Playgroud)
这是如何实现的。柏树中是否有injectGlobal类型的交易?
另外,如果我以独立方式运行login.spec.js,是否可以使用“提供的”凭据或通过全局参数保存的凭据来运行所有测试?那有意义吗?
我想运行所有测试...
registration.spec.js (create dynamic email/pw)
login.spec.js (use dynamic values from registration process)
or
login.spec.js (standalone, use supplied credentials)
Run Code Online (Sandbox Code Playgroud)
是的,Cypress支持在 UI 中创建和重用操作和状态的功能,例如在测试前注册和登录。
然而,Cypress, throughcy.request()允许您比用户更强大地控制浏览器的状态,使您的测试更简单、更快、更可靠
查看下面的示例,其中cy.request用于在服务器上创建/读取状态。
在commands/index.js:
Cypress.Commands.add('login', (user) => {
cy.request('POST', `${apiUrl}/users/login`, user)
})
Cypress.Commands.add("register", (user) => {
cy.request('POST', `${apiUrl}/users/register`, user)
})
Cypress.Commands.add('getUser', (username) => {
return cy.request('GET', `${apiUrl}/users/${username}`)
})
Run Code Online (Sandbox Code Playgroud)
在register.spec.js:
it ('can register', () => {
const user = {
name: 'jake',
email: 'jake@jake.com',
password: '12345'
}
cy.visit('/register')
cy.get('input[name="name"]').type(user.name)
cy.get('input[name="email"]').type(user.email)
cy.get('input[name="password"]').type(user.password)
cy.get('input[name="password-confirm"]').type(user.password)
cy.get('input[type="submit"]').click()
// ensure register page sends you /home after register
cy.url().should('contain', '/home')
// expect user from server to match user from test
cy.getUser(user.name)
.then((dbUser) => expect(dbUser).to.deep.eql(user))
})
Run Code Online (Sandbox Code Playgroud)
在login.spec.js:
it('can log in', () => {
const user = {
name: 'jane',
email: 'jane@jane.com',
password: '12345'
}
// register w/out UI
cy.register(user)
cy.visit('/login')
cy.get('input[name="name"]').type(user.name)
cy.get('input[name="password"]').type(user.password)
cy.get('input[type="submit"]').click()
// ensure the login page sends you home after login
cy.url().should('contain', '/home')
})
Run Code Online (Sandbox Code Playgroud)
在userSettings.spec.js:
it('can change email', () => {
const user = {
name: 'jane',
email: 'jane@jane.com',
password: '12345'
}
// register and login w/o UI
cy.register(user)
cy.login(user)
cy.visit('/settings')
cy.get('input[name="email"]').type('UpdatedEmail@jane.com')
cy.get('input[type="submit"]').click()
cy.getUser(user.name)
.then((dbUser) => expect(dbUser.email).to.eql('UpdatedEmail@jane.com'))
})
Run Code Online (Sandbox Code Playgroud)