1

enter image description hereI want to get the text of all links within a menu and compare them to the expected texts. I am new to testcafe.

const all_GeneralDisplayTopLink = Selector('nav.shTop').find('ul').find('li');

async areGeneralDisplayedLinksPresent(){

let gettexts =  all_GeneralDisplayTopLink;
  var Count   = await gettexts.count;
  console.log(Count + " I need help");// this return four as expected
   for (var i = 0; i < Count; i++){
    var printText = await gettexts.nth(i).textContent;// this throwing an error.
      printText++;
      console.log(printText + " I need help2");
Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
  • 1
    Check the following test example, which is based on your code:``import { Selector } from 'testcafe'; fixture `New Fixture`.page `https://8h6vt.csb.app/`; test(`New Test`,async t =>{ const all_GeneralDisplayTopLink = Selector('ul').find('li'); let gettexts = all_GeneralDisplayTopLink; var Count = await gettexts.count; for(var i = 0;i< Count; i++){ var printText = await gettexts.nth(i).textContent; await t.expect(printText).eql("Item "+(i + 1)); } });`` It works correctly on my side. Can you change it so that I will be able to reproduce the issue? – aleks-pro Aug 16 '19 at 12:26

2 Answers2

0

Try starting the for loop at 1 instead of 0: for (var i = 1; i <= Count; i++)

lostlemon
  • 744
  • 1
  • 6
  • 17
  • 2
    According to TestCafe documentation, the `index` parameter of the [`nth`](https://devexpress.github.io/testcafe/documentation/test-api/selecting-page-elements/selectors/functional-style-selectors.html#nth) method is zero-based. – aleks-pro Aug 16 '19 at 12:31
  • @AleksandrProkhorov Ah yes, you're correct. I confused `nth` with the `nth-child` css selector – lostlemon Aug 16 '19 at 14:57
  • I have tried it but am still getting an error, here are the screenshot of the code and the error. – Solomon Adekunle Aug 16 '19 at 22:02
0

here is the solution: let gettexts =all_GeneralDisplayLinks;

  var Count = await gettexts.count;

   console.log(Count);
    for(var i=0; i<= Count-1; i++){
      var printText = await gettexts.nth(i).innerText;
      console.log(printText + "please help");
      var formated= gatValues.generalDisplayLinks;// get the values from data file.
      var getFomatedValue= formated.split(",");
     await t.expect(printText).eql(getFomatedValue[i]).wait(1000);
       console.log(printText);
    }