title: How to read a columns data into a variable and share it in all test functions (single spec), in Test Cafe
body: <p>I've read multiple posts on here about reading in table data, however, every post is how to read and use that data within a single test function. How do I read a table in, and use it in any test function within a test spec?</p>
  <p>If I write this into a test function it works and gives back all the info correctly:</p>
  <pre><code>import { sAdmin } from &quot;..authentication&quot;;
  import TablePage from &quot;../TablePage&quot;;
  
  const tablePage = new TablePage();
  
  fixture `A fixture`
      .page`https://subdomain.example.com/#/table`
      .beforeEach( async t =&gt; {
          await t
              .resizeWindow(1284, 722)
              .useRole(sAdmin);
      });
  
  // this will work, but this won't share context with any other test function in the spec
  // this should be initialized at the start to be able to verify search clears
  test(`A test`, async t =&gt; {
      const tableColumn = await tablePage.woNumber;
      const tableCount = await tablePage.woNumber.count;
      const cellData = [];
  
      for (let i = 0; i &lt; tableCount; i++) {
          const text = await tableColumn.nth(i).innerText;
  
          cellData.push(text);
      }
  
      console.log('in spec - tableCount', tableCount);
      console.log('in spec - cellData', cellData);
  });
  </code></pre>
  <p>The output of both console logs is correct:</p>
  <pre><code>in spec - tableCount 4
  in spec - cellData [ '0007', '0003', '0005', '0006' ]
  
  </code></pre>
  <p>I've tried an async function in my test spec, and in my page object model (POM). Async function won't work in my spec unless it's within a test function. The one in the POM works, it'll get called, however i can't do <code>const tableCount = await tablePage.woNumber.count;</code> it will yell at me that I can't use a selector like that. It's because of the modifier .count. I adjusted the .count to be within the for loop but that just returned undefined or other data that didn't help.</p>
  <p>Example of the async function in my page object model (TablePage)</p>
  <pre><code>async rowCount() {
      const tableColumn = await tablePage.fooSelector;
      const tableCount = await tablePage.fooSelector;
      const cellData = [];
  
      for (let i = 0; i &lt; tableCount.count; i++) {
          const text = await tableColumn.nth(i).innerText;
  
          cellData.push(text);
      }
  
      console.log('in page - tableColumn', tableColumn);
      console.log('in page - tableCount', tableCount);
      console.log('in page - cellData', cellData);
  
      return tableCount;
  };
  </code></pre>
  <p>It's called with this in my spec file, not sure where to call it though:</p>
  <pre><code>const count = tablePage.rowCount();
  </code></pre>
  <p>I need this to run after the page has loaded to grab the cell data, and allow me to share context across all tests within this spec file at the very minimum. I'd prefer to put it in my POM, so it can be used elsewhere in other tests. But I'd settle for it working in my test spec without it being in a test function so it can be shared across all tests in the spec file.</p>
  <p>I've tried to do a fixture context, but that also had issues and returned undefined. Here is a before with context that I tried, that didn't work.</p>
  <pre><code>.before( async ctx =&gt; {
  
      const tableColumn = await tablePage.fooSelector;
      const tableCount = await tablePage.fooSelector;
  
      for (let i = 0; i &lt; tableCount.count; i++) {
          const text = await tableColumn.nth(i).innerText;
  
          ctx.cellData.push(text);
      }
  
      // console.log('in spec - tableCount', tableCount);
      // console.log('in in spec - cellData', cellData);
  })
  
  </code></pre>
  <p>These console logs return undefined, or objects instead of the text.</p>
  <p>Any help would be greatly appreciated. Here are resources I referenced already:</p>
  <p><a href="https://stackoverflow.com/questions/53501761/testcafe-storing-results-of-selector-in-variable">TestCafe - Storing results of Selector in variable</a></p>
  <p><a href="https://stackoverflow.com/questions/41018686/how-do-i-can-get-a-text-of-all-the-cells-of-the-table-using-testcafe">How do I can get a text of all the cells of the table using testcafe</a></p>
  <p><a href="https://stackoverflow.com/questions/55283981/testcafe-get-text-from-element">Testcafe get text from element</a></p>
  <p><a href="https://testcafe.io/documentation/402670/reference/test-api/domnodestate" rel="nofollow noreferrer">https://testcafe.io/documentation/402670/reference/test-api/domnodestate</a></p>
  <p>EDIT: I'm still looking for a way to share context of the data I get, I wasn't able to return the data back to the test spec. Maybe if I do more tinkering I can share the values I've obtained.</p>
  <p>Here is my solution that doesn't share context, but it does let me prevent code reuse in every test function.</p>
  <p>Page Object Model</p>
  <pre><code>import { Selector, t } from &quot;testcafe&quot;;
  
  class TablePage{
      constructor() {
          this.searchInput = Selector('#searchInput');
          this.tableCount = Selector('.class-selector');
      };
  
      async validateSearchResults(selector, searchText) {
          await t
              .typeText(this.searchInput, searchText)
              .pressKey('enter');
  
          const rowCount = await this.tableCount.count;
          let searchResults = []
  
          for (let i = 0; i &lt; rowCount; i++) {
              let text = await selector.nth(i).innerText;
  
              searchResults.push(text);
              await t.expect(searchResults[i]).contains(searchText);
          }
      };
  
  }
  
  export default TablePage;
  </code></pre>
  <p>Spec File</p>
  <pre><code>import { sAdmin } from &quot;..authentication&quot;;
  import TablePage from &quot;../TablePage&quot;;
  
  const tablePage = new TablePage();
  
  fixture `Test search functionality`
      .page`https://examplepage.com`
      .beforeEach( async t =&gt; {
          await t
              .useRole(sAdmin)
      });
  
  test(`User can search via order number`, async t =&gt; {
      await tablePage.validateSearchResults(tablePage.tableCount, 'foo');
  });
  </code></pre>