我需要多次执行登录脚本。

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

I need to execute the login script multiple times

问题

我开始写脚本,总共有5页:

  1. 登录
  2. 登录
  3. 仪表板
  4. 账户页面
  5. 设置页面

我为所有页面编写了单独的测试用例。已成功编写了登录页面的测试用例。然后,当我尝试执行仪表板页面的脚本时,需要验证5个测试用例。在这种情况下,我使用beforeEach方法调用登录脚本,并为这5个测试用例使用了5个it块。问题是,我需要一次又一次地登录以运行这5个用例。我需要一次登录运行所有5个测试用例。

英文:

I started script writing and there are 5 pages
01.Sign in
02.Login
03.Dasboard
04.Account Page
05.Settings Page

I wrote seperate test cases for all pages. Written test cases for login page is successfully completed.Then when I try to execute script for dasboard page.There are 5 test cases have to verify.In that case I user beforeEach method for call the login script and use 5 it blocks for those 5 test cases. The issue is I need to login again and again to run that 5 cases. I need to run all 5 test cases with one login.

require('@cypress/xpath')
import '../../support/commands'

import {searchPage} from '../../PageObjects/Serach&FilterPage.js';


const SandF = new searchPage()

describe('Login', () => {
    beforeEach(() => {
        cy.login('username', 'password');
        cy.wait(1000); 
      });

      it('Search functionality with name', () => {

        cy.xpath("//p[contains(.,'Candidates')]").should('have.text','Candidates')
        cy.wait(2000);
          })

      it('Filter functionality with By Completion Status ', () => {
        SandF.Status().click({ force: true })
        cy.wait(5000); 
        cy.contains('Pending').focused().click({ force: true })
        cy.wait(1500); 
     
            })

      it('Filter functionality with By Completion Status =', () => {
      
       SandF.Status().click({ force: true })
        cy.wait(2000); 
        cy.contains('Passed & Completed').scrollIntoView().click({ force: true })
       
            })

      it('Filter functionality with By Completion Status', () => { 
        SandF.Status().click({ force: true })
        cy.wait(1000); 
        cy.contains('Failed & Completed').scrollIntoView().click({ force: true })
        SandF.Reset().click({ force: true })
    })`
    })`

答案1

得分: 4

将您的beforeEach()更改为使用cy.session(),以便它仅登录一次但保留每个测试的凭据。

describe('一组测试套件', () => {
  beforeEach(() => {
    cy.session('登录', () => {
      cy.login('用户名', '密码');
    })
  })

  it('测试1将登录', () => {...})

  it('测试2将登录', () => {...})
})

在文档中查看更多信息这里

英文:

Change your beforeEach() to use a cy.session(), so that it only logs in once but keeps the credentials each test

describe('A suite of tests', () => {
  beforeEach(() => {
    cy.session('login', () => {
      cy.login('username', 'password');
    })
  })

  it('test1 will be logged in', () => {...

  it('test2 will be logged in', () => {...
})

Read more about it in the documentation here

huangapple
  • 本文由 发表于 2023年7月20日 09:22:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76726109.html
匿名

发表评论

匿名网友

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

确定