简体   繁体   English

如何使用赛普拉斯从 fixtures.CSV 文件中删除回车符(换行符)

[英]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.在我的 Cypress 测试中,我从网站导入一些数据并尝试从 fixtures CSV 文件中读取数据以进行比较。

Below is the data of my fixtures/sda_ergebnisstatistik_export/Ergebnisstatistik_5Werkstückträger_Export.csv下面是我的 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.我希望断言通过,同时将 downloads 文件夹中的 csv 数据与 fixtures 文件夹中的 csv 数据进行比较。

Here is my latest code, it reads the csv data in downloads folder and validates it with CSV file data in fixtures folder:这是我最新的代码,它读取下载文件夹中的 csv 数据,并使用 fixtures 文件夹中的 CSV 文件数据对其进行验证: 来自 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()); }); }); {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 ')。修剪()); }); }); }); });

But the assertion is failing.但断言是失败的。 Test Result from Cypress Test Runner 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. fixtures 文件中的字符串具有来自 windows 操作系统的换行符/carraige 控件(“\r\n”),而来自 downloads->csv 文件的字符串包含来自 Linux/Unix 操作系统的换行符(“\n”)。 I want to remove carraige return \r from each line in the text(of fixturesCsv).我想从文本(fixturesCsv)的每一行中删除 carraige return \r。 So I used.replace('\r\n','\n').所以我用了.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.尾随的g将选项设置为全局。

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

Example of the step is here Regex replace all newline characters with comma该步骤的示例在这里正则表达式用逗号替换所有换行符

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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