别名在Cypress中的每个单独测试文件之前都会被重置。

huangapple go评论55阅读模式
英文:

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(&#39;test1&#39;, () =&gt; {
  const signupData = { email: &#39;fakeEmail&#39;, password: &#39;fakePassword&#39; }
  cy.writeFile(&#39;cypress/fixtures/signupData.json&#39;, signupData)
})

spec2.cy.js

it(&#39;test2&#39;, () =&gt; {
  cy.readFile(&#39;cypress/fixtures/signupData.json&#39;).then(signupData =&gt; {
    expect(signupData).to.deep.eq({ email: &#39;fakeEmail&#39;, password: &#39;fakePassword&#39; })
  })
})

答案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 &#39;cypress&#39;

const finalConfig = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // Setup plugins
      config = import(&#39;@optimumqa/cypress-store&#39;)(on, config)

      return config
    },
  },
})

Cypress version < 10

// ./cypress/plugins/index.js

module.exports = (on, config) =&gt; {
  config = require(&#39;@optimumqa/cypress-store&#39;)(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(&#39;setItem&#39;, {
  storeId: &#39;MyStoreForUsefulStuff&#39;,
  item: {
    name: &#39;foo&#39;,
    value: &#39;bar&#39;,
  },
})

Example of getting an item from a store:

// mySpec2.cy.js

cy.task(&#39;getItem&#39;, {
  storeId: &#39;MyStoreForUsefulStuff&#39;,
  item: {
    name: &#39;foo&#39;,
  },
}).then((item) =&gt; {
  console.log(item) // { name: &#39;token, value: &#39;foo&#39; }
  console.log(item.name) // foo
  console.log(item.value) // bar
})

huangapple
  • 本文由 发表于 2023年6月8日 20:05:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76431688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定