Search code examples
javascriptnode.jswebdriver-iodatatables-1.10

How to store a table of information in javascript and to retrieve the same data for validation?


I have a excel data with size as row and column as category and the price is calculated according to the row and column values. I don't want to fetch the data from excel or other external files but another js file. I tried to use datatable to store the values but i keep getting error as:

$(document).ready(function() { ^

ReferenceError: $ is not defined

I tried to use objects like,

pricetable = [
    ["size", "category" , "price"],
    [1-50, C , 500],
    [51-99, B , 400],
    [100-199, A , 300],
],

That didn't work out well either. So, Can anybody help me out on how to store this data and retrieve it ?


Solution

  • Please use this repo where I have given example for read and write excel

    https://github.com/Bharath-Kumar-S/Read_Write_excel

    I would recommend you to read the data from Excel or Json file in your beforeAll hooks and make use of it through out your test. By this way your script and data are separated.

    const parser = require('simple-excel-to-json');
    const doc = parser.parseXls2Json(`Sample.xlsx`);
    
    doc[0].forEach((data, i) => {
        console.log(`${i} ${data.name} ${data.age}`)
    })
    

    enter image description here

    enter image description here

    With testdata in js objects

    let priceTable = {
        size: [1 - 50, 51 - 99, 100 - 199],
        category: [`C`, `B`, `A`],
        price: [500, 400, 300]
    }
    
    priceTable.size.forEach((e,index)=>{
        console.log(`size ${e}`)
        console.log(`category ${priceTable.category[index]}`)
        console.log(`category ${priceTable.price[index]}`)
    })
    

    enter image description here