【问题标题】:How to print string value of a page object in Testcafe?如何在 Testcafe 中打印页面对象的字符串值?
【发布时间】:2019-03-19 20:32:38
【问题描述】:

我想使用 testcafe console.log 打印我在网页上找到的变量的字符串值。我正在尝试获取我用选择器标识的对象的文本值(下面的 frameSizetext)。该脚本正在运行,并且 .contains() 方法验证它是否包含我正在检查的文本,但我还想将文本值打印到控制台,这将有助于更轻松地调试脚本。

 `test('Test ==> Check size details are listed for article ', async t 
=> {

const form = main.searchForm
const list = main.sunglassesList
const firstRow = list.getRowByIndex(0)
const articlesList = main.articlesList
const frameSizeText = articlesList.frameSize.innerText

    await t
    .wait(1000)
    .typeText(form.searchField, '44444')
    .click(form.submitButton)
    .expect(main.sunglassesList.list.innerText)
    .contains('SKODA Bar')
    .click(firstRow.element)
    .expect(frameSizeText, 'Text matches ' +frameSizeText)
    .contains('44/22-144', 'checking framesize text')

   console.log("[DEBUG], Framesize is detailed as:" 
    +frameSizeText.toString())

   })`



 ✓ Test ==> Check frame color is detailed for article 
 [DEBUG], Framesize is detailed as:[object Object]
 ✓ Test ==> Check size details are listed for article 

我正在寻找的文本目前正在打印为“[object Object]”,见上文。如何打印这个对象的字符串值?

【问题讨论】:

    标签: javascript testing automated-tests console.log testcafe


    【解决方案1】:

    您尝试过使用 await 吗?

    fixture `Sample Test`
    .page `https://devexpress.github.io/testcafe/example/`
    
    test('Sample test', async t => {
    await t
    const sometext = await Selector('#main-form > div > div.row > div.column.col-1 > fieldset:nth-child(1) > legend').innerText;
    console.log('sometext   -- ' + sometext);
    })
    

    【讨论】:

      【解决方案2】:
      console.log("[DEBUG], Framesize is detailed as:", frameSizeText)
      

      应该可以解决问题。

      要获得复杂对象的字符串表示,您可以使用JSON.stringify 函数。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-13
        • 1970-01-01
        • 1970-01-01
        • 2021-12-22
        • 1970-01-01
        • 2015-06-08
        • 1970-01-01
        • 2016-03-31
        相关资源
        最近更新 更多