1

I have very many tables in different pages of the platform and wanted to create a generic function such as - provide the table name and column label and wanted the text returned by the function back to the test.

I have imported this function into the test and am able to send the table definition without having the selector declaration in the function as well. For starters, the ability to return the tdText back to the test is what I am battling with. Any insight - or am I going about this totally wrong?

export async function columnMatcher(tableDefinition){
    const table       = tableDefinition;
    const rowCount    = await table.find('tbody > tr').count;
    const columnCount = await table.find('tbody > tr').nth(0).find('*').count;


    for(let i = 0; i < rowCount; i++) {
        for(let j = 0; j < columnCount; j++) {  
            let tdText = await table.find('tbody > tr').nth(i).find('*').nth(j).textContent;
        }
    }
}

UPDATE: I was able to just add return tdText and it worked. However, I would like this as a client function. Still figuring out working with client functions.

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
Anjana
  • 89
  • 6
  • 2
    I for one don't understand what exactly you're asking. I don't know whether this is because I don't know [tag:testcafe], or whether it's simply unclear what you're asking. Some clarification would certainly not hurt. – deceze Jan 28 '20 at 07:51
  • I was able to tackle this myself. – Anjana Jan 28 '20 at 23:37

1 Answers1

1

helper function:

import {t} from "testcafe" 

export async function columnMatcher(tableDefinition, columnValue, columnReference,columnValuer,columnReferencer){

    const table       = tableDefinition;
    const rowCount    = await table.find('tbody > tr').count;          

    for(let i = 0; i < rowCount; i++) {
        const tdText = await table.find('tbody > tr').nth(i).find('*').nth(columnValuer).textContent;
        const tdReferrer = await table.find('tbody > tr').nth(i).find('*').nth(columnReferencer).textContent;
        if (tdText == columnValue && tdReferrer == columnReference){
            const foundMatch = true
            return foundMatch;
        }
    }
}

in my test:

const foundMatch = await columnMatcher(messagingListPage.messageListtable,userdata.reasontype,userdata.templatename,columnvaluer,columnreferencer)  
    if (foundMatch != true){
        console.log ("Match was not found")
      }
Anjana
  • 89
  • 6