Rumah > Soal Jawab > teks badan
Saya cuba mencipta folder untuk menyimpan tangkapan skrin yang ditangkap daripada Cypress dalam folder tersuai dalam tangkapan skrin, di mana dalam folder ScreenshotFolder folder baharu dengan folder dengan tarikh tetapi ia tidak berfungsi.
Masalahnya ialah apabila kod run.cypress() dilaksanakan, pada akhirnya ia menukar laluan yang saya letakkan dan mengekalkan laluan lalai.
Kod boleh dilaksanakan seperti berikut:
const cypress = require('cypress'); const fs = require('fs'); const path = require('node:path'); //Function that create the test run var createRunTest = async function (info, folderNameResults){ //Datetime will be modified every time that the function is called var datetime2 = new Date(); //test start date datetime2 = datetime2.toISOString().slice(0, 19).replace('T', '_'); datetime2 = datetime2.replace(/:\s*/g, '_'); //Then I create the folders of the results for reports and screenshots with the number of execution and his datetime //Creation datetime folder in reports (time it runs) var reportsFolder = path.join(__dirname, 'cypress', 'reports', folderNameResults, cronInfo.execution_num + '_' + datetime2); fs.mkdir(reportsFolder, (err) => { if (err) { if (err.code == 'EEXIST') return console.error("file exist"); return console.error(err); } }); //Creation datetime folder in screenshots (time it runs) var screenshotsFolder = path.join(__dirname, 'cypress', 'screenshots', folderNameResults, info.execution_num + '_' + datetime2); fs.mkdir(screenshotsFolder, (err) => { if (err) { if (err.code == 'EEXIST') return console.error("file exist"); return console.error(err); } }); console.log("It should be: ", screenshotsFolder); let results = await cypress.run({ browser: 'chrome', configFile: __dirname + '/cypress.config.js', spec: __dirname + '/cypress/e2e/investigation/testWeb.cy.js', //put your test here reporter: "cypress-multi-reporters", reporterOptions: { "reporterEnabled": "mochawesome", "mochawesomeReporterOptions": { "reportDir": reportsFolder + "/json/", "overwrite": false, "html": false, "json": true } }, videosFolder: __dirname + '/cypress/videos', screenshotsFolder: screenshotsFolder, }); console.log("But it is this", results.config.screenshotsFolder); info.execution_num += 1; return; } //Here i have information of execution times var info = { id: 1 created: new Date().toISOString().slice(0, 10), execution_num: 0, //execution number } var folderNameResults = info.id + '_' + info.created; //here i create a folder with folderNameResults in directories "cypress/reports/ and cypress/screenshots" //i.e. remaining as follow: cypress/reports/1_05_17_2023 (and the same with screenshots) fs.mkdir(__dirname + '/cypress/reports/' + folderNameResults, (err) => { //creation in REPORTS if (err) { if (err.code == 'EEXIST') return console.error("file exist"); return console.error(err); } }); fs.mkdir(__dirname + '/cypress/screenshots/' + folderNameResults, (err) => { //creation in SCREENSHOTS if (err) { if (err.code == 'EEXIST') return console.error("file exist"); return console.error(err); } }); //Then i call the function to create a execution test console.log("FIRST EXECUTION"); //increment +1 execution number (1) createRunTest(info, folderNameResults).then( () => { console.log("SECOND EXECUTION"); //increment +1 execution number (2) createRunTest(info, folderNameResults); });
Pada pelaksanaan pertama, output menunjukkan bahawa ia tidak berfungsi:
It should be: C:\Users\xeom\Desktop\Ayudantia\v2_script\script/cypress/screenshots/1_2023-05-17/0_2023-05-17_19_32_30 But it is this C:\Users\xeom\Desktop\Ayudantia\v2_script\script\cypress\screenshots
Jadi apa yang berlaku adalah seperti yang ditunjukkan di bawah:
Fail yang mengandungi tangkapan untuk setiap pelaksanaan disimpan di luar folder yang anda buat dan juga ditimpa dalam folder testWeb.cy.js (setiap folder pelaksanaan harus mempunyai folder bernama seperti ini). < /p>
Selain itu, kita dapat melihat, melalui laporan, bahawa ia berfungsi dengan baik.
Bagaimana untuk membetulkannya?
P粉3561286762024-03-30 13:47:10
Wandille betul, anda hanya menetapkan konfigurasi di tempat yang salah - jadi saya mengesyaki anda bergelut untuk mempelajari asasnya.
Saya syorkan menggunakan before()
设置屏幕截图路径,这样更改不仅适用于模块 API 调用,还适用于 cypress run
和 cypress open
.
before(() => { const folderName = 'ROX'; const timestamp = (new Date()) .toISOString() .slice(0, 19) .replace('T', '_') .replace(/:\s*/g, '_') const myScreenshotsFolder = `${folderName}/${timestamp}` Cypress.Commands.overwrite('screenshot', (originalFn, subject, ...args) => { let {name, userOptions} = args name = `${myScreenshotsFolder}/${(name || Cypress.currentTest.title)}` originalFn(subject, name, userOptions) }) })
Nota:
Laluan tambahan yang anda mahukan myScreenshotsFolder
会自动添加到基本路径 cypress/screenshots
Nama fail definisi ujian anda cy.screenshot('some-file-name')
akan digunakan, jika tidak, tajuk ujian akan digunakan. Ini mengikuti amalan Cypress semasa.
before()
应放入 cypress/support/e2e.js
fail untuk kegunaan global
P粉8284636732024-03-30 09:35:11
screenshotsFolder
应位于 config
bahagian
let results = await cypress.run({ browser: 'chrome', configFile: __dirname + '/cypress.config.js', //spec: __dirname + '/cypress/e2e/investigacion/testWeb.cy.js', reporter: "cypress-multi-reporters", reporterOptions: { "reporterEnabled": "mochawesome", "mochawesomeReporterOptions": { "reportDir": "cypress/reports/" + folderName + '/' + datetime + "/json/", //"reportDir": "cypress/reports/json/", "overwrite": false, "html": false, "json": true } }, config:{ videosFolder: __dirname + '/cypress/videos', screenshotsFolder: screenshotsFolder } });
KodSumber