diff --git a/README.md b/README.md index 9ccb889..359d9b1 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,8 @@ To pick a locator, run the `codegen` command followed by URL, `npx playwright co ## Sample Test +### Unit/Integration Testing + ### Sample Web Test > Note: Refer to [sample-web-test](https://github.com/abhaybharti/playwright-framework-template/tree/master/src/tests/web/example) @@ -185,6 +187,8 @@ This will start running script in debug mode & open PlayWright inspector #### Run & Generate Report +## GitHub Actions - created workflow to run test + ## Contributing We love our contributors! Here's how you can contribute: diff --git a/src/helper/web/webHelper.ts b/src/helper/web/webHelper.ts index f98604f..c3f7e36 100644 --- a/src/helper/web/webHelper.ts +++ b/src/helper/web/webHelper.ts @@ -408,4 +408,49 @@ export class WebHelper extends Helper { }); }); } + + async changeElementValue(): Promise {} + + async verifyValueFromUi(): Promise {} + + async getAttribute(locator: string, attributeName: string): Promise { + const value = await this.webPage + .locator(locator) + .getAttribute(attributeName); + return value ?? ""; + } + + async getText(locator: string): Promise { + const value = await this.webPage.locator(locator).textContent(); + return value ?? ""; + } + + async press(key: string): Promise { + await this.webPage.keyboard.press(key); + } + + async addStep(stepDescription: string, stepFunction: any): Promise { + return await test.step(stepDescription, stepFunction); + } + + async attachScreenshot( + locator: string, + fileName: string, + testInfo: TestInfo + ): Promise { + const file = testInfo.outputPath(fileName); + const pathFile = path.dirname(file); + const pathAttachments = path.join(pathFile, "attachments"); + const attachmentFile = path.join(pathAttachments, fileName); + const screenshot = await webPage + .locator(locator) + .isVisible() + .screenshot({ path: file }); + await fs.promise.writeFile(file, screenshot); + if (!fs.existsSync(pathAttachments)) { + fs.mkdirSync(pathAttachments, { recursive: true }); + } + await fs.promises.writeFile(attachmentFile, screenshot); + await testInfo.attach(fileName, { contentType: "image/png", path: file }); + } } diff --git a/src/tests/web/example/autoWait.spec.ts b/src/tests/web/example/autoWait.spec.ts new file mode 100644 index 0000000..43417cc --- /dev/null +++ b/src/tests/web/example/autoWait.spec.ts @@ -0,0 +1,36 @@ +import { test, expect, Request, Response } from "@playwright/test"; + +//Different wait available in Playwright +// 1. waitForTimeout -- Thread.sleep() + +// 2. waitForRequest +test("test waitForRequest demo", async ({ page }) => { + const request: Request = await page.waitForRequest(/ohrm_logo.png/); + console.log(request.url()); +}); +// 3. waitForResponse +test("test waitForResponse demo", async ({ page }) => { + const response: Response = await page.waitForResponse(/ohrm_logo.png/); + console.log(response.request().url()); +}); + +// 4. waitForUrl +// 5. waitForLoadState +// 6. waitForSelector + +test("test waitForSelector demo", async ({ page }) => { + //use below approach than using waitForSelector + await expect(page.getByAltText("OrangeHRM")).toBeVisible({ timeout: 3_000 }); +}); +// 7. waitForFunction +test("test waitForFunction demo", async ({ page }) => { + await page.waitForFunction(() => { + window.scrollBy(0, 600); + }); +}); + +// 8. waitForEvent + +test("test waitForEvent demo", async ({ page }) => { + await page.waitForEvent("domcontentloaded"); +}); diff --git a/src/utils/config/uiMap/sit/orangeHRM.json b/src/utils/config/uiMap/sit/orangeHRM.json new file mode 100644 index 0000000..09a36f9 --- /dev/null +++ b/src/utils/config/uiMap/sit/orangeHRM.json @@ -0,0 +1,166 @@ +{ + "App": { + "Login": { + "objType": "page", + "OrangeHRMLogo": { + "objType": "img", + "locatorType": "xpath", + "locatorValue": "//img[@alt='company-branding']" + }, + "LoginInfo": {}, + "UsernameText": { + "objType": "text", + "locatorType": "xpath", + "locatorValue": "//label[normalize-space()='Username']", + "label": "Username" + }, + "Username": { + "objType": "input", + "locatorType": "xpath", + "locatorValue": "//input[@placeholder='Username']", + "label": "", + "validTestData": "a,abcde", + "invalidTestData": ",abcdef,%%%%,$$$$", + "saveTestData": "admin,test", + "fieldErrorMessage": "Required,invalid error" + }, + "PasswordText": { + "objType": "text", + "locatorType": "xpath", + "locatorValue": "//label[normalize-space()='Password']", + "label": "Password" + }, + "Password": { + "objType": "input", + "locatorType": "xpath", + "locatorValue": "//input[@placeholder='Password']", + "label": "", + "validTestData": "", + "invalidTestData": "", + "saveTestData": "", + "fieldErrorMessage": "" + }, + "Login": { + "elementType": "button", + "locatorType": "xpath", + "locatorValue": "//button[normalize-space()='Login']", + "labelName": "Login" + }, + "Forgotyourpassword": { + "elementType": "link", + "locatorType": "xpath", + "locatorValue": "//p[normalize-space()='Forgot your password?']", + "labelName": "Forgot your password?" + } + }, + "Admin": { + "objType": "sidebar", + "locatorType": "xpath", + "locatorValue": "//a[@class='oxd-main-menu-item active']//span[1]", + "label": "Admin", + "UserManagement": { + "objType": "tab", + "locatorType": "xpath", + "locatorValue": "//span[normalize-space()='User Management']", + "label": "User Management", + "Users": { + "objType": "subtab", + "locatorType": "xpath", + "locatorValue": "//a[text()='Users']", + "SystemUsers": { + "objType": "section", + "locatorType": "xpath", + "locatorValue": "//*[text()='System Users']", + "UsernameText": { + "objType": "text", + "locatorType": "xpath", + "locatorValue": "//label[text()='Username']", + "label": "Username", + "Username": { + "objType": "input", + "locatorType": "xpath", + "locatorValue": "//input[@class='oxd-input oxd-input--active']", + "label": "", + "validTestData": "", + "invalidTestData": "", + "saveTestData": "", + "fieldErrorMessage": "", + "UserRoleText": { + "objType": "text", + "locatorType": "xpath", + "locatorValue": "//label[text()='User Role']", + "label": "User Role", + "UserRole": { + "objType": "input", + "locatorType": "xpath", + "locatorValue": "//div[@class='oxd-select-text-input']", + "label": "", + "validTestData": "", + "invalidTestData": "", + "saveTestData": "", + "fieldErrorMessage": "", + "EmployeeNameText": { + "objType": "text", + "locatorType": "xpath", + "locatorValue": "//label[text()='Employee Name']", + "label": "Employee Name", + "EmployeeName": { + "objType": "input", + "locatorType": "xpath", + "locatorValue": "//input[@placeholder='Type for hints...']", + "label": "", + "validTestData": "", + "invalidTestData": "", + "saveTestData": "", + "fieldErrorMessage": "", + "StatusText": { + "objType": "text", + "locatorType": "xpath", + "locatorValue": "//label[text()='Status']", + "label": "User Role", + "Status": { + "objType": "input", + "locatorType": "xpath", + "locatorValue": "//div[@class='oxd-select-text-input']", + "label": "", + "validTestData": "", + "invalidTestData": "", + "saveTestData": "", + "fieldErrorMessage": "", + "Reset": { + "elementType": "button", + "locatorType": "xpath", + "locatorValue": "//button[text()=' Reset ']", + "labelName": "Reset", + "Search": { + "elementType": "button", + "locatorType": "xpath", + "locatorValue": "//button[text()=' Search ']", + "labelName": "Search", + "Add": { + "elementType": "button", + "locatorType": "xpath", + "locatorValue": "//button[text()=' Add ']", + "labelName": "Add", + "RecordsTable": { + "objType": "table/grid", + "locatorType": "xpath", + "locatorValue": "//div[@role='table']" + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } + } +}