使用cypress在iframe中访问元素时出错

时间:2019-09-11 12:59:25

标签: jquery iframe cypress

我对cypress / javascript / jquery还是比较陌生,非常感谢您的帮助!我正在尝试一种使用cypress来访问iframe中的元素的方法,该方法在这里建议使用:https://github.com/cypress-io/cypress/issues/136#issuecomment-525994895。我的.its命令出现了柏树错误,而getInDocument自定义命令中出现了后续错误。

这是正在测试的html代码的摘录:

<iframe id="iframeDiag" style="width:100%;height:100%;border:none;padding:0px;z-index:1001;" src="https://<snip>">
#document
<snip>
<button _ngcontent-c11="" class="btn btn-md pointer btn-primary" type="button">Next</button>
<snip>
</iframe>

这是柏树代码:

cy.get("#iframeDiag")
  .iframeLoaded()
  .its('document')
  .getInDocument('button:contains(Next)')
  .trigger('click')

.get成功。 .its有此错误:

cypress_runner.js:126133 Uncaught TypeError: Cannot convert object to primitive value
at baseToString (cypress_runner.js:126133)
at toString (cypress_runner.js:134455)
at Function.trim (cypress_runner.js:136853)
at cypress_runner.js:157797
at cypress_runner.js:126802
at baseForOwn (cypress_runner.js:124887)
at cypress_runner.js:126771
at Function.forEach (cypress_runner.js:131239)
at Object._logValues (cypress_runner.js:157794)
at Object.logFormatted (cypress_runner.js:157783)
at EventEmitter. (cypress_runner.js:157457)
at EventEmitter.emit (cypress_runner.js:121451)
at EventEmitter. (cypress_runner.js:101916)
at EventEmitter.emit (cypress_runner.js:121451)
at Object.emit (cypress_runner.js:101958)
at Object. (cypress_runner.js:100786)

我应该关注TypeError吗?从表面上看,这似乎是一个柏树错误,但我意识到它可能是受测试的应用程序代码。

还是可以忽略掉它,我应该专注于.getInDocument命令中的选择器?

任何帮助将不胜感激!谢谢!

1 个答案:

答案 0 :(得分:0)

嗯,实际上,我认为没有用的东西。如果有人看到该问题,则会进行尝试。我不能赞扬这种方法的创建者,因为我不记得我从哪里得到的(沿着兔子洞!):

it ('Checking Accessibility', () => {
    // inject axe into iframes
    cy.window()
    .then((win) => {
        var iframe = win.document.getElementsByTagName('iframe')[0];
        console.log('Inject axe into iframe '+iframe.id);
        var script = iframe.contentWindow.document.createElement("script");
        script.src = '../js/axe.min.js';
        script.type = 'text/javascript';
        iframe.contentWindow.document.body.appendChild(script);
        //wait(6000);
        const axe = require('axe-core');
        return win.axe.run(iframe, { iframes: true} );
    })
    .then(({ violations }) => {
        if (violations.length) {
            cy.wrap(violations, { log: false }).each(v => {
                Cypress.log({
                    name: 'a11y error!',
                    consoleProps: () => v,
                    message: `${v.id} on ${v.nodes.length} Node${
                        v.nodes.length === 1 ? '' : 's'
                        }`
                })  
            })
        }
      return cy.wrap(violations, { log: false })
    })
    .then(violations => {
        assert.equal(
            violations.length,
            0,
            `${violations.length} accessibility violation${
            violations.length === 1 ? '' : 's'
            } ${violations.length === 1 ? 'was' : 'were'} detected`
        )
    })
});

我确实遇到了可访问性冲突,所以ax可以正常工作,但是iframe内什么都没有(并且iframe内的冲突确实存在)。