一个图标存在于所有页面,如何在不使用重复代码的情况下进行编程?

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

An icon exists in all pages, how to code this without using repetitive code?

问题

以下是翻译好的内容:

在所有网页中都显示了一个小的聊天图标。如何在 Cypress 中编写?

这个小的聊天图标具有 Widget 的类,并且应该在所有页面中找到。

我可以像下面这样写一些代码,但是我想知道是否有其他方法可以摆脱重复的 should('have','Widget'),尽管在这一点上,我甚至不确定是否使用 should('have','Widget') 是一个正确的做法,但它能起作用。

cy.get('.pageA').should('have.value', 'Widget')
cy.get('.pageB').should('have.value', 'Widget')
cy.get('.pageC').should('have.value', 'Widget')
cy.get('.pageD').should('have.value', 'Widget')

我正在使用带有 Cucumber 预处理器的 Cypress。

英文:

I have a small chat icon shown in all web pages. How to write in Cypress?

The small chat icon has class of Widget and it should be found in all pages.

I could write something like this below, but I wonder if there is any other way to get rid of repetitive should('have','Widget') although, at this point I'm not even sure if using should('have','Widget') is a correct practice but it works.

cy.get('.pageA').should('have.value', 'Widget')
cy.get('.pageB').should('have.value', 'Widget')
cy.get('.pageC').should('have.value', 'Widget')
cy.get('.pageD').should('have.value', 'Widget')

I am using Cypress with Cucumber Preprocessor.

答案1

得分: 1

你可以利用自定义命令来避免编写可重用的代码。
你可以前往 cypress/support/commands.js 并编写:

Cypress.Commands.add('checkChatIcon', (page) => {
  cy.get(page).should('have.class', 'Widget')
})

在你的测试中,你只需要写:

cy.checkChatIcon('.pageA') 
cy.checkChatIcon('.pageB')
英文:

You can make use of custom commands to avoid writing reusable code.
You can go to cypress/support/commands.js and write:

Cypress.Commands.add('checkChatIcon', (page) => {
  cy.get(page).should('have.class', 'Widget')
})

In your tests you can just write:

cy.checkChatIcon('.pageA') 
cy.checkChatIcon('.pageB')

答案2

得分: 1

如果单个测试中的断言相符,您可以采用数据驱动的方法

    ['.pageA', '.pageB', '.pageC', '.pageD'].forEach(page => {

      cy.get(page).should('have.value', 'Widget')

    })

或者如果您想要单独的测试

    ['.pageA', '.pageB', '.pageC', '.pageD'].forEach(page => {

      it(`页面 ${page} 包含图标`, () => {
        cy.get(page).should('have.value', 'Widget')
      })

    });

一个来自[Cypress教程](https://github.com/cypress-io/cypress-tutorial-build-todo/blob/00b13fd2173be8a949896be783f6a7ab0c7558c2/cypress/integration/footer.spec.js#L20)的更具体的示例,

    it.only('处理筛选链接', () => {
      const filters = [
        {link: 'Active', expectedLength: 3},
        {link: 'Completed', expectedLength: 1},
        {link: 'All', expectedLength: 4}
      ]
      cy.wrap(filters)
        .each(filter => {
          cy.contains(filter.link)
          .click()

          cy.get('.todo-list li')
          .should('have.length', filter.expectedLength)
        })
    })
英文:

If the assertions line up within a single test, you can take a data-driven approach

['.pageA', '.pageB', '.pageC', '.pageD'].forEach(page => {

  cy.get(page).should('have.value', 'Widget')

})

or if you want individual tests

['.pageA', '.pageB', '.pageC', '.pageD'].forEach(page => {

  it(`Page ${page} has the icon`, () => {
    cy.get(page).should('have.value', 'Widget')
  })

});

A more concrete example from a Cypress tutorial,

it.only('Handles filter links', () => {
  const filters = [
    {link: 'Active', expectedLength: 3},
    {link: 'Completed', expectedLength: 1},
    {link: 'All', expectedLength: 4}
  ]
  cy.wrap(filters)
    .each(filter => {
      cy.contains(filter.link)
      .click()

      cy.get('.todo-list li')
      .should('have.length', filter.expectedLength)
    })
})

答案3

得分: 0

你应该考虑将图标放置在所有页面上的机制,并仅测试该机制。

如果你必须为每个页面执行某些操作,以确保图标存在,那么你需要测试每个页面。

如果你已经采取了某些措施,使得你编写的每个页面自动都包含了图标,那么你只需要测试你所做的那个措施。在这种情况下,编写一个单元测试来测试这个措施可能会更容易,因为它可能位于你的平台代码层次结构中的较高位置,而不仅仅是渲染单个页面。

你还可以考虑其他方法来避免测试每个页面:

  • 随机选择页面,使得每次运行测试都会测试一个随机页面。
  • 创建一个 @slow 测试,以间隔较长时间运行,测试每个页面。
    • 你可以使用爬虫或其他方法获取要进行此测试的页面列表。
    • 你可以利用这种机制将其他全局测试组合在一起。

总的来说,为共同内容的每个页面进行测试是非常不好的做法。它会导致运行时开销巨大,随着规模的扩大而变得糟糕,并且几乎没有任何好处。

英文:

You should think about the mechanism that puts the icon on all pages and just test that.

If you have to do something for each page you write to ensure the icon is present then you will need to test every page.

If you have done something so that every page you write automatically has the icon then you only need to test the thing you have done. In this case it might be easier to write a unit test to test this something, as its probably something higher up your platforms code hierarchy than the rendering a single page.

Other approaches you could consider is alternatives to testing every page are

  • randomizing the page choice so each run tests a random page
  • creating a @slow test that tests every page that you run infrequently
    • you could get a list of pages for this using a spider, or by other methods
    • you could combine tests for other global things using this mechanism

In general testing every page for a piece of common content is very poor practice. It creates a large run-time overhead that scales terribly and provides very little benefit.

huangapple
  • 本文由 发表于 2020年10月15日 10:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/64363985.html
匿名

发表评论

匿名网友

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

确定