Skip to content

Commit

Permalink
Merge pull request #12 from penpot/test-fix
Browse files Browse the repository at this point in the history
Test fix
  • Loading branch information
chalapkoStanislav authored Apr 4, 2024
2 parents 461d500 + eef37ab commit 808b55b
Show file tree
Hide file tree
Showing 305 changed files with 1,592 additions and 218 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/playwright_pre.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Penpot Regression Tests on PRE env
on:
schedule: ## run tests each Thursday at 6:00am UTC
- cron: '00 6 * * 4'
- cron: '00 6 * * 1-5'
workflow_dispatch:

jobs:
Expand All @@ -23,6 +23,7 @@ jobs:
BASE_URL: ${{ secrets.BASE_URL }}
LOGIN_EMAIL: ${{ secrets.LOGIN_EMAIL }}
LOGIN_PWD: ${{ secrets.LOGIN_PWD }}
GITHUB_RUN_ID: ${{ github.run.id }}
run: npx playwright test --project=chrome -gv 'PERF'
- name: Upload Playwright Report
uses: actions/upload-artifact@v3
Expand All @@ -31,3 +32,10 @@ jobs:
name: playwright-report-chromium
path: playwright-report/
retention-days: 30
- name: Send mattermost Message
env:
CHANNEL_ID: ${{ secrets.CHANNEL_ID }}
LOGIN_ID_MATTERMOST: ${{ secrets.LOGIN_ID_MATTERMOST }}
PASSWORD_MATTERMOST: ${{ secrets.PASSWORD_MATTERMOST }}
if: always()
run: npx ts-node -e "require('./helpers/mattermost.helper.js').sendMessage()"
88 changes: 88 additions & 0 deletions helpers/mattermost.helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const axios = require('axios');
const { readResultsFromFile } = require('./saveTestResults');
// const fs = require('fs')
// const FormData = require('form-data');


const baseUrl = 'https://chat.kaleidos.net/api/v4';
const channel_id = `${process.env.CHANNEL_ID}`;

async function getToken() {
const url = `${baseUrl}/users/login`;
const requestBody = {"login_id":`${process.env.LOGIN_ID_MATTERMOST}`,"password":`${process.env.PASSWORD_MATTERMOST}`};

try {
const response = await axios.post(url, requestBody, {
headers: { 'Content-Type': 'application/json' }
});
return response.headers['token'];
} catch (error) {
console.error('Error:', error);
return null;
}
}

// async function uploadFile() {
// const url = `${baseUrl}/files`;
//
// const token = await getToken();
//
// const fileBuffer = fs.readFileSync('playwright-report.zip');
// const filename = 'playwright-report.zip';
//
//
//
// const formData = new FormData();
// formData.append('channel_id', channel_id);
// formData.append('files', fileBuffer, { filename });
//
//
// try {
// const response = await axios.post(url, formData, {
// headers: {
// 'Content-Type': 'multipart/form-data',
// Authorization: `Bearer ${token}`,
// },
// });
//
// console.log('Ответ сервера:', response.data);
// return response.data;
// } catch (error) {
// console.error('Ошибка загрузки файла:', error);
// return null;
// }
// }

async function sendMessage() {
const url = `${baseUrl}/posts`;
const token = await getToken();

// await uploadFile()
function roundNumber(num) {
return Math.round(num * 100) / 100;
}
const results = await readResultsFromFile()
const messageWithLink =
`**Total Tests** : **${results.Passed+results.Failed+results.Flaky}** :person_doing_cartwheel: **Success Percentage:** **${roundNumber(results.PercentPassed)}%**
:white_check_mark: Success: ${results.Passed}
:x: Failure: ${results.Failed}
:ballot_box_with_check: Flaky: ${results.Flaky}
:cat2: GitRun: https://github.com/penpot/penpotqa/actions/runs/${process.env.GITHUB_RUN_ID}`;
const requestBody = { channel_id: channel_id, message: messageWithLink };

try {
const response = await axios.post(url, requestBody, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
}
});

return response.data;
} catch (error) {
console.error('Error:', error);
return null;
}
}

module.exports = { sendMessage };
55 changes: 55 additions & 0 deletions helpers/saveTestResults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs');

async function readResultsFromFile() {
try {
return JSON.parse(fs.readFileSync('testResults.json', 'utf8'));
} catch (error) {
console.error('Error reading JSON file:', error);
return null;
}
}

async function updateTestResults(result, retryCount) {
if (!fs.existsSync('testResults.json')) {
const initialResults = { Passed: 0, Failed: 0, Flaky: 0, PercentPassed: 0 };
try {
fs.writeFileSync('testResults.json', JSON.stringify(initialResults, null, 2));
console.log('Test results file created:', initialResults);
} catch (err) {
console.error('Error creating testResults.json:', err);
return;
}
}

let testResults;
try {
const data = fs.readFileSync('testResults.json', 'utf8');
testResults = JSON.parse(data);
} catch (err) {
console.error('Error reading testResults.json:', err);
return;
}

if (result === 'passed') {
testResults.Passed++;
} else if (result === 'failed' && retryCount === 2) {
testResults.Failed++;
} else if (result === 'flaky') {
testResults.Flaky++;
}

const totalTests = testResults.Passed + testResults.Failed
testResults.PercentPassed = (testResults.Passed / totalTests) * 100;


try {
fs.writeFileSync('testResults.json', JSON.stringify(testResults, null, 2));
console.log('Test results updated:', testResults);
} catch (err) {
console.error('Error writing testResults.json:', err);
}
}

module.exports = { readResultsFromFile, updateTestResults};


Loading

0 comments on commit 808b55b

Please sign in to comment.