简体   繁体   English

如何将用户名和密码从夹具文件调用到赛普拉斯的测试场景中

[英]How to call usernames and password from fixture file into test scenario in Cypress

I have my login test scenario which is working.我有我的登录测试场景正在运行。 I have removed my username and password to a fixture file, which I then want to call in order to populate the username and password fields.我已将我的用户名和密码删除到一个夹具文件中,然后我想调用它以填充用户名和密码字段。 Can someone point me in the right direction of how this should be done?有人可以指出我应该如何做到这一点的正确方向吗? Below is my test scenario tyring to call the fixture to populate the fields下面是我的测试场景,试图调用夹具来填充字段

 describe('My Login Test', function (){ it('Visit Risk App Landing Page', function (){ cy.visit('site url') cy.get('button').click() cy.get('a.auth0-lock-alternative-link').contains('Not your account?').click() cy.get('input.auth0-lock-input').first() .type(cy.fixture('loginUser').email) cy.get('input.auth0-lock-input').last() .type(cy.fixture('loginUser').password) cy.get('button').click() cy.url().should('eq','site url') }) })

I was given the answer by someone in a gitter chatroom: 在一个聊天室里有人给我答案:

describe('My Login Test', function (){
it('Visit Risk App Landing Page', function (){
    cy.visit('https://bvt-riskassessment.lmkcloud.net')
    cy.get('button').click()
    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')

I can now login using this without having the username or password in every test scenario. 现在,我可以使用此帐户登录,而无需在每个测试方案中都使用用户名或密码。

I'd recommend using a fixture alias. 我建议使用灯具别名。

see: Accessing-Fixture-Data 请参阅: 访问固定数据

First, create a local variable for the fixture: 首先,为灯具创建一个局部变量:

cy.fixture('loginUser').as('myUser');

then, when you want to access it's properties, it's just this.myUser.email or something. 然后,当您要访问它的属性时,它就是this.myUser.email或其他内容。 This would change your test to: 这会将您的测试更改为:

.type(this.myUser.email)

Hopefully that will help with your problems? 希望这对您的问题有所帮助? But, as demouser123 mentioned, we'd need the actual error in order to help you. 但是,正如demouser123所述,我们需要实际的错误才能为您提供帮助。

Also you can use import<\/code> like this:您也可以像这样使用import<\/code> :

import jsonFile from '../../../fixtures/file.json'

describe('getting field from json', () => {
it('get something from json', () => {
        cy.get('input[name=inputField]').clear().type(jsonFile.myField)
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM