Skip to content
This repository has been archived by the owner on Feb 3, 2023. It is now read-only.

Commit

Permalink
Merge branch 'master' into add-offset-support-to-click-and-hover
Browse files Browse the repository at this point in the history
# Conflicts:
#	example/tests/elementActions.test.js
#	example/tests/interceptor.test.js
  • Loading branch information
vmaciulis committed Sep 28, 2020
2 parents 15c66f6 + 386d52f commit 5afeada
Show file tree
Hide file tree
Showing 16 changed files with 355 additions and 62 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@
* interceptor.waitForRequestAfterAction() waits for specific or any first request and returns all its data.
* interceptor.waitForResponseAfterAction() waits for specific or any first response and returns all its data.

### API testing ###

* apiHelpers class uses Axios library for issuing HTTP requests.
* apiHelpers.request() can be used for sending arbitrary HTTP requests.
* apiHelpers.get(), apiHelpers.post(), apiHelpers.put() and apiHelpers.delete() are shortcuts for most common request types.
* Refer to https://github.com/axios/axios documentation for more complex scenarios.

### Parallel execution ###
* By default Jest runs tests in parallel with a worker pool of child processes
* The console commands responsible for parallelization settings ([Official Jest documentation](https://jestjs.io/docs/en/cli.html)):
Expand Down
6 changes: 4 additions & 2 deletions example/pages/FeedbackPage.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Element } from "test-juggler";
import TopBar from "../components/TopBar";

export default class FeedbackPage {
class FeedbackPage {
constructor() {
this.TopBar = new TopBar();
this.TitleText = new Element("#feedback-title");
}
}
}

export default new FeedbackPage();
6 changes: 4 additions & 2 deletions example/pages/HomePage.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { Element } from "test-juggler";
import TopBar from "../components/TopBar";

export default class HomePage {
class HomePage {
constructor() {
this.TopBar = new TopBar();
this.NavigationBar = new Element("#nav");
Expand Down Expand Up @@ -32,4 +32,6 @@ export default class HomePage {
async clicOnlineFeedBackLink() {
await this.FeedBackLink.click();
}
}
}

export default new HomePage();
11 changes: 4 additions & 7 deletions example/tests/VisualRegression.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ import HomePage from "../pages/HomePage";
import FeedbackPage from "../pages/FeedbackPage";

describe("Visual Regression", () => {
let homepage = new HomePage();
let feedbackpage = new FeedbackPage();

beforeEach(async () => {
await homepage.visit();
await HomePage.visit();
console.log("Running test: " + jasmine["currentTest"].fullName);
});

Expand Down Expand Up @@ -50,7 +47,7 @@ describe("Visual Regression", () => {

it("should capture screenshot of specific page element", async () => {
//Arrange, Act
const screenshot = await (await feedbackpage.TopBar.SignInButton.wait()).screenshot();
const screenshot = await (await FeedbackPage.TopBar.SignInButton.wait()).screenshot();

//Assert
expect(screenshot).toMatchImageSnapshot();
Expand All @@ -61,7 +58,7 @@ describe("Visual Regression", () => {
await page.evaluate((selector) => {
let signinButtonDomElement = document.querySelector(selector);
signinButtonDomElement.innerText = "Expected Content";
}, feedbackpage.TopBar.SignInButton.selector);
}, FeedbackPage.TopBar.SignInButton.selector);

//Act
const screenshot = await page.screenshot();
Expand All @@ -72,7 +69,7 @@ describe("Visual Regression", () => {

it("should cover unwanted element before making screenshot comparison", async () => {
//Arrange
await homepage.NavigationBar.cover();
await HomePage.NavigationBar.cover();

//Act
const screenshot = await page.screenshot();
Expand Down
120 changes: 120 additions & 0 deletions example/tests/apiHelpers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { ApiHelpers } from "test-juggler";

describe("Helpers", () => {
let apiHelpers = new ApiHelpers({ baseURL: "https://reqres.in" });

beforeEach(async () => {
console.log("Running test: " + jasmine["currentTest"].fullName);
});

it("should issue GET request", async () => {
//Arrange, Act
const response = await apiHelpers.get("/api/users/2");

//Assert
expect(response.status).toEqual(200);
});

it("should issue POST request", async () => {
//Arrange, Act
const response = await apiHelpers.post("/api/users",
{
"name": "morpheus",
"job": "leader"
});

//Assert
expect(response.status).toEqual(201);
});

it("should issue PUT request", async () => {
//Arrange, Act
const response = await apiHelpers.put("/api/users/2",
{
"name": "morpheus",
"job": "zion resident"
});

//Assert
expect(response.status).toEqual(200);
});

it("should issue PATCH request", async () => {
//Arrange, Act
const response = await apiHelpers.patch("/api/users/2",
{
"name": "morpheus",
"job": "zion resident"
});

//Assert
expect(response.status).toEqual(200);
});

it("should issue DELETE request", async () => {
//Arrange, Act
const response = await apiHelpers.delete("/api/users/2");

//Assert
expect(response.status).toEqual(204);
});

it("should issue arbitrary request", async () => {
//Arrange, Act
const response = await apiHelpers.request({
method: "patch",
url: "/api/users/2",
data: {
"name": "morpheus",
"job": "zion resident"
}
});

//Assert
expect(response.status).toEqual(200);
});

it("should handle bad response", async () => {
//Arrange, Act
const response = await apiHelpers.get("/api/users/23");

//Assert
expect(response.status).toEqual(404);
});

it("should verify JSON response", async () => {
//Arrange, Act
const response = await apiHelpers.get("/api/users/2");

//Assert
expect(response.data.data.first_name).toEqual("Janet");
});

it("should convert XML response to JSON", async () => {
//Arrange
const response = await apiHelpers.get("https://www.crcind.com/csp/samples/SOAP.Demo.cls?soap_method=AddInteger&Arg1=1&Arg2=2");

//Act
const responseJsonData = apiHelpers.parseXml(response.data);

//Assert
expect(responseJsonData["SOAP-ENV:Envelope"]["SOAP-ENV:Body"][0].AddIntegerResponse[0].AddIntegerResult[0]).toEqual("3");
});

it("should issue request with basic auth", async () => {
//Arrange
const authedApiHelpers = new ApiHelpers({
baseURL: "https://postman-echo.com",
auth: {
username: "postman",
password: "password"
}
});

//Act
const response = await authedApiHelpers.get("/basic-auth");

//Assert
expect(response.data.authenticated).toBe(true);
});
});
13 changes: 13 additions & 0 deletions example/tests/elementActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,19 @@ describe("Element Actions", () => {
expect(await toolTip.text()).toEqual("series-2: 55");
});

it("should type element's text value", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/inputs");
const inputElement = new Element("input[type=number]");
const inputNumber = "456";

//Act
await inputElement.enterText(inputNumber);

//Assert
expect(await inputElement.value()).toEqual(inputNumber);
});

xit("should cover element", async () => {
//TODO: Test should be added and unxit`ed when DTAF-78 is implemented.
});
Expand Down
30 changes: 20 additions & 10 deletions example/tests/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,33 @@ import { Element, Helpers } from "test-juggler";
const fs = require("fs");

describe("Helpers", () => {
let helpers = new Helpers();

beforeEach(async () => {
console.log("Running test: " + jasmine["currentTest"].fullName);
});

it("should take screenshot and save to test logs directory", async () => {
it("should take screenshot, save to logs folder and return filepath", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
const fileName = Date.now();
const expectedFilePath = `./logs/Helpers/should take screenshot, save to logs folder and return filepath/${fileName}.png`;

//Act
const actualFilePath = await Helpers.takeScreenshot(fileName);

//Assert
expect(actualFilePath).toBe(expectedFilePath);
expect(fs.existsSync(actualFilePath)).toBeTruthy();
});

it("should use date now for screenshot file name when none is provided", async () => {
//Arrange
await page.goto("http://the-internet.herokuapp.com/");
const filename = Date.now();
const filepath = `./logs/Helpers/should take screenshot and save to test logs directory/${filename}.png`;

//Act
await helpers.takeScreenshot(filename);
const actualFilePath = await Helpers.takeScreenshot();

//Assert
expect(fs.existsSync(filepath)).toBeTruthy();
expect(actualFilePath).toContain(Date.now().toString().slice(0, -6));
});

it("should retry until action have succeeded", async () => {
Expand All @@ -30,7 +40,7 @@ describe("Helpers", () => {

//Act
await startButton.click();
await helpers.retry(async () => {
await Helpers.retry(async () => {
await elementToLoad.click();
});

Expand All @@ -44,7 +54,7 @@ describe("Helpers", () => {
const progressLoader = new Element("html.nprogress-busy");

//Act
await helpers.goToUrlAndLoad("https://www.jqueryscript.net/demo/jQuery-Html5-Based-Preloader-Plugin-html5loader/");
await Helpers.goToUrlAndLoad("https://www.jqueryscript.net/demo/jQuery-Html5-Based-Preloader-Plugin-html5loader/");

//Assert
await expect(progressLoader.exists()).resolves.toBeFalsy();
Expand All @@ -57,7 +67,7 @@ describe("Helpers", () => {
await page.goto("http://the-internet.herokuapp.com/iframe");

//Act
const frame = await helpers.getFrame(iFrameSelector);
const frame = await Helpers.getFrame(iFrameSelector);
const textContent = await frame.$eval(textFrameSelector, element => element.textContent);

//Assert
Expand Down
23 changes: 10 additions & 13 deletions example/tests/interceptor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ const successMessage = new Element(".alert-success");
const addToCartButton = new Element(".product-layout:nth-child(1) > div button:nth-child(1)");

describe("Interceptor", () => {
let helpers = new Helpers();
let interceptor = new Interceptor();

beforeEach(async () => {
console.log(`Running test: '${jasmine["currentTest"].fullName}'`);
//this is workaraound to avoid 'Request is already handled!' error. Shoud be removed when https://github.com/smooth-code/jest-puppeteer/issues/308 defect is fixed.
Expand All @@ -21,7 +18,7 @@ describe("Interceptor", () => {
const navBar = new Element(".navbar");
const requestUrlFragment = "topmenu";

await interceptor.abortRequests(requestUrlFragment);
await Interceptor.abortRequests(requestUrlFragment);

//Act
await page.goto(DemoGuruSite);
Expand All @@ -48,7 +45,7 @@ describe("Interceptor", () => {
await expect(navBar.exists()).resolves.toBeTruthy();

//Act
await interceptor.abortRequests(requestUrlFragment);
await Interceptor.abortRequests(requestUrlFragment);
await page.reload( { waitUntil: "networkidle2" } );

//Assert
Expand All @@ -59,7 +56,7 @@ describe("Interceptor", () => {
//Arrange
const navBar = new Element(".navbar");
const requestUrlFragment = "topmenu";
await interceptor.abortRequestsAfterAction(page.goto(DemoGuruSite), requestUrlFragment);
await Interceptor.abortRequestsAfterAction(page.goto(DemoGuruSite), requestUrlFragment);

//Assert
await expect(navBar.exists()).resolves.toBeFalsy();
Expand All @@ -73,7 +70,7 @@ describe("Interceptor", () => {

it("should block any request after action", async () => {
//Arrange
await helpers.goToUrlAndLoad(DemoOpenCartSite);
await Helpers.goToUrlAndLoad(DemoOpenCartSite);
var alertMessage = null;
page.on("dialog", dialog => {
console.log(`Alert was detected: '${dialog.message()}'`);
Expand All @@ -82,7 +79,7 @@ describe("Interceptor", () => {
});

//Act
await interceptor.abortRequestsAfterAction(addToCartButton.click());
await Interceptor.abortRequestsAfterAction(addToCartButton.click());

//Assert
await expect(successMessage.isVisible()).resolves.toBeFalsy();
Expand All @@ -91,7 +88,7 @@ describe("Interceptor", () => {

it("should count all requests", async () => {
//Act
var totalRequests = await interceptor.getAllRequestsData(helpers.goToUrlAndLoad(DemoOpenCartSite));
var totalRequests = await Interceptor.getAllRequestsData(Helpers.goToUrlAndLoad(DemoOpenCartSite));

//Assert
expect(totalRequests.length > 0).toBeTruthy();
Expand All @@ -101,10 +98,10 @@ describe("Interceptor", () => {
it("should detect specific response after action", async () => {
//Arrange
const responseUrlFragment = "cart/info";
await helpers.goToUrlAndLoad(DemoOpenCartSite);
await Helpers.goToUrlAndLoad(DemoOpenCartSite);

//Act
var responseAfterAction = await interceptor.waitForResponseAfterAction(addToCartButton.click(), responseUrlFragment);
var responseAfterAction = await Interceptor.waitForResponseAfterAction(addToCartButton.click(), responseUrlFragment);

//Assert
await expect(successMessage.isVisible()).resolves.toBeTruthy();
Expand All @@ -115,10 +112,10 @@ describe("Interceptor", () => {

it("should detect any request after action", async () => {
//Arrange
await helpers.goToUrlAndLoad(DemoOpenCartSite);
await Helpers.goToUrlAndLoad(DemoOpenCartSite);

//Act
var requestAfterAction = await interceptor.waitForRequestAfterAction(addToCartButton.click());
var requestAfterAction = await Interceptor.waitForRequestAfterAction(addToCartButton.click());

//Assert
await expect(successMessage.isVisible()).resolves.toBeTruthy();
Expand Down
Loading

0 comments on commit 5afeada

Please sign in to comment.