Skip to content

Commit

Permalink
Merge pull request #106 from rtjord/75-add-tests-to-frontend-using-se…
Browse files Browse the repository at this point in the history
…lenium

75 add tests to frontend using selenium
  • Loading branch information
rtjord authored Dec 9, 2024
2 parents 82560a4 + 1333905 commit 80701c1
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 6 deletions.
5 changes: 5 additions & 0 deletions frontend/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": "ts-node/register",
"extension": ["ts"],
"spec": "tests/**/*.test.ts"
}
15 changes: 12 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"test": "NODE_OPTIONS=--experimental-loader=ts-node/esm mocha tests/**/*.test.ts"
},
"type": "module",
"dependencies": {
"aws-amplify": "^6.6.6",
"next": "14.2.15",
Expand All @@ -17,18 +19,25 @@
"devDependencies": {
"@aws-amplify/backend": "^1.5.1",
"@aws-amplify/backend-cli": "^1.3.0",
"@types/node": "^20",
"@types/chai": "^5.0.1",
"@types/mocha": "^10.0.10",
"@types/node": "^20.17.9",
"@types/react": "^18",
"@types/react-dom": "^18",
"@types/selenium-webdriver": "^4.1.27",
"aws-cdk": "^2.164.0",
"aws-cdk-lib": "^2.164.0",
"chai": "^5.1.2",
"constructs": "^10.4.2",
"esbuild": "^0.24.0",
"eslint": "^8",
"eslint-config-next": "14.2.15",
"mocha": "^11.0.1",
"postcss": "^8",
"selenium-webdriver": "^4.27.0",
"tailwindcss": "^3.4.14",
"ts-node": "^10.9.2",
"tsx": "^4.19.1",
"typescript": "^5.6.3"
"typescript": "^5.7.2"
}
}
2 changes: 2 additions & 0 deletions frontend/tests/mocha.opts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--require ts-node/register
tests/**/*.test.ts
32 changes: 32 additions & 0 deletions frontend/tests/selenium/recommend.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Builder, By, until, WebDriver, WebElement } from "selenium-webdriver";
import { expect } from "chai";

describe("Recommendation Feature Tests", function () {
let driver: WebDriver; // Replace 'any' with 'WebDriver'

before(async function () {
driver = await new Builder().forBrowser("chrome").build();
});

after(async function () {
await driver.quit();
});

it("should return package recommendations", async function () {
await driver.get("https://www.teamfivepackages.com/recommend");

const inputField: WebElement = await driver.findElement(By.id("recommend-description-input"));
await inputField.sendKeys("A tool for web development");

const recommendButton: WebElement = await driver.findElement(By.id("recommend-button"));
await recommendButton.click();

const recommendations: WebElement = await driver.wait(
until.elementLocated(By.id("recommend-results")),
5000
);

const items: WebElement[] = await recommendations.findElements(By.tagName("li"));
expect(items.length).to.be.greaterThan(0);
});
});
29 changes: 29 additions & 0 deletions frontend/tests/selenium/search.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Builder, By, until, WebDriver, WebElement } from "selenium-webdriver";
import { expect } from "chai";

describe("Search Package Tests", function () {
let driver: WebDriver;

before(async function () {
driver = await new Builder().forBrowser("chrome").build();
});

after(async function () {
await driver.quit();
});

it("should return results for a valid package search", async function () {
await driver.get("https://www.teamfivepackages.com");

const searchBar: WebElement = await driver.findElement(By.id("express")); // Replace 'any' with 'WebElement'
await searchBar.sendKeys("popular-package");
await searchBar.sendKeys("\n");

const results: WebElement = await driver.wait(
until.elementLocated(By.id("search-results")),
5000
);
const text: string = await results.getText(); // Ensure 'text' is a string
expect(text).to.include("popular-package");
});
});
32 changes: 32 additions & 0 deletions frontend/tests/selenium/submit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Builder, By, until, WebDriver, WebElement } from "selenium-webdriver";
import { expect } from "chai";

describe("Submit Package Tests", function () {
let driver: WebDriver; // Replace 'any' with 'WebDriver'

before(async function () {
driver = await new Builder().forBrowser("chrome").build();
});

after(async function () {
await driver.quit();
});

it("should submit a package successfully", async function () {
await driver.get("https://www.teamfivepackages.com/submit");

// Find and interact with elements
await driver.findElement(By.id("package-name-input")).sendKeys("test-package");
await driver.findElement(By.id("package-description-input")).sendKeys("This is a test package.");
await driver.findElement(By.id("package-tags-input")).sendKeys("test, selenium");
await driver.findElement(By.id("submit-button")).click();

// Wait for success message and get text
const successMessage: WebElement = await driver.wait(
until.elementLocated(By.id("success-message")),
5000
);
const text: string = await successMessage.getText(); // Ensure 'text' is a string
expect(text).to.include("Package submitted successfully");
});
});
48 changes: 48 additions & 0 deletions frontend/tests/selenium/website.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Builder, By, until, WebDriver } from 'selenium-webdriver';
import { expect } from 'chai';
import 'mocha';

describe('Team Five Packages Website', function () {
let driver: WebDriver;

// Initialize WebDriver
before(async function () {
driver = await new Builder().forBrowser('chrome').build();
});


it('should load the homepage with correct title', async function () {
await driver.get('https://www.teamfivepackages.com');
const title = await driver.getTitle();
expect(title).to.include('Team Five Packages'); // Replace with expected title
});


it('should navigate to the Packages page', async function () {
const packagesLink = await driver.findElement(By.linkText('Packages'));
await packagesLink.click();

await driver.wait(until.urlContains('/packages'), 5000); // Adjust timeout as needed
const currentUrl = await driver.getCurrentUrl();
expect(currentUrl).to.include('/packages');
});


it('should search for a package and display results', async function () {
const searchBox = await driver.findElement(By.name('search')); // Adjust selector
await searchBox.sendKeys('example-package');

const searchButton = await driver.findElement(By.css('button[type="submit"]'));
await searchButton.click();

await driver.wait(until.elementLocated(By.css('.package-result')), 5000); // Adjust selector
const results = await driver.findElements(By.css('.package-result'));
expect(results.length).to.be.greaterThan(0); // Ensure results are displayed
});


// Quit WebDriver after tests
after(async function () {
await driver.quit();
});
});
12 changes: 9 additions & 3 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
{
"ts-node": {
"esm": true
},
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"module": "ESNext",
"moduleResolution": "bundler",
"target": "ESNext",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve",
"typeRoots": ["./node_modules/@types", "./types"],
"types": ["node", "mocha"],
"incremental": true,
"plugins": [
{
Expand All @@ -19,8 +25,8 @@
],
"paths": {
"@/*": ["./*"]
}
}
},
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts", "tests/**/*.ts"],
"exclude": ["node_modules"]
}

0 comments on commit 80701c1

Please sign in to comment.