英文:
Aliases are reset before every separate test file in Cypress
问题
I am running two tests separately. One is Signup Test File and the other one is Login. When I sign up, I stored the data and want to use it in the Login Test File.
SignUp Test File
// import cypress from "cypress"
import { BrandSignUp } from "../Brand/brand_signup"
const signuppage = new BrandSignUp()
it('Brand Sign Up', () => {
cy.visit('https://staging:ec2-18-189-16-96.us-east-2@staging.brand.plastk.ca/sign-up', { failOnStatusCode: false })
const fakeEmail = signuppage.generateFakeEmail();
const fakePassword = signuppage.generateFakePassword();
signuppage.enterEmail(fakeEmail);
signuppage.enterPassword(fakePassword);
signuppage.enterConfirmPassword(fakePassword);
signuppage.checkTermsAndConditions();
signuppage.clickPasswordEyeIcon();
signuppage.clickConfirmPasswordEyeIcon();
signuppage.checkPasswordsMatch();
signuppage.clickSignUp();
cy.wrap({ fakeEmail, fakePassword }).as('signupData');
// signuppage.storeGeneratedEmail(fakeEmail);
// signuppage.storeGeneratedPassword(fakePassword);
})
Login Test File
// import cypress from "cypress"
import { BrandLogin } from "../Brand/brand_login"
const loginpage = new BrandLogin()
it('Login to Brand Portal', () => {
cy.visit('https://staging:ec2-18-189-16-96.us-east-2@staging.brand.plastk.ca/')
cy.get('@signupData').then((signupData) => {
const { email, password } = signupData;
// const SuEmail = loginpage.retrieveStoredEmail();
// const SuPassword = loginpage.retrieveStoredPassword();
loginpage.enterEmail(email);
loginpage.enterPassword(password);
loginpage.enterRememberMe();
loginpage.eyeIcon();
loginpage.forgotPasswordButton();
loginpage.signUpButton();
loginpage.clickSignIn();
cy.wait(8000);
cy.get('#company_name').type('Bilal Test');
cy.get('.UploadFilestyles__UploadIcon-sc-xxr9kh-1').attachFile('Topics of Cypress.png');
cy.get('#company_address').type('71 stationview place');
cy.get('#primary_contact_person').type('Bilal');
cy.get('#primary_contact_number').type('(342) 343-3423');
})
})
Always getting an error in the line
cy.get('@signupData').then((signupData) => {
const { email, password } = signupData;
英文:
I am running two tests separately. One is Signup Test File and otherone is Login. When I sign up I stored the data and wants to use it in Login Test File.
Signup Test file runs and showing alliases is created and values are stored but when I run the login Test file it shows me an error message of
> cy.get()
could not find a registered alias for: @signupData.
>
> You have not aliased anything yet.
I run my Test file of login by getting data from sign up but its not working.
SignUp Test File
//import cypress from "cypress"
import { BrandSignUp } from "../Brand/brand_signup"
const signuppage = new BrandSignUp()
it('Brand Sign Up', () => {
cy.visit('https://staging:ec2-18-189-16-96.us-east-2@staging.brand.plastk.ca/sign-up', { failOnStatusCode: false })
const fakeEmail = signuppage.generateFakeEmail();
const fakePassword = signuppage.generateFakePassword();
signuppage.enterEmail(fakeEmail);
signuppage.enterPassword(fakePassword);
signuppage.enterConfirmPassword(fakePassword);
signuppage.checkTermsAndConditions();
signuppage.clickPasswordEyeIcon();
signuppage.clickConfirmPasswordEyeIcon();
signuppage.checkPasswordsMatch();
signuppage.clickSignUp();
cy.wrap({ fakeEmail, fakePassword }).as('signupData');
// signuppage.storeGeneratedEmail(fakeEmail);
// signuppage.storeGeneratedPassword(fakePassword);
})
Login Test File
//import cypress from "cypress"
import { BrandLogin } from "../Brand/brand_login"
const loginpage = new BrandLogin()
it('Login to Brand Portal', () => {
cy.visit('https://staging:ec2-18-189-16-96.us-east-2@staging.brand.plastk.ca/')
cy.get('@signupData').then((signupData) => {
const { email, password } = signupData;
// const SuEmail = loginpage.retrieveStoredEmail();
// const SuPassword = loginpage.retrieveStoredPassword();
loginpage.enterEmail(email);
loginpage.enterPassword(password);
loginpage.enterRememberMe();
loginpage.eyeIcon();
loginpage.forgotPasswordButton();
loginpage.signUpButton();
loginpage.clickSignIn();
cy.wait(8000);
cy.get('#company_name').type('Bilal Test');
cy.get('.UploadFilestyles__UploadIcon-sc-xxr9kh-1').attachFile('Topics of Cypress.png');
cy.get('#company_address').type('71 stationview place');
cy.get('#primary_contact_person').type('Bilal');
cy.get('#primary_contact_number').type('(342) 343-3423');
})
})
Always getting an error in the line
cy.get('@signupData').then((signupData) => {
const { email, password } = signupData;
</details>
# 答案1
**得分**: 2
你可以将变量写入和读取自一个夹具文件:
**spec1.cy.js**
```js
it('test1', () => {
const signupData = { email: 'fakeEmail', password: 'fakePassword' }
cy.writeFile('cypress/fixtures/signupData.json', signupData)
})
spec2.cy.js
it('test2', () => {
cy.readFile('cypress/fixtures/signupData.json').then(signupData => {
expect(signupData).to.deep.eq({ email: 'fakeEmail', password: 'fakePassword' })
})
})
英文:
You could write and read the variable to a fixture file:
spec1.cy.js
it('test1', () => {
const signupData = { email: 'fakeEmail', password: 'fakePassword' }
cy.writeFile('cypress/fixtures/signupData.json', signupData)
})
spec2.cy.js
it('test2', () => {
cy.readFile('cypress/fixtures/signupData.json').then(signupData => {
expect(signupData).to.deep.eq({ email: 'fakeEmail', password: 'fakePassword' })
})
})
答案2
得分: 0
以下是已翻译的内容:
你可以使用这个包来在测试文件之间存储和保留数值。
安装:
npm install @optimumqa/cypress-store
用法:
Cypress 版本 >= 10:
// ./cypress.config.js
import { defineConfig } from 'cypress'
const finalConfig = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// 设置插件
config = import('@optimumqa/cypress-store')(on, config)
return config
},
},
})
Cypress 版本 < 10:
// ./cypress/plugins/index.js
module.exports = (on, config) => {
config = require('@optimumqa/cypress-store')(on, config)
return config
}
用法:
下面是一个示例,在一个规范中保存一个令牌,并在另一个规范中重复使用它。
你可以拥有无限的存储空间。
设置新项目到 'MyStoreForUsefulStuff' 中的示例:
如果存储空间不存在,将自动创建:
// mySpec1.cy.js
cy.task('setItem', {
storeId: 'MyStoreForUsefulStuff',
item: {
name: 'foo',
value: 'bar',
},
})
从存储空间获取项目的示例:
// mySpec2.cy.js
cy.task('getItem', {
storeId: 'MyStoreForUsefulStuff',
item: {
name: 'foo',
},
}).then((item) => {
console.log(item) // { name: 'token, value: 'foo' }
console.log(item.name) // foo
console.log(item.value) // bar
})
英文:
You can use this package to store and preserve values across test files
https://github.com/optimumqa/cypress-store
Installation
npm install @optimumqa/cypress-store
Usage
Cypress version >= 10
// ./cypress.config.js
import { defineConfig } from 'cypress'
const finalConfig = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// Setup plugins
config = import('@optimumqa/cypress-store')(on, config)
return config
},
},
})
Cypress version < 10
// ./cypress/plugins/index.js
module.exports = (on, config) => {
config = require('@optimumqa/cypress-store')(on, config)
return config
}
Usage
Below is an example saving a token inside one spec, and re-use it inside another.
You can have infinite stores.
Example of setting a new item into 'MyStoreForUsefulStuff'.
> If the store does not exist, it will be automatically created:
// mySpec1.cy.js
cy.task('setItem', {
storeId: 'MyStoreForUsefulStuff',
item: {
name: 'foo',
value: 'bar',
},
})
Example of getting an item from a store:
// mySpec2.cy.js
cy.task('getItem', {
storeId: 'MyStoreForUsefulStuff',
item: {
name: 'foo',
},
}).then((item) => {
console.log(item) // { name: 'token, value: 'foo' }
console.log(item.name) // foo
console.log(item.value) // bar
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论