简体   繁体   中英

How to remove carriage return characters (for new lines) from a fixtures .CSV file using Cypress

In my Cypress test, I'm importing some data from the website and trying to read data from fixtures CSV file for comparison.

Below is the data of my fixtures/sda_ergebnisstatistik_export/Ergebnisstatistik_5Werkstückträger_Export.csv固定装置 CSV 文件

I want the assertion to pass, while comparing the csv data from downloads folder with csv data from fixtures folder.

Here is my latest code, it reads the csv data in downloads folder and validates it with CSV file data in fixtures folder: 来自 Visual Studio Code IDE 的代码片段

const grouplist = [' TYP ', ' PRÜFSTAND ', ' TYP/PRÜFSTAND ', ' PRÜFSTAND/TYP ', ' WERKSTÜCKTRÄGER NUMMER ']
it('Ergebnisstatistik - check export for grouplist - Werkstückträger 🏇', function() {
  cy.log('Export the Werkstückträger Nummer table into EXCEL');
  const dayjs = require('dayjs');

  cy.get('button.icon-btn_statistik').should('be.visible').click();
  cy.contains('div>div.option', grouplist[4]).should('be.visible').click();
  cy.wait(500);
  cy.get('.icon-table').click();
  sdi.waitForTable();
  cy.get('.icon-export-table').click();
  cy.readFile(`${Cypress.config('downloadsFolder')}/Ergebnisstatistik ` +
    dayjs().format('YYYY-MM-DD') + '.csv', 'utf-8', 

{timeout: 15000}).then((downloadCsv) => { expect(downloadCsv).to.be.a('string') cy.readFile( ${Cypress.config('fixturesFolder')}/sda_ergebnisstatistik_export/Ergebnisstatistik_5Werkstückträger_Export.csv , 'utf-8', {timeout: 15000}).then((fixturesCsv) => { expect(downloadCsv.trim()).to.eq(fixturesCsv.replace('\r\n', '\n').trim()); }); }); });

But the assertion is failing. Test Result from Cypress Test Runner

The string from fixtures file has line breaks/carraige controls ("\r\n") from windows OS, where as the string from downloads->csv file contains ("\n") for new line from Linux/Unix OS. I want to remove carraige return \r from each line in the text(of fixturesCsv). So I used.replace('\r\n','\n'). But it is not working as expected for all the rows. In fact, for the first row, it is getting replaced as expected, but not for the remaining rows. Any help to resolve this issue, would be appreciated. Thanks in advance.

You would need to make the replace expression global (all instances). The trailing g sets options to global.

fixturesCsv.replace(/\r\n/g, '\n').trim()   

Example of the step is here Regex replace all newline characters with comma

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM