你如何使用 Cypress 在框架(不是 iframe)中 select 元素?断言错误

How do you select elements in a frame (not iframe) using Cypress? AssertionError

我看过诸如此类的帖子 了解如何在 Cypress 中处理 iframes。但我使用的是旧的和过时的 frames,这是遗留系统(我必须测试)使用的。

我已经检查了 Cypress 推荐的 Github -Cypress iframes,但还没有找到普通旧框架的答案。使用 iframe 的解决方案,没有奏效。

问题与 iframe 相同,当尝试 select 使用

的元素时
cy.get('input').type('test');

您收到一个 AssertionError 声明:

Timed out retrying after 4000ms: Expected to find element: input, but never found it.

如有任何建议,我们将不胜感激。

我对 <frame> 没有任何经验,但正在测试此来源

index.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN">
<html>

<head>
  <meta name="robots" content="noindex">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Example page</title>
</head>

<frameset cols="150,*">
  <frame src="example_a.html">
  <frame src="example_b.html">
  <noframes>
    <body>
      <p>Alternate content</p>
    </body>
  </noframes>
</frameset>

</html>

example_b.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>

<head>
  <meta name="robots" content="noindex">
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Example page</title>
</head>

<body>
  <input />
</body>

</html>

这个测试有效

cy.visit('http://127.0.0.1:5500/index.html')  // served from vscode

cy.get('frame')
  .eq(1)                                     // 2nd frame
  .then($frame => $frame[0].contentWindow)   // it's window
  .its('document.body')                      
  .within(() => {                  // sets cy.root() to 2nd frame body
    cy.get('input')
      .type('something')
      .invoke('val')
      .should('eq', 'something')
  })

如果您有更复杂的事情要做,您可以尝试访问框架的源代码,例如,这会使框架内容位于顶部 window

cy.visit('http://127.0.0.1:5500/example_b.html')

cy.get('input')
  .type('something')
  .invoke('val')
  .should('eq', 'something')

您可以尝试这样的操作:

cy.get('#iframeSelector')
       .then(($iframe) => {
            const $body = $iframe.contents().find('body')

                cy.wrap($body)
                    .find('input')
                    .type('test')
            });