Automated tests 无法断言H1文本

Automated tests 无法断言H1文本,automated-tests,ui-automation,e2e-testing,web-testing,testcafe,Automated Tests,Ui Automation,E2e Testing,Web Testing,Testcafe,我试图写一些东西来检查下一页是否有“关于我们”的内容:我只是撞到了墙。这应该不难,但我在这上面花了太多时间 我们正在使用Gherking testcafe: NPM:6.9.0 TestCafe:1.0.1 Gherking Testcafe:2.0.0 我尝试过(以下所有测试都是隔离测试,也就是说所有不同的t.expect都是自己运行的): 并尝试移除等待: const h1AboutUs = Selector('h1'); await t.expect(h1AboutUs.inn

我试图写一些东西来检查下一页是否有“关于我们”的内容:我只是撞到了墙。这应该不难,但我在这上面花了太多时间

我们正在使用Gherking testcafe:

NPM:
6.9.0

TestCafe:
1.0.1

Gherking Testcafe:
2.0.0

我尝试过(以下所有测试都是隔离测试,也就是说所有不同的t.expect都是自己运行的):

并尝试移除等待:

  const h1AboutUs = Selector('h1'); 

  await t.expect(h1AboutUs.innerText).eql('About Us');
  await t.expect(h1AboutUs.innerText).contains('About Us');
  await t.expect(h1AboutUs.value).eql('About Us');
  await t.expect(Selector('html').textContent).contains('About Us');
如果我这样做,它会起作用:

这是我的测试:

When("I see the page load", async t => {
  const h1AboutUs = await Selector('h1');

  await t.expect(h1AboutUs.visible).eql(true);
  await t.hover(h1AboutUs);
  await t.expect(h1AboutUs.value).contains('about');
  console.log(h1AboutUs.value);
});
我的testCafe runner:

const createTestCafe = require('gherkin-testcafe');
const fs = require('fs');
const reportPath = './frontend/src/tests/test-reports'
let testcafe = null;

function readTestCafeConfig() {
  configData = fs.readFileSync('.testcaferc.json', 'utf8');
  const js = JSON.parse(configData);
  return getJSONValues(js, 'src');
};

function getJSONValues(obj, key) {
  var objects = [];

  for (var i in obj) {
    if (!obj.hasOwnProperty(i)) continue;
    if (i === key) {
      objects.push(obj[i]);
    }
  }
  return objects;
}

createTestCafe('localhost', 1337, 1338)
  .then(tc => {
    testcafe = tc;
    const runner = testcafe.createRunner();
    const src = readTestCafeConfig();

    return runner
      .src([src])
      .browsers('chrome')
      .reporter(['spec', {
        name: 'json',
        output: `${reportPath}/report/report.json`
      },
        {
          name: 'xunit',
          output: `${reportPath}/report/report.xml`
        }])
      // .video(`${reportPath}/videos`, {
      //   singleFile: true,
      //   failedOnly: true,
      //   pathPattern: '${USERAGENT}/${FILE_INDEX}.mp4'
      // })
      .tags('@aboutUs')
      .run();
  })
  .then(failedCount => {
    console.log('Tests failed: ' + failedCount);
    testcafe.close();
  });
我在控制台中得到的错误是:

1) Selector cannot implicitly resolve the test run in context of which it should be executed. If you need to call Selector from the Node.js API callback, pass the test controller manually via
      Selector's `.with({ boundTestRun: t })` method first. Note that you cannot execute Selector outside the test code.

      Browser: Chrome 74.0.3729 / Mac OS X 10.14.4

         12 |});
         13 |
         14 |When("I see the page load", async t => {
         15 |  const h1AboutUs = await Selector('h1');
         16 |
       > 17 |  await t.expect(h1AboutUs.visible).eql(true);
         18 |  await t.hover(h1AboutUs);
         19 |  await t.expect(h1AboutUs.value).contains('about');
         20 |  console.log(h1AboutUs.value);
         21 |});
         22 |

我希望不会看到这个错误msg

您需要实现选择器绑定到TestCafe的测试控制器以进行此类测试。请看以下示例:

const{Before}=require('cumber');
const{Selector:NativeSelector}=require('testcafe');
常量选择器=(输入,t)=>{
返回NativeSelector(input).with({boundTestRun:t});
};
在('@aboutHook',async()=>{
log('运行AGI测试');
});
给定(“我打开AGI页面”,异步t=>{
等待t.navigateTo('https://www.aggrowth.com/en-us/about-us');
});
然后(“我应该看看关于我们的检查”,async t=>{
常数h1AboutUs=选择器('h1',t);
//或const h1AboutUs=等待选择器('h1',t);如果需要
等待
.expect(h1AboutUs.visible).eql(true)
.悬停(H1);
});
您可以在中获得更多示例。 还要注意,
h1
元素没有属性
值。

您可以在TestCafe文档中了解更多关于和的信息。

我已经将我的选择器作为“必需”实现,因为它在任何地方都被使用,而且我知道TestCafe可能会将小黄瓜作为标准实现,所以我认为更改一个文件比更改多个文件更容易:)谢谢您的回答,我将尝试一下!:)这意味着我无法检查“h1”包含的文本/值是什么?这意味着您可以使用
h1AboutUs.textContent
h1AboutUs.innerText
检查
h1
元素的文本。但是像标题、段落、div等元素没有
属性。请参阅具有
属性的元素的列表。感谢您的快速详细回复:)
1) Selector cannot implicitly resolve the test run in context of which it should be executed. If you need to call Selector from the Node.js API callback, pass the test controller manually via
      Selector's `.with({ boundTestRun: t })` method first. Note that you cannot execute Selector outside the test code.

      Browser: Chrome 74.0.3729 / Mac OS X 10.14.4

         12 |});
         13 |
         14 |When("I see the page load", async t => {
         15 |  const h1AboutUs = await Selector('h1');
         16 |
       > 17 |  await t.expect(h1AboutUs.visible).eql(true);
         18 |  await t.hover(h1AboutUs);
         19 |  await t.expect(h1AboutUs.value).contains('about');
         20 |  console.log(h1AboutUs.value);
         21 |});
         22 |