Skip to content

Commit

Permalink
added helper methos
Browse files Browse the repository at this point in the history
  • Loading branch information
abhaybharti committed Jan 30, 2024
1 parent 23035e8 commit f81d29c
Showing 1 changed file with 99 additions and 2 deletions.
101 changes: 99 additions & 2 deletions src/helper/web/webHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,126 @@ export class WebHelper {
//TBD
}

/**
* The function asserts that the current page URL matches the expected URL.
* @param {string} url - The `url` parameter is a string that represents the expected URL of a web
* page.
*/
async assertPageURL(url: string): Promise<void> {
console.log("Assertion for Page URL");
console.log(`Asserts that page url is ${url}.`);
await expect(this.webPage).toHaveURL(url);
}

/**
* The function asserts that the page title matches the expected title.
* @param {string} title - The title parameter is a string that represents the expected title of the
* web page.
*/
async assertPageTitle(title: string): Promise<void> {
console.log("Assertion for Page Title");
console.log(`Asserts that page title is ${title}.`);
await expect(this.webPage).toHaveTitle(title);
}
/**
* The function opens a new tab in a browser context, navigates to a specified URL, and returns the
* page object representing the new tab.
* @param {string} url - A string representing the URL of the webpage that you want to open in a new
* tab.
* @returns a Promise that resolves to a Page object.
*/
async openNewTab(url: string): Promise<Page> {
const pageOne = await this.browserContext.newPage();
await pageOne.goto(url);
return pageOne;
}

/**
* The function takes a screenshot of a web page and saves it as an image file.
* @param {string} imageName - The imageName parameter is a string that specifies the name of the
* screenshot image file. If no value is provided, it defaults to "screenshot.png".
*/
async takeScreenshot(imageName: string = `screenshot.png`): Promise<void> {
await this.webPage.screenshot({ path: `${imageName}`, fullPage: true });
}

/**
* The function takes a locator and an optional image name as parameters, finds the element on a web
* page using the locator, and takes a screenshot of the element.
* @param {string} locator - The `locator` parameter is a string that represents the element you want
* to take a screenshot of. It can be a CSS selector, an XPath expression, or any other valid locator
* strategy supported by the `this.webPage.locator` method.
* @param {string} imageName - The `imageName` parameter is a string that specifies the name of the
* screenshot image file. If no value is provided, it defaults to "screenshot.png".
*/
async takeScreenshotOfElement(
locator: string,
imageName: string = `screenshot.png`
): Promise<void> {
const el = await this.webPage.locator(locator);
await el.screenshot({ path: `${imageName}` });
}

/**
* The function checks if an element on a web page contains the expected text.
* @param {string} target - A string representing the target element to locate on the web page.
* @param {string} expectedText - The expected text that you want the element to contain.
*/
async elementContainText(
target: string,
expectedText: string
): Promise<void> {
console.log(
`Asserts that element ${target} contains text ${expectedText}.`
);
const el = await this.webPage.locator(target);
await expect(el).toContainText(expectedText);
}

/**
* The function checks if an element on a web page has the expected text.
* @param {string} target - The target parameter is a string that represents the locator for the
* element you want to check for text. It could be a CSS selector, an XPath expression, or any other
* valid locator strategy supported by the testing framework you are using.
* @param {string} expectedText - The expected text that the element should have.
*/
async elementHasText(target: string, expectedText: string): Promise<void> {
console.log(
`Asserts that element ${target} has expected text ${expectedText}.`
);
const el = await this.webPage.locator(target);
await expect(el).toHaveText(expectedText);
}

/**
* The function asserts that a specified element is visible on a web page.
* @param {string} target - The `target` parameter is a string that represents the locator of the
* element you want to check for visibility. It could be a CSS selector, an XPath expression, or any
* other valid locator that can be used to identify the element on the web page.
*/
async elementIsVisible(target: string): Promise<void> {
console.log(`Asserts that element ${target} is visible.`);
expect(await this.webPage.locator(target)).toBeVisible();
}

/**
* The function asserts that a specified element is not visible on a web page.
* @param {string} target - The target parameter is a string that represents the locator or selector
* for the element that you want to check for visibility. It can be a CSS selector, an XPath
* expression, or any other valid locator that can be used to identify the element on the web page.
*/
async elementIsNotVisible(target: string): Promise<void> {
console.log(`Asserts that element ${target} is not visible.`);
expect(await this.webPage.locator(target)).toBeHidden();
}

async elementHasAttributeAndValue(
target: string,
attribute: string,
attributeVal: string
): Promise<void> {
console.log(
`Asserts that '${target}' has a specific attribute '${attribute}' with the expected value '${attributeVal}'.`
);
//expect(await (target).toHaveAttribute(attribute, attributeVal));

}
}

0 comments on commit f81d29c

Please sign in to comment.