简体   繁体   English

赛普拉斯 function 调用有几个选项

[英]Cypress function call with several options

Cypress.Commands.add('Login', (env,username) => {
        env(staging) = cy.visit('LINK') 
        env(live) = cy.visit('LINK') 
        username(practitioner1) = {
            cy.get('input[name="Parameter.UserName"]').type('practitioner1')
            cy.get('input[name="Parameter.Password"]').type('pass')
        }

    cy.contains('Login').click()
  })

I don't know why i have ',' expected ts(1005) error我不知道为什么我有 ',' 预期的 ts(1005) 错误

I want to call from another file something like cy.Login(staging,practitioner1), or cy.Login(live,practitioner2) so that I don't have to hard code the link, username and password every time.我想从另一个文件中调用类似 cy.Login(staging,practitioner1) 或 cy.Login(live,practitioner2) 之类的文件,这样我就不必每次都对链接、用户名和密码进行硬编码。 Here is a prinscreen with errors: pasteboard.co/0bYOL4Dx7mnE.png这是一个有错误的主屏幕:pasteboard.co/0bYOL4Dx7mnE.png

I would suggest the following:我建议如下:

Cypress.Commands.add('login', (env, user) => {
    const practitionerUser1 = { username: 'foo', password: 'myPasswordFoo#'};
    const practitionerUser2 = { username: 'zoo', password: 'myPasswordZoo#'};

    env = 'staging' ? cy.visit('staging_link') : cy.visit('production_link')


    user = 'practitionerUser1'
        ? (cy.get('input[name="email"]').type(practitionerUser1.username),
          cy.get('input[name="password"]').type(practitionerUser1.password))
        : (cy.get('input[name="email"]').type(practitionerUser2.username),
          cy.get('input[name="password"]').type(practitionerUser2.password));

    cy.contains('login').click()
});

and use it in your tests like:并在您的测试中使用它,例如:

cy.login('staging', 'practitionerUser1');

PS Because the above command interacts with the UI, for performance reasons I would suggest for actions like login to use api requests. PS 由于上述命令与 UI 交互,出于性能原因,我建议登录等操作使用 api 请求。 It is more robust and reliable over time;)随着时间的推移,它会变得更加健壮和可靠;)

  1. Go to your cypress.json and add all your links there. Go 到您的cypress.json并在那里添加所有链接。
{
  //Other cypress.json elements
  "env": {
    "staging": "www.staging.com",
    "live": "www.live.com"
  }
}
  1. In your support/commands.js add the custom command在您的support/commands.js添加自定义命令
Cypress.Commands.add('Login', (environment, username, password) => {
  cy.visit(Cypress.env(environment))
  cy.get('input[name="Parameter.UserName"]').type(username)
  cy.get('input[name="Parameter.Password"]').type(password)
  cy.contains('Login').click()
})
  1. In your test write:在你的测试中写:
cy.Login('staging', 'username', 'password')

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

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