-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
54d5510
commit 362dcef
Showing
3 changed files
with
170 additions
and
9 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 |
---|---|---|
@@ -1,32 +1,99 @@ | ||
import { request } from "@playwright/test"; | ||
import { request, expect, APIResponse } from "@playwright/test"; | ||
import exp from "constants"; | ||
export class ApiHelper { | ||
private apiContext: any; | ||
constructor(apiContext: any) { | ||
this.apiContext = apiContext.newContext(); | ||
} | ||
|
||
async hitApiEndPoint(operationType: string) { | ||
async hitApiEndPoint( | ||
operationType: string, | ||
endPoint: string, | ||
payload: object | ||
) { | ||
switch (operationType.toLowerCase()) { | ||
case "get": | ||
await this.invokeGetApi(); | ||
await this.invokeGetApi(endPoint); | ||
break; | ||
case "post": | ||
await this.invokePostApi(); | ||
await this.invokePostApi(endPoint, payload); | ||
break; | ||
case "delete": | ||
await this.invokeDeleteApi(); | ||
break; | ||
case "put": | ||
await this.invokePutApi(); | ||
await this.invokePutApi(endPoint, payload); | ||
break; | ||
|
||
default: | ||
break; | ||
} | ||
} | ||
|
||
async invokeGetApi() {} | ||
async invokeGetApi(endPoint: string) { | ||
let response; | ||
try { | ||
console.log(`endPoint: , ${endPoint} `); | ||
response = await this.apiContext.get(endPoint); | ||
expect(response.status()).toBe(200); | ||
return await response.json(); | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
async invokeDeleteApi() {} | ||
async invokePostApi() {} | ||
async invokePutApi() {} | ||
|
||
/** | ||
* The function `invokePostApi` is an asynchronous function that sends a POST request to an API | ||
* endpoint with a payload and returns the response data. | ||
* @param {string} endPoint - The `endPoint` parameter is a string that represents the URL or endpoint | ||
* of the API you want to call. | ||
* @param {object} payload - The `payload` parameter is an object that contains the data to be sent in | ||
* the request body. It is typically used to send data to the server for processing or to update a | ||
* resource. | ||
* @returns the response data as a JSON object if the response status is 200. If there is an error, it | ||
* will return the error object. | ||
*/ | ||
async invokePostApi(endPoint: string, payload: object) { | ||
let response; | ||
try { | ||
console.log(`endPoint: , ${endPoint} payload :${payload} `); | ||
response = await this.apiContext.post(endPoint, { | ||
data: payload, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
expect(response.status()).toBe(200); | ||
return await response.json(); | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
async invokePutApi(endPoint: string, payload: object) { | ||
let response; | ||
try { | ||
console.log(`endPoint: , ${endPoint} payload :${payload} `); | ||
response = await this.apiContext.put(endPoint, { | ||
data: payload, | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
expect(response.status()).toBe(200); | ||
return await response.json(); | ||
} catch (error) { | ||
return error; | ||
} | ||
} | ||
|
||
async verifyStatusCode( | ||
response: APIResponse, | ||
statusCode: number = 200 | ||
): Promise<void> { | ||
await expect( | ||
response, | ||
`${statusCode} status code was not displayed` | ||
).toBeOK(); | ||
} | ||
} |
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,86 @@ | ||
import { BrowserContext, Page, expect } from "@playwright/test"; | ||
|
||
export class WebHelper { | ||
readonly webPage: Page; | ||
readonly browserContext: BrowserContext; | ||
|
||
constructor(webPage: Page, browserContext: BrowserContext) { | ||
this.webPage = webPage; | ||
this.browserContext = browserContext; | ||
} | ||
|
||
/** | ||
* The `delay` function is an asynchronous function that waits for a specified amount of time before | ||
* resolving. | ||
* @param {number} time - The `time` parameter is a number that represents the duration of the delay | ||
* in seconds. | ||
* @returns a Promise that resolves to void. | ||
*/ | ||
async delay(time: number): Promise<void> { | ||
return new Promise(function (resolve) { | ||
setTimeout(resolve, time * 1000); | ||
}); | ||
} | ||
|
||
/** | ||
* The function clicks on an element on a web page based on its text content. | ||
* @param {string} text - The text parameter is a string that represents the text content of an element | ||
* that you want to click on. It is used to locate the element on the web page. | ||
* @param {boolean} [exact=true] - The `exact` parameter is a boolean value that determines whether the | ||
* text should be matched exactly or not. If `exact` is set to `true`, the `clickByText` function will | ||
* only click on elements that have an exact match with the provided text. If `exact` is set to ` | ||
*/ | ||
async clickByText(text: string, exact: boolean = true): Promise<void> { | ||
await this.webPage.getByText(text, { exact: exact }).click(); | ||
} | ||
|
||
async rightClickButton(locator: string): Promise<void> { | ||
await this.webPage.locator(locator).click({ button: "right" }); | ||
} | ||
|
||
async leftClickButton(locator: string): Promise<void> { | ||
await this.webPage.locator(locator).click({ button: "left" }); | ||
} | ||
|
||
async navigateToUrl(url: string): Promise<void> { | ||
await this.webPage.goto(url); | ||
} | ||
|
||
async verifyDragAndDrop( | ||
source: string, | ||
target: string, | ||
verifyText: string | ||
): Promise<void> { | ||
let draggable = await this.webPage.locator(source); | ||
let droppable = await this.webPage.locator(target); | ||
await draggable.hover(); | ||
await this.webPage.mouse.down(); | ||
await droppable.hover(); | ||
await this.webPage.mouse.up(); | ||
await expect(droppable).toContainText(verifyText); | ||
} | ||
|
||
async verifyToolTip(locator: string, hoverText: string): Promise<void> { | ||
let el = await this.webPage.locator(locator); | ||
el.hover(); | ||
await expect(el).toContainText(hoverText); | ||
} | ||
|
||
async verifyFileDownload(): Promise<void> { | ||
//TBD | ||
} | ||
|
||
async verifyNewTab(newTabUrlExpected: string): Promise<void> { | ||
//TBD | ||
} | ||
|
||
async verifyNewWindow(newWindowUrlExpected: string): Promise<void> { | ||
//TBD | ||
} | ||
async verifyFrameText(): Promise<void> { | ||
//TBD | ||
} | ||
async verifyNestedFrame(): Promise<void> { | ||
//TBD | ||
} | ||
} |
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