如何在打字稿量角器中逐行读取csv文件?

时间:2019-09-11 13:56:30

标签: typescript protractor

我不知道如何使用打字稿在量角器中读取或写入.csv文件?

尝试使用Google,但没有成功。

2 个答案:

答案 0 :(得分:0)

只需服用https://www.npmjs.com/package/csv

不幸的是,您不会自动拥有类型,需要编写自己的类型

答案 1 :(得分:0)

请尝试以下解决方案。 CSV文件应具有cols名字和姓氏

import {browser} from 'protractor';
import * as path from 'path';
 describe('Protractor Typescript Demo', () => {
            it('CSV File Operations', async () => {
                const papa = require('papaparse');
                const fs = require('fs');
                const fileName = 'samplebook';
                const fileLocation: string = './resources/' + fileName + '.csv';
                console.log('Getting file path.....' + fileLocation);
                // Input Windows path fix, replace Windows specific '\' with '/' character after absolute resolution
                const absolutePath = await (path.resolve(__dirname, fileLocation)).replace(/\\/g, '/');
                console.log('Geting absolute path....' + (path.resolve(__dirname)));
                const file = await fs.readFileSync  (absolutePath, 'utf8');
                const results = await papa.parse(file, {
                    header: true
                    //  delimiter: '>'
                });
                for (let i = 0; i < results.data.length - 1; i++) {
                    await readFromCSV.dataIteration(results.data[i]['Firstname'], results.data[i]['Lastname'], results.data[i]['Address']);
                }
            });
        });

        method called from different class///
         public async dataIteration(firstName: string, lastName: string, address?: string) {
                    console.log(firstName);
                    browser.waitForAngularEnabled(false);
                    await launchURL(); //custom methods*emphasized text*
                    await setFirstName(firstName);
                    await setLastName(lastName);
    }