预期 `

` 包含 @selectedText

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

expected <h1> to contain @selectedText

问题

你要做的是比较"class"为"ellipsis-2-lines text-lg text-weight-semibold"的文本与下一页的"h1"是否相同。

这个测试抛出了一个错误:期望 <h1.text-h4.text-weight-semibold.q-mb-sm> 包含 @selectedText

我认为在使用invoke函数时出现了错误。可能甚至不可能以这种方式使用它。如何修复这个错误?

英文:

What i want to do is compare that the text in class "ellipsis-2-lines text-lg text-weight-semibold" is the same as h1 on next open page

this test throws an error: expected &lt;h1.text-h4.text-weight-semibold.q-mb-sm&gt; to contain @selectedText

I think there is an error when using invoke function. it might not even be possible to use it that way. How to fix this error?

describe(&#39;template spec&#39;, () =&gt; {
  it(&#39;visit gxpbox&#39;, () =&gt; {
    cy.visit(&#39;https://gxpbox.org/&#39;)
    cy.get(&quot;input[placeholder=\&quot;Where are you going?\&quot;]&quot;).type(&quot;Dubai&quot;)
    cy.get(&#39;[class=&quot;q-item q-item-type row no-wrap q-item--clickable q-link cursor-pointer q-focusable q-hoverable items-center&quot;]&#39;).contains(&#39;Dubai, United Arab Emirates&#39;).click()
    cy.get(&quot;button[type=\&quot;submit\&quot;]&quot;).click();
    cy.get(&#39;[class=&quot;ellipsis-2-lines text-lg text-weight-semibold&quot;]&#39;)
      .should(&#39;have.length.gt&#39;, 19)
      .its(&#39;length&#39;)
      .then((n) =&gt; Cypress._.random(0, n - 1))
      .then((k) =&gt; {
        cy.log(`picked random index ${k}`)
        cy.get(&#39;[class=&quot;ellipsis-2-lines text-lg text-weight-semibold&quot;]&#39;).eq(k).click().invoke(&#39;text&#39;).as(&#39;selectedText&#39;);
      })
    cy.get(&#39;h1&#39;).should(&quot;contain&quot;, &quot;@selectedText&quot;)
  })
})

答案1

得分: 4

你可以使用这种语法与插件 bahmutov/cypress-aliases

这方便地修改了 .should() 命令,以接受别名作为第二个参数。

import 'cypress-aliases/commands/should';

it('访问 gxpbox', () => {
  cy.visit('https://gxpbox.org/')
  ...
  cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
  .should('have.length.gt', 19)
  .its('length')
  .then((n) => Cypress._.random(0, n - 1))
  .then((k) => {
    cy.log(`选择了随机索引 ${k}`)
    cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
      .eq(k).click().invoke('text').as('selectedText');
  })
  cy.get('h1').should("contain", "@selectedText")

否则,你需要添加另一个嵌套层级

import 'cypress-aliases/commands/should';

it('访问 gxpbox', () => {
  cy.visit('https://gxpbox.org/')
  ...
  cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
  .should('have.length.gt', 19)
  .its('length')
  .then((n) => Cypress._.random(0, n - 1))
  .then((k) => {
    cy.log(`选择了随机索引 ${k}`)
    cy.get('[class="ellipsis-2-lines text-lg text-weight-semibold"]')
      .eq(k).click().invoke('text')
      .then(title => {
        cy.get('h1').should("contain", title)
      });
  })
英文:

You can use that syntax with plugin bahmutov/cypress-aliases.

This conveniently modifies the .should() command to accept an alias as a second parameter.

import &#39;cypress-aliases/commands/should&#39;

it(&#39;visit gxpbox&#39;, () =&gt; {
  cy.visit(&#39;https://gxpbox.org/&#39;)
  ...
  cy.get(&#39;[class=&quot;ellipsis-2-lines text-lg text-weight-semibold&quot;]&#39;)
  .should(&#39;have.length.gt&#39;, 19)
  .its(&#39;length&#39;)
  .then((n) =&gt; Cypress._.random(0, n - 1))
  .then((k) =&gt; {
    cy.log(`picked random index ${k}`)
    cy.get(&#39;[class=&quot;ellipsis-2-lines text-lg text-weight-semibold&quot;]&#39;)
      .eq(k).click().invoke(&#39;text&#39;).as(&#39;selectedText&#39;);
  })
  cy.get(&#39;h1&#39;).should(&quot;contain&quot;, &quot;@selectedText&quot;)

otherwise you have to add another nesting level

import &#39;cypress-aliases/commands/should&#39;

it(&#39;visit gxpbox&#39;, () =&gt; {
  cy.visit(&#39;https://gxpbox.org/&#39;)
  ...
  cy.get(&#39;[class=&quot;ellipsis-2-lines text-lg text-weight-semibold&quot;]&#39;)
  .should(&#39;have.length.gt&#39;, 19)
  .its(&#39;length&#39;)
  .then((n) =&gt; Cypress._.random(0, n - 1))
  .then((k) =&gt; {
    cy.log(`picked random index ${k}`)
    cy.get(&#39;[class=&quot;ellipsis-2-lines text-lg text-weight-semibold&quot;]&#39;)
      .eq(k).click().invoke(&#39;text&#39;)
      .then(title =&gt; {
        cy.get(&#39;h1&#39;).should(&quot;contain&quot;, title)
      });
  })

答案2

得分: 1

问题在于使用.should('contains', '@selectedText')时,第二个参数被解释为字符串而不是别名的值。相反,我们可以使用别名将其与h1元素的字符串值进行比较。

cy.get('h1').invoke('text').then((text) => { // 获取h1元素的文本
  cy.get('@selectedText').should('contain', text);
});
英文:

The issue is that when using .should(&#39;contains&#39;, &#39;@selectedText&#39;), the second parameter is being interpreted as a string and not the aliased value. Instead, we can use the alias to compare it to the string value of the h1 element.

cy.get(&#39;h1&#39;).invoke(&#39;text&#39;).then((text) =&gt; { // get the text of the h1
  cy.get(&#39;@selectedText&#39;).should(&#39;contain&#39;, text);
});

huangapple
  • 本文由 发表于 2023年2月8日 17:46:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/75383903.html
匿名

发表评论

匿名网友

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

确定