Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE#11] add: dotenv config, login tests #12

Merged
merged 11 commits into from
Nov 5, 2023
4 changes: 4 additions & 0 deletions .env-template
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BASE_URL='http://localhost:3000'
USER_EMAIL=''
USER_PASSWORD=''
USER_ID=''
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/
/test-results/
/playwright-report/
/playwright/.cache/
.env
23 changes: 8 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,15 @@
git clone https://github.com/kat-kan/kawqa-gad-playwright.git
```
2. install dependencies:
```
npm install
```
`npm install`
3. setup Playwright with:
`npx playwright install --with-deps chromium`
kat-kan marked this conversation as resolved.
Show resolved Hide resolved
4. copy application main URL as value of `BASE_URL` variable in `.env` file
5. add user email and user password to the `.env` file

more info on how to add changes to the repo - see our [Contribution Guidlines](https://github.com/kat-kan/kawqa-gad-playwright/blob/CONTRIBUTION.md/)
More info on how to add changes to the repo - see our [Contribution Guidelines](https://github.com/kat-kan/kawqa-gad-playwright/blob/CONTRIBUTION.md/)

### Running Tests

- to run all tests:

```
npx playwright test
```

- to run a specific test:

```
npx playwright test {testfilename.ts}
```
- to run all tests: `npx playwright test`
- to run a specific test: `npx playwright test {testfilename.ts}`
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
"devDependencies": {
"@playwright/test": "^1.39.0",
"@types/node": "^20.8.7",
"dotenv": "^16.3.1",
"typescript": "^5.2.2"
},
"scripts": {}
"scripts": {
bugITwhisperer marked this conversation as resolved.
Show resolved Hide resolved
"test": "npx playwright test",
"test:headed": "npm run test -- --headed",
"show-report": "npx playwright show-report"
}
}
25 changes: 11 additions & 14 deletions playwright.config.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { defineConfig, devices } from '@playwright/test';

import { defineConfig, devices } from "@playwright/test";
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();
require("dotenv").config();

/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests',
testDir: "./tests",
/* Run tests in files in parallel */
globalSetup: "./src/global-setup.ts",
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
Expand All @@ -23,20 +23,18 @@ export default defineConfig({
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
// baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
baseURL: process.env.BASE_URL,
ignoreHTTPSErrors: true,
trace: "retain-on-failure",
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
name: "chromium",
use: { ...devices["Desktop Chrome"] },
},


// {
// name: 'firefox',
// use: { ...devices['Desktop Firefox'] },
Expand Down Expand Up @@ -67,8 +65,7 @@ export default defineConfig({
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
Expand Down
1 change: 0 additions & 1 deletion src/.gitkeep

This file was deleted.

1 change: 0 additions & 1 deletion src/fixtures/.gitkeep

This file was deleted.

1 change: 0 additions & 1 deletion src/fixtures/api/.gitkeep

This file was deleted.

5 changes: 5 additions & 0 deletions src/fixtures/api/auth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const testUser = {
userEmail: process.env.USER_EMAIL ?? "[NOT SET]",
userPassword: process.env.USER_PASSWORD ?? "[NOT SET]",
userId: process.env.USER_ID ?? "[NOT SET]",
};
8 changes: 8 additions & 0 deletions src/global-setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import dotenv from "dotenv";

async function globalSetup(): Promise<void> {
dotenv.config({ override: true });
console.log("⚠️ URL:", process.env.BASE_URL);
bugITwhisperer marked this conversation as resolved.
Show resolved Hide resolved
}

export default globalSetup;
1 change: 0 additions & 1 deletion tests/.gitkeep

This file was deleted.

1 change: 0 additions & 1 deletion tests/api/.gitkeep

This file was deleted.

63 changes: 63 additions & 0 deletions tests/api/login.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { test, expect } from "@playwright/test";
import { testUser } from "../../src/fixtures/api/auth";

test.describe("Login endpoint tests", async () => {
let accessToken = "";
let baseURL = process.env.BASE_URL;

test("Login endpoint returns 200 OK for correct login credentials", async ({
request,
}) => {
const expectedResponseCode = 200;

//When POST request is send to LOGIN endpoint
const response = await request.post(`${baseURL}/api/login`, {
kat-kan marked this conversation as resolved.
Show resolved Hide resolved
// And request body in JSON format is used with proper login credentials
data: {
email: testUser.userEmail,
password: testUser.userPassword,
},
});

//Then status code should be 200
const code = response.status();
expect(code).toBe(expectedResponseCode);

// And response body should have "access_token" string
const body = await response.json();
expect(JSON.stringify(body)).toContain("access_token");

// And access_token should not be empty
expect(body.access_token?.length).toBeGreaterThan(0);

// saving access token for next tests
//console.log(JSON.stringify(body));
accessToken = `Bearer ${body.access_token}`;
//console.log(accessToken);
});

test("Login endpoint returns 401 Unauthorized for incorrect login credentials", async ({
request,
}) => {
const incorrectPassword = "wrongPassword";
const expectedResponseCode = 401;
const expectedErrorMessage = "Incorrect email or password";

//When POST request is send to LOGIN endpoint
const response = await request.post(`${baseURL}/api/login`, {
// And request body in JSON format is used with proper login credentials
data: {
email: testUser.userEmail,
password: incorrectPassword,
},
});

//Then status code should be 401
const code = response.status();
expect(code).toBe(expectedResponseCode);

// And response body should have error message
const body = await response.json();
expect(body.message).toBe(expectedErrorMessage);
});
});