-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Integrating playwright with azure devops test plan, updating test cas…
…e outcome
- Loading branch information
Showing
6 changed files
with
255 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const XLSX = require('xlsx'); | ||
const fs = require('fs').promises; | ||
|
||
async function globalSetup() { | ||
console.log('Global setup is running...') | ||
const filePath = process.env.TC_STATUS_PATH | ||
|
||
if (process.env.UPDATE_TEST_PLAN == 'Yes' && process.env.PIPELINE == 'Yes') { | ||
try { | ||
// Check if the file exists | ||
try { | ||
await fs.access(filePath); | ||
// File exists, delete it | ||
await deleteFile(filePath); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
// File does not exist, no need to delete | ||
console.log(`File does not exist at ${filePath}`); | ||
} else { | ||
// Unexpected error | ||
throw err; | ||
} | ||
} | ||
|
||
// Create the Excel file | ||
await createTCStatusFile(filePath); | ||
} catch (err) { | ||
console.error('An error occurred:', err); | ||
} | ||
} else { | ||
|
||
} | ||
} | ||
|
||
async function createTCStatusFile(filePath) { | ||
const workbook = XLSX.utils.book_new(); | ||
const headers = ['Test_Case_Id', 'Test_Case_Status', 'TC_Outcome_Updated']; | ||
const worksheet = XLSX.utils.aoa_to_sheet([headers]); | ||
XLSX.utils.book_append_sheet(workbook, worksheet, process.env.TC_STATUS_SHEET); | ||
XLSX.writeFile(workbook, filePath); | ||
console.log(`Generated new test case status file : ${filePath}`); | ||
} | ||
|
||
async function deleteFile(filePath) { | ||
try { | ||
await fs.unlink(filePath); | ||
console.log(`File successfully deleted at ${filePath}`); | ||
} catch (err) { | ||
if (err.code === 'ENOENT') { | ||
// File does not exist | ||
console.log(`File does not exist at ${filePath}`); | ||
} else { | ||
// Unexpected error | ||
console.error(`Error deleting file at ${filePath}:`, err); | ||
} | ||
} | ||
} | ||
export default globalSetup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const XLSX = require('xlsx'); | ||
const path = require('path'); | ||
|
||
import AzureDevOps from '../utils/azuredevops'; | ||
|
||
async function globalTeardown() { | ||
console.log('Global teardown is running...'); | ||
const azureDevOps = new AzureDevOps(); | ||
const filePath = process.env.TC_STATUS_PATH; | ||
const sheetName = process.env.TC_STATUS_SHEET; | ||
|
||
if (process.env.UPDATE_TEST_PLAN == 'Yes' && process.env.PIPELINE == 'Yes') { | ||
await azureDevOps.updateTestCaseStatus(filePath,sheetName); | ||
} else { | ||
|
||
} | ||
} | ||
module.exports = globalTeardown; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Include playwright module | ||
const { test, expect } = require('@playwright/test'); | ||
|
||
import { writeTestStatusToExcelFile } from '../utils/excelhandler'; | ||
|
||
test.afterEach('Running after each test...', async ({ page }, testInfo) => { | ||
if (process.env.UPDATE_TEST_PLAN == 'Yes' && process.env.PIPELINE == 'Yes') { | ||
await writeTestStatusToExcelFile(testInfo); | ||
} else { | ||
} | ||
}); | ||
|
||
test('[2] testcase1', async ({ page, request }) => { | ||
console.log('test is running'); | ||
}); | ||
|
||
test('[9,12] testcase2 @test1', async ({ page }) => { | ||
console.log('test is running'); | ||
}); | ||
|
||
test('[14] testcase3', async ({ page }) => { | ||
expect(true).toBe(false); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
const { request, expect } = require('@playwright/test'); | ||
|
||
const fs = require('fs'); | ||
const XLSX = require('xlsx'); | ||
|
||
import { readExcelFile } from '../utils/excelhandler'; | ||
|
||
|
||
let apiRequest; | ||
|
||
// Initialize the API request object | ||
(async () => { | ||
apiRequest = await request.newContext(); | ||
})(); | ||
|
||
const credentials = Buffer.from(`${process.env.AZURE_DEVOPS_USER}:${process.env.AZURE_DEVOPS_PASS}`).toString('base64'); | ||
|
||
/** | ||
* Bakkappa N | ||
*/ | ||
class AzureDevOps { | ||
constructor() { | ||
} | ||
|
||
async updateTestCaseStatus(filePath, sheetName) { | ||
|
||
try { | ||
const data = await readExcelFile(filePath, sheetName); | ||
|
||
// Log the keys of the first row to check column names | ||
if (data.length > 0) { | ||
console.log('First row column headers:', Object.keys(data[0])); | ||
} else { | ||
console.error('No data found.'); | ||
return; | ||
} | ||
|
||
console.log(`Total no. of test cases : ${data.length}`); | ||
console.log('Excel data converted into JSON : ' + JSON.stringify(data)); | ||
|
||
const testPlanId = process.env.TEST_PLAN_ID; | ||
const testSuiteId = process.env.TEST_SUITE_ID; | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
let row = data[i]; | ||
const testCaseId = row['Test_Case_Id']; | ||
const testCaseStatus = row['Test_Case_Status']; | ||
const testPointId = await this.getTestPoint(testPlanId, testSuiteId, testCaseId); | ||
await this.updateTestPointStatus(testPlanId, testSuiteId, testPointId, testCaseStatus.charAt(0).toUpperCase() + testCaseStatus.slice(1)); | ||
console.log('row is : ' + row); | ||
console.log(`Test Case ID - ${testCaseId} is : ${testCaseStatus}`); | ||
} | ||
} catch (error) { | ||
console.error('Error in updating test case status:', error); | ||
throw error; // Rethrow to indicate failure | ||
} | ||
} | ||
|
||
async getTestPoint(testPlanId, testSuiteId, testCaseId) { | ||
const values = [testPlanId, testSuiteId, testCaseId]; | ||
const URL = process.env.TEST_PLAN_GET_API.replace(/{(\d+)}/g, (match, number) => values[number] || match); | ||
|
||
let getTestPointResponse = await apiRequest.get(URL, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
'Authorization': `Basic ${credentials}` | ||
}, | ||
}); | ||
|
||
const jsonResponse = await getTestPointResponse.json(); | ||
expect(getTestPointResponse.ok()).toBeTruthy(); | ||
expect(getTestPointResponse.status()).toBe(200); | ||
return jsonResponse.value[0].id; | ||
} | ||
|
||
async updateTestPointStatus(testPlanId, testSuiteId, testPointId, testCaseStatus) { | ||
const values = [testPlanId, testSuiteId, testPointId]; | ||
const URL = process.env.TEST_PLAN_PATCH_API.replace(/{(\d+)}/g, (match, number) => values[number] || match); | ||
const patchAPIResponse = await apiRequest.patch(URL, { | ||
headers: { | ||
"Content-Type": "application/json", | ||
'Authorization': `Basic ${credentials}` | ||
}, | ||
data: { | ||
"outcome": testCaseStatus // Passed, Failed, Passed, Blocked, | ||
}, | ||
}); | ||
expect(patchAPIResponse.ok()).toBeTruthy(); | ||
expect(patchAPIResponse.status()).toBe(200); | ||
} | ||
} | ||
|
||
function sleep(ms) { | ||
return new Promise(resolve => setTimeout(resolve, ms)); | ||
} | ||
|
||
export default AzureDevOps; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters