If statement 在cypress测试场景中创建if语句

If statement 在cypress测试场景中创建if语句,if-statement,testing,cypress,If Statement,Testing,Cypress,我想在我的登录测试中添加一个if语句,因为有两个登录页面可以加载。当下面的测试找不到文本“Not your account?”(不是您的帐户?)时,它将继续执行登录步骤,测试将失败。我认为这是因为两个登录页面具有相同的元素“a.auth0-lock-alternative-link”,但包含不同的文本 如何在测试中创建if语句来实现这两种场景 describe('My Login Test2', function (){ it('Visit Risk App Landing Page', fun

我想在我的登录测试中添加一个if语句,因为有两个登录页面可以加载。当下面的测试找不到文本“Not your account?”(不是您的帐户?)时,它将继续执行登录步骤,测试将失败。我认为这是因为两个登录页面具有相同的元素“a.auth0-lock-alternative-link”,但包含不同的文本

如何在测试中创建if语句来实现这两种场景

describe('My Login Test2', function (){
it('Visit Risk App Landing Page', function (){
cy.visit('https://bvt-riskassessment.lmkcloud.net')
cy.get('button').click()
if(cy.get('a.auth0-lock-alternative-link').contains('Not your account?'))
{
cy.get('a.auth0-lock-alternative-link').contains('Not your account?').click();
}
cy.fixture('loginUser').as('myUserFixture');
cy.get('@myUserFixture').then(user => {
cy.get('input.auth0-lock-input').first().type(user.email);
cy.get('input.auth0-lock-input').last().type(user.password);
cy.get('button').click()
cy.url().should('eq','https://bvt-riskassessment.lmkcloud.net/workflow')
})
})
})

您可以使用each函数对项目进行迭代,然后检查其中的文本内容。通过使用cy.wrap包装元素,u可以使用非正常cypress函数

更多信息:

 cy.get('a.auth0-lock-alternative-link')
    .each(($elm) => {
      cy.wrap($elm).invoke('text').then((text) => {
        if (text === "Not your account?") {
          // do somthing
        } 
      })
    })