英文:
I need to execute the login script multiple times
问题
我开始写脚本,总共有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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论