PM2 Testcafe fs操作导致崩溃

如果我使用TestCafe在pm2下运行我的应用程序,则如果Testcafe脚本使用FS操作,pm2总是崩溃。

        async function unlinkFiles () {
            if(fs.existsSync(downloadsFolder()+'/export.xml')){
                fs.unlinkSync(downloadsFolder()+'/export.xml');
            }
            if(fs.existsSync(downloadsFolder()+'/export.json')){
                fs.unlinkSync(downloadsFolder()+'/export.json');
            }
            return true;
        }

如果我注释掉此fs操作pm2不会在此位置崩溃,而是稍后崩溃。因此,每次我在pm2下的TestCafe脚本中使用FS操作时,pm2都会崩溃。这是已知问题吗?

我的脚本:

import { Selector,ClientFunction } from 'testcafe'; 
import fs from "fs"; 
import downloadsFolder from "downloads-folder"; 
import xpath from 'xpath'; 
import jp from 'jsonpath'; 
import {DOMParser} from 'xmldom'; 

fixture `SMTest 1-11-2019 (23:31:12)`

const getStyleWidthInPercents = ClientFunction(() => { 
   var calVar = Math.min(100,parseInt(document.querySelector('.progress-bar.progress-bar-success').style.width)) + '%'; 
   return calVar; 
}); 

const compareValues = ClientFunction(test1 => { 
   const enumVar = document.querySelector(test1).innerHTML; 
   return enumVar; 
}); 


test
    .httpAuth({
        username: 'xxx',password: 'xxx'
    })
    ('RV_mvK22',async t => {
        console.log('tcafeprogress:0');
        await t
            .navigateTo("xxx")
            .resizeWindow(1280,720)

        async function unlinkFiles () {
            if(fs.existsSync(downloadsFolder()+'/export.xml')){
                fs.unlinkSync(downloadsFolder()+'/export.xml');
            }
            if(fs.existsSync(downloadsFolder()+'/export.json')){
                fs.unlinkSync(downloadsFolder()+'/export.json');
            }
            return true;
        }

        console.log('tcafeprogress:5');

        await t
            .expect(await unlinkFiles()).ok()

        await t
            .wait(2000)
            .click(Selector('a').withText('Regelwerk'))
            //.typeText(Selector('#settingsTable_filter').find('.form-control.input-sm'),'tcafe')
            //.wait(2000)
            .click(Selector('button').withText('Upload'))

        console.log('tcafeprogress:10');

        await t
            .setfilesToUpload(Selector('#fileupload'),['./uploads/RV_mvk2.xml'])
            .wait(2000)

        //Cheap Developed UI with Workaround here
        await t
            .expect(getStyleWidthInPercents()).eql('100%',{timeout: 90000})

        console.log('tcafeprogress:20');
        await t
            .wait(2000)
            .click(Selector('.btn.btn-secondary.close-button[data-dismiss="modal"]'))
            .wait(2000)


            //Upload success,now search and create XML
            .typeText(Selector('#settingsTable_filter').find('.form-control.input-sm'),'RV_mvk2.xml')
            .wait(2000)
            //.debug()
            .click(Selector('button').withText('Erstellen'))
        console.log('tcafeprogress:30');

        //Prüfen ob das Fenster zu geht,wenn nicht dann abbrechen.
            //Vielleicht die ID Exists?
        await t
            .expect(Selector('#ContainerId').exists).ok('War wohl nicht erfolgreich',{ timeout: 300000 })

        const korpusid = await Selector('#ContainerId').innerText;

        console.log('tcafeprogress:40');

        //If fail,please note that the report will always report one step more.
        const value0 = await Selector('#KH').value 
await t 
  .expect(compareValues('#KH_value')).eql(value0,'Wertvergleich fehlgeschlagen',{timeout: 40000}) 
const value1 = await Selector('#KB').value 
await t 
  .expect(compareValues('#KB_value')).eql(value1,{timeout: 40000}) 
const value2 = await Selector('#KT').value 
await t 
  .expect(compareValues('#KT_value')).eql(value2,{timeout: 40000}) 
const value3 = await Selector('#Plattendicke').value 
await t 
  .expect(compareValues('#Plattendicke_value')).eql(value3,{timeout: 40000}) 
const value4 = await Selector('#Schiene_Size_x').value 
await t 
  .expect(compareValues('#Size_x_value')).eql(value4,{timeout: 40000}) 
  .wait(2000) 


        console.log('tcafeprogress:50');

        //XML DOWNLOAD
        await t
            .click(Selector('button').withText('Aktionen'))
            .click(Selector('a').withText('Xml Exportieren'));
        await t
            .wait(6000)
        await t  .expect(fs.existsSync(downloadsFolder()+'/export.xml')).ok()


        //JSON DOWNLOAD
        await t
            .click(Selector('button').withText('Aktionen'))
            .click(Selector('a').withText('Preis Exportieren'))
        await t
            .wait(6000)
        await t.expect(fs.existsSync(downloadsFolder()+'/export.json')).ok()

        console.log('tcafeprogress:70');


        //Nun müssten das XML öffnen und XPATH lesen.
        async function getXMLInfo (path,attr) {
            var xml = fs.readFileSync(downloadsFolder()+'/export.xml','utf8').toString();
            var doc = new DOMParser().parseFromString(xml);
            var enumeration = '';
            if(attr===true){
                enumeration = xpath.select1(path,doc).value;
            }else{
                enumeration = xpath.select(path,doc);
            }

            var noXML = enumeration.toString();
            noXML = noXML.replace(/\n|  |   /g,' ');
            noXML = noXML.replace(/ {1,}/g,' ');

            console.log(noXML);
            return noXML;
        }

        await t 
    .expect(await getXMLInfo('//CCSYSTEMCABINETID[1]/text()[1]',false)).eql(korpusid,'XML Wertvergleich fehlgeschlagen',{timeout: 40000}) 
await t 
    .expect(await getXMLInfo('//CabinetPart[@id=\'ID0001\'][1]/@name',true)).eql('Unterboden',{timeout: 40000}) 
await t 
    .expect(await getXMLInfo('//ExternalPart[1]/@identifier',true)).eql('schiene_xyz',{timeout: 40000}) 
await t 
    .expect(await getXMLInfo('//ExternalPart[1]/@length',true)).eql('1000',{timeout: 40000}) 
await t 
    .expect(await getXMLInfo('//point[4]/@coordinate',true)).eql('1200 23 0',{timeout: 40000}) 


        console.log('tcafeprogress:80');

        async function getJSONInfo (path) {
            var xml = fs.readFileSync(downloadsFolder()+'/export.json','utf8').toString();

            if(path==='$..*'){
                console.log("Alles");
                return xml;
            }

            var enumeration = jp.query(JSON.parse(xml),path);

            console.log(enumeration.toString());
            return enumeration.toString();
        }

        await t 
   .expect(await getJSONInfo('$..CumulatedList[1].Name')).eql('schiene_xyz','JSON Wertvergleich fehlgeschlagen',{timeout: 40000}) 


        console.log('tcafeprogress:100');

    });

PM2因报告而崩溃。我不知道为什么显示JSON错误,因为此步骤没有JSON函数。通过使用try and error调试代码,我能够将问题减少到node fs操作。

PM2 Testcafe fs操作导致崩溃

delong521 回答:PM2 Testcafe fs操作导致崩溃

对于所有有类似问题的人。在我的情况下,问题是节点模块“下载文件夹”中的子进程执行程序。删除Exec语句后,崩溃消失了。

本文链接:https://www.f2er.com/3167977.html

大家都在问