进入相同测试中的2个页面。

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

Navigate into 2 pages in the same test

问题

这是你提供的 JavaScript 代码的翻译:

  1. 我在JavaScriptCypress中非常新如果这是非常简单的事情请原谅我
  2. 我想要做的是在一个测试中导航到两个页面
  3. 我的test.cy.js文件包含以下代码
  4. describe('Sitop Manager登录页面', () => {
  5. it('打开登录页面,填写表单并单击提交', () => {
  6. cy.visit('/login')
  7. cy.get('input[name="username"]').type('admin')
  8. cy.get('input[name="password"]').type('admin')
  9. cy.contains('Login').click()
  10. })
  11. it('打开设备页面', () => {
  12. cy.visit('/devices')
  13. cy.wait(500)
  14. cy.get('ix-menu ix-menu-item[tab-icon=cogwheel]').click() //CSS选择器
  15. })
  16. })

问题在于,当它完成/login页面后,它会重新加载到登录页面,因此/devices页面无法进行测试。在我的cypress.config.js文件中,我已经设置了baseUrl:

  1. const { defineConfig } = require("cypress");
  2. module.exports = defineConfig({
  3. e2e: {
  4. baseUrl: 'http://localhost:3000', //在所有测试类中使用的baseUrl
  5. setupNodeEvents(on, config) {
  6. // 在这里实现节点事件监听器
  7. },
  8. },
  9. });

希望这有所帮助。如果你有任何问题,请随时提出。

英文:

I'm very new in Javascript and Cypress so pardon me if it's something very simple.

What I would like to do is to navigate into 2 pages inside one test.
My test.cy.js file has this code:

  1. describe('Sitop Manager Login Page', () => {
  2. it('opens login page, fill the form and clicks submit', () => {
  3. cy.visit('/login')
  4. cy.get('input[name="username"]').type('admin')
  5. cy.get('input[name="password"]').type('admin')
  6. cy.contains('Login').click()
  7. })
  8. it('Open devices page', () => {
  9. cy.visit('/devices')
  10. cy.wait(500)
  11. cy.get('ix-menu ix-menu-item[tab-icon=cogwheel]').click() //css selector
  12. })
  13. })

The issue is that when it finishes the /login page, then it reloads to login page again so the /devices page cannot be tested. In my cypress.config.js file i have setted the baseUrl

  1. const { defineConfig } = require("cypress");
  2. module.exports = defineConfig({
  3. e2e: {
  4. baseUrl: 'http://localhost:3000', //Baseurl to be used in all accross the testing classes
  5. setupNodeEvents(on, config) {
  6. // implement node event listeners here
  7. },
  8. },
  9. });

答案1

得分: 1

如果 /devices 是一个需要登录的路由,那么任何访问它的测试的第一步都必须是登录。因此,你总是需要首先访问 /login,经过登录流程,然后才能访问 /devices

wait 在登录之后访问 /devices 之前可能是不必要的,这取决于你的测试环境的默认超时设置。

如 @jjhelguero 在评论中提到的,你还可以拦截网络请求(也许是到你的登录端点),等待它成功返回作为 wait 的替代。get 会进行重试,但可能会超时:https://docs.cypress.io/api/commands/intercept

有一些方法可以避免实际登录,比如设置一个会话cookie,但可能你想要在测试中模拟实际用户的工作流程。

英文:

If /devices is a "login required" route, then the first step in any test that accesses it must be to log in. So you will always need to visit /login first, go through the login workflow, then you can visit /devices.

  1. it('Open devices page', () => {
  2. cy.visit('/login')
  3. cy.get('input[name="username"]').type('admin')
  4. cy.get('input[name="password"]').type('admin')
  5. cy.contains('Login').click()
  6. cy.wait(500)
  7. cy.visit('/devices')
  8. cy.get('ix-menu ix-menu-item[tab-icon=cogwheel]').click() //css selector
  9. })

The wait after logging in and before visiting /devices may not be necessary, depending on the default timeout settings for your testing environment.

As @jjhelguero mentioned in the comments, you can also intercept a network request (perhaps to your login endpoint) and wait for it to return successfully as an alternative to the wait. get will retry, but could timeout: https://docs.cypress.io/api/commands/intercept

There are things you can do to avoid needing to actually log in, like setting a session cookie, but probably you want to emulate the actual user workflow in your tests.

huangapple
  • 本文由 发表于 2023年2月14日 21:40:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448685.html
匿名

发表评论

匿名网友

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

确定