HTML   发布时间:2022-04-15  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了javascript – 使用Puppeteer,Mocha和Chai在html标记属性中断言文本的存在大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我开始使用这些技术(包括Javascript),所以,一个初学者的问题.我正在努力弄清楚如何断言HTML属性中的给定文本是否符合预期.

HTML代码段:

Tin|out" data-com.user-edited="yes">
@H_675_10@

到目前为止,这是我的.it功能,使用了Mochai,Puppeteer和Chai(为了清晰起见,设置和拆卸已被省略:

  it('opt out of email',async function () {

        await page.setDefaultNavigationTimeout();
        await page.waitForSELEctor('.widget-title');
        const frame = page.frames().find(frame => frame.name() === 'iframe');
        const emailOptOutButton = await frame.$('#email-optout-8hv3a');
        await emailOptOutButton.click();
        const emailOptOutConfirmedValue = await frame.$('#email-optout-8hv3a',e => e.getAttribute('data-com.user-edited'))
        expect(emailOptOutConfirmedvalue).to.include('yes')


    })@H_675_10@

一切都在点击事件之前一直有效,但我的断言显然是错误的.错误是:

 AssertionError: object tested must be an array,a map,an object,a set,a String,or a weakset,but object given
@H_675_10@

我试过了

    it('opt out of email',async function () {

    await page.setDefaultNavigationTimeout();
    await page.waitForSELEctor('.widget-title');
    const frame = page.frames().find(frame => frame.name() === 'iframe');
    const emailOptOutButton = await frame.$('#email-optout-8hv3a');
    await emailOptOutButton.click();
    await page.$eval.emailOptOutConfirmedValue.getAttribute('data-com.user-edited')
    expect(emailOptOutConfirmedvalue).to.include('yes')


})
@H_675_10@

这使:

  TypeError: CAnnot read property 'getAttribute' of undefined
@H_675_10@

并且:

 it('opt out of email',async function () {

    await page.setDefaultNavigationTimeout();
    await page.waitForSELEctor('.widget-title');
    const frame = page.frames().find(frame => frame.name() === 'iframe');
    const emailOptOutButton = await frame.$('#email-optout-8hv3a');
    await emailOptOutButton.click();
    const emailOptOutConfirmedValue = await frame.$('#email-optout-8hv3a',e => e.getAttribute('data-com.user-edited'))
    assert.equal(emailOptOutConfirmedValue,'yes')


})
@H_675_10@

这使:

ReferenceError: assert is not defined
@H_675_10@

正如我所说,我是初学者所以请任何帮助表示赞赏!

最佳答案
这段代码错了:

const emailOptOutConfirmedValue = await frame.$('#email-optout-8hv3a',e => e.getAttribute('data-com.user-edited'))
expect(emailOptOutConfirmedvalue).to.include('yes')
@H_675_10@

你正在使用frame.$(selector).注意它只需要一个参数.第二个参数被忽略,你的调用返回一个DOM元素的句柄,这导致expect(…).to.include(…)失败,抱怨它无法测试那个东西.

你应该使用frame.$eval(selector,pageFunction[,...args])
代替.此调用将调用作为应用选择器结果的第二个参数传递的函数.所以代码应该是:

const emailOptOutConfirmedValue = await frame.$eval('#email-optout-8hv3a',e => e.getAttribute('data-com.user-edited'))
expect(emailOptOutConfirmedvalue).to.include('yes')
@H_675_10@

我在上面完整地保留了你的测试,但在我看来你在寻找的是属性的特定值,所以我使用这个测试:

expect(emailOptOutConfirmedvalue).to.equal('yes')
@H_675_10@

如果属性的值是baryesfoo,则包含测试将通过.

大佬总结

以上是大佬教程为你收集整理的javascript – 使用Puppeteer,Mocha和Chai在html标记属性中断言文本的存在全部内容,希望文章能够帮你解决javascript – 使用Puppeteer,Mocha和Chai在html标记属性中断言文本的存在所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。