赛普拉斯:contains()。click()带有或语句

发布于 2025-02-10 04:51:56 字数 518 浏览 1 评论 0原文

问题在于,从柏树中的Docker映像将浏览器语言设置为英语,并且页面上的某些元素被翻译成英语。看来这是柏树中的错误,因为Docker映像中的浏览器,无论设定语言如何,都将某些文本转换为英语。即使设置浏览器语言不同。

我本地的浏览器语言与Docker映像中的语言不同,因此某些文本对我而言与Dockerimage(英语)中的文本不同。现在,我必须建立解决方法,直到柏树设法修复该错误为止。

我希望柏树选择一个由逻辑或(||)选择的元素。但是,它行不通,因为cypress.contains()不支持此。 对于更好的说明,这里是我的意思的简单示例。您有一个想法如何实施吗?

const value1 = data.text_local_language
const value2 = data.text_english

         cy.get("element")
          .contains(value1 || value2)
          .click();

the problem is that the Docker image from Cypress sets the browser language to english and some elements on the page are translated to english. It looks like it's a bug in Cypress because the browser in the Docker image, regardless of the set language, translates certain texts into English. even if the set browser language is different.

my local browser language is different than the one in the docker image so some texts are different for me locally than in the dockerimage (english). now i have to build a workaround until cypress manages to fix the bug.

i want cypress to select an element which is selected by a logical or ( || ). However it doesn't work because cypress.contains() doesn't support this.
For a better illustration here is a simple example of what I mean. Do you have an idea how to implement this?

const value1 = data.text_local_language
const value2 = data.text_english

         cy.get("element")
          .contains(value1 || value2)
          .click();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扎心 2025-02-17 04:51:56

您可以使用 continains() /a>

const value1 = data.text_local_language
const value2 = data.text_english

const regex = new RegExp(`${value1}|${value2}`)  // build regex from variables

cy.get("element")
  .contains(regex)
  .click();

正则表达式(在斜线内)value1 | value2匹配value1value2 is用管道符号|表示。

You can use a regular expression in contains()

const value1 = data.text_local_language
const value2 = data.text_english

const regex = new RegExp(`${value1}|${value2}`)  // build regex from variables

cy.get("element")
  .contains(regex)
  .click();

The regular expression (inside the slashes) value1|value2 matches either value1 or value2 (or is denoted by the pipe-symbol |).

请止步禁区 2025-02-17 04:51:56

oneof 如果确切的匹配是可以接受。

cy.get('element')
  .should('satisfy', ($el) => {
    expect($el.text()).to.be.oneOf([value1,value2])
  })
  .click()

There is the oneOf assertion if an exact match is is acceptable.

cy.get('element')
  .should('satisfy', ($el) => {
    expect($el.text()).to.be.oneOf([value1,value2])
  })
  .click()
昵称有卵用 2025-02-17 04:51:56

您可以查看文本包含中的文本。

const thisText = 'this'
const thatText = 'that'

cy.get('element')
  .then($el => {
    const text =  $el.text()
    const thisOrThat = text.includes(thisText) || text.includes(thatText)
    expect(thisOrThat, `text contains ${thisText} or ${thatText}`).to.be.true
    cy.wrap($el).click()
  })

You can check the text contains either text inside a .then() command and assert it.

const thisText = 'this'
const thatText = 'that'

cy.get('element')
  .then($el => {
    const text =  $el.text()
    const thisOrThat = text.includes(thisText) || text.includes(thatText)
    expect(thisOrThat, `text contains ${thisText} or ${thatText}`).to.be.true
    cy.wrap($el).click()
  })
情话已封尘 2025-02-17 04:51:56

要断言元素包含替代零件您可以在contains()中使用正则表达式(REGEXP)。

surstert 包含 regexp,

从变量中构建正则模式,使用constructor 使用字符串表达式使用新的Regexp

const value1 = 'Hello'
const value2 = 'World'

// simple with plus operator
cy.get("element").contains(new RegExp(value1 + '|' + value2))

// short and expressive with ES6 template literals
cy.get("element").contains(new RegExp(`${value1}|${value2}`))

// for multiple elements use Array.join()
const elements = ['Fire', 'Air', 'Water'];
cy.get("element").contains(new RegExp(elements.join('|'))

另请参见文本内容

// use cy.contains to find an element with its text
// matching the given regular expression
cy.contains('[data-testid="greeting"]', /^Hello/)

配置浏览器语言,以您的容器化柏树

如果要配置用于测试的语言,则可以:

在柏树中设置浏览器语言

To assert that an element contains alternative parts you can use a regular expression (RegExp) inside contains().

Assert Contains RegExp

To build a regex pattern from variables use the constructor new RegExp with a string expression.

const value1 = 'Hello'
const value2 = 'World'

// simple with plus operator
cy.get("element").contains(new RegExp(value1 + '|' + value2))

// short and expressive with ES6 template literals
cy.get("element").contains(new RegExp(`${value1}|${value2}`))

// for multiple elements use Array.join()
const elements = ['Fire', 'Air', 'Water'];
cy.get("element").contains(new RegExp(elements.join('|'))

See also the Cypress Assertions guide on Text Content:

// use cy.contains to find an element with its text
// matching the given regular expression
cy.contains('[data-testid="greeting"]', /^Hello/)

Configure browser language in your containerized Cypress

If you want to configure the language used for testing, you could:

  • set the LANG environment variable in your docker container, which can then be picked by the browser as system/default locale
  • set the browser preferences in Cypress's Browser Launch API, which will then be used in the accept-language request header

See Setting the browser language in Cypress

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文