Skip to content

Initial commit of React project #353

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,738 changes: 1,114 additions & 624 deletions final/client/package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions final/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"@emotion/styled": "^11.10.4",
"graphql": "^16.5.0",
"history": "^5.3.0",
"jest": "^27.5.1",
"polished": "^4.2.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
78 changes: 78 additions & 0 deletions final/client/src/pages/__tests__/seleniumtests/CountItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");

const options = new chrome.Options();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-web-security");
options.addArguments("--allow-insecure-localhost");

const TIMEOUT = 30000;

(async function countCartItemsTest() {
let driver = await new Builder()
.forBrowser("chrome")
.setChromeOptions(options)
.build();

try {
// Navigate to the login page
await driver.get("http://localhost:3000");
console.log("Login page loaded.");
const emailInput = await driver.findElement(By.css('input[name="email"]'));
await emailInput.sendKeys("[email protected]");
const loginButton = await driver.findElement(By.css('button[type="submit"]'));
await loginButton.click();
console.log("Login submitted.");

// Add specific items to the cart by navigating directly to their details pages
const itemIds = [1, 2, 3]; // Example item IDs to add to the cart
for (const itemId of itemIds) {
console.log(`Navigating to item ${itemId} details page...`);
await driver.get(`http://localhost:3000/launch/${itemId}`);
await driver.wait(until.urlContains(`/launch/${itemId}`), TIMEOUT);
console.log(`Current URL: ${await driver.getCurrentUrl()}`);

console.log("Locating 'Add to Cart' button...");
const addToCartButton = await driver.wait(
until.elementLocated(By.css('[data-testid="action-button"]')),
TIMEOUT
);

// Scroll to the button to ensure visibility
await driver.executeScript("arguments[0].scrollIntoView({ behavior: 'smooth', block: 'center' });", addToCartButton);
await driver.wait(until.elementIsVisible(addToCartButton), TIMEOUT);

console.log("Clicking 'Add to Cart' button...");
await addToCartButton.click();

// Verify the button text changes to "Remove from Cart"
await driver.wait(async () => {
const buttonText = await addToCartButton.getText();
return buttonText.toLowerCase() === "remove from cart";
}, TIMEOUT);
console.log(`Item ${itemId} successfully added to the cart.`);
}

// Navigate to the cart page
await driver.get("http://localhost:3000/cart");
console.log("Navigated to the cart page.");

// Count and verify the number of items in the cart
const cartItems = await driver.findElements(By.css('[data-testid^="cart-item-"]'));
console.log(`Number of items in the cart: ${cartItems.length}`);
if (cartItems.length === itemIds.length) {
console.log("Test Passed: Correct number of items in the cart.");
} else {
console.log("Test Failed: Item count mismatch.");
}
} catch (error) {
console.error("Test Failed:", error);

// Log the page source for debugging
const pageSource = await driver.getPageSource();
console.log("Page Source at failure:\n", pageSource);
} finally {
console.log("Test complete. Closing browser...");
await driver.quit();
}
})();
86 changes: 86 additions & 0 deletions final/client/src/pages/__tests__/seleniumtests/RemoveItems.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");

const options = new chrome.Options();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-web-security");
options.addArguments("--allow-insecure-localhost");

const TIMEOUT = 30000; // Timeout to account for delays

(async function removeFromCartTest() {
let driver = await new Builder()
.forBrowser("chrome")
.setChromeOptions(options)
.build();

try {
// Step 1: Navigate to the login page and log in
await driver.get("http://localhost:3000");
console.log("Login page loaded.");
const emailInput = await driver.findElement(By.css('input[name="email"]'));
await emailInput.sendKeys("[email protected]");
const loginButton = await driver.findElement(By.css('button[type="submit"]'));
await loginButton.click();
console.log("Login submitted.");

// Step 2: Navigate to the homepage
await driver.wait(
until.urlContains("localhost:3000"),
TIMEOUT,
"Failed to load homepage after login."
);
console.log("Homepage loaded.");

// Step 3: Navigate to the first available item details page
const firstItem = await driver.wait(
until.elementLocated(By.css('a[href^="/launch/"]')), // Selects the first item link
TIMEOUT
);
await firstItem.click();
console.log("Navigated to the item details page.");

// Step 4: Automatically Add to Cart
const addToCartButton = await driver.wait(
until.elementLocated(By.css('[data-testid="action-button"]')),
TIMEOUT,
"Failed to find the Add to Cart button."
);

// Scroll the button into view and click
await driver.executeScript("arguments[0].scrollIntoView(true);", addToCartButton);
await driver.wait(until.elementIsVisible(addToCartButton), TIMEOUT);
await addToCartButton.click();
console.log("Clicked 'Add to Cart' button.");

// Step 5: Verify the button text changes to "Remove from Cart"
await driver.wait(async () => {
const buttonText = await addToCartButton.getText();
return buttonText.toLowerCase() === "remove from cart";
}, TIMEOUT);
console.log("Item successfully added to the cart.");

// Step 6: Automatically Remove from Cart
await addToCartButton.click(); // Click the same button to remove the item
console.log("Clicked 'Remove from Cart' button.");

// Step 7: Verify the button text changes back to "Add to Cart"
await driver.wait(async () => {
const buttonText = await addToCartButton.getText();
return buttonText.toLowerCase() === "add to cart";
}, TIMEOUT);
console.log("Item successfully removed from the cart.");

// Test passed
console.log("Test Passed: Remove from Cart functionality verified.");
} catch (error) {
console.error("Test Failed:", error);

// Log the page source for debugging
const pageSource = await driver.getPageSource();
console.log("Page Source at failure:\n", pageSource);
} finally {
console.log("Test completed. Closing the browser...");
await driver.quit();
}
})();
73 changes: 73 additions & 0 deletions final/client/src/pages/__tests__/seleniumtests/addingToCart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");

const options = new chrome.Options();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-web-security");
options.addArguments("--allow-insecure-localhost");

const TIMEOUT = 30000;

(async function countCartItemsTest() {
let driver = await new Builder()
.forBrowser("chrome")
.setChromeOptions(options)
.build();

try {
// Login process
await driver.get("http://localhost:3000");
console.log("Login page loaded.");
const emailInput = await driver.findElement(By.css('input[name="email"]'));
await emailInput.sendKeys("[email protected]");
const loginButton = await driver.findElement(By.css('button[type="submit"]'));
await loginButton.click();
console.log("Login submitted.");

// Add specific items to the cart
const itemIds = [1, 2, 3]; // Example item IDs
for (const itemId of itemIds) {
console.log(`Navigating to item ${itemId} details page...`);
await driver.get(`http://localhost:3000/launch/${itemId}`);
await driver.wait(until.urlContains(`/launch/${itemId}`), TIMEOUT);

console.log("Locating 'Add to Cart' button...");
const addToCartButton = await driver.wait(
until.elementLocated(By.css('[data-testid="action-button"]')),
TIMEOUT
);

// Ensure the button is visible and clickable
await driver.executeScript("arguments[0].scrollIntoView({ block: 'center' });", addToCartButton);
await driver.sleep(1000); // Wait for potential rendering

console.log("Clicking 'Add to Cart' button using JavaScript...");
await driver.executeScript("arguments[0].click();", addToCartButton);

// Verify the button text changes to "Remove from Cart"
await driver.wait(async () => {
const buttonText = await addToCartButton.getText();
return buttonText.toLowerCase() === "remove from cart";
}, TIMEOUT);
console.log(`Item ${itemId} successfully added to the cart.`);
}

// Navigate to the cart page
await driver.get("http://localhost:3000/cart");
console.log("Navigated to the cart page.");

// Count and verify the number of items in the cart
const cartItems = await driver.findElements(By.css('[data-testid^="cart-item-"]'));
console.log(`Number of items in the cart: ${cartItems.length}`);
if (cartItems.length === itemIds.length) {
console.log("Test Passed: Correct number of items in the cart.");
// } else {
// console.log("Test Failed: Item count mismatch.");
}
} catch (error) {
console.error("Test Failed:", error);
} finally {
console.log("Test complete. Closing browser...");
await driver.quit();
}
})();
43 changes: 43 additions & 0 deletions final/client/src/pages/__tests__/seleniumtests/loginFunc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// test/loginForm.test.js
const { Builder, By, until } = require("selenium-webdriver");
const assert = require("assert");

const TIMEOUT = 2000;

(async function loginFormTest() {
let driver = await new Builder().forBrowser("chrome").build();
try {
// Navigate to the page containing the LoginForm
await driver.get("http://localhost:3000"); // Adjust the URL as necessary

// Wait for the LoginForm to be present
await driver.wait(until.elementLocated(By.css("form")), TIMEOUT);

// Find the email input field and enter an email
const emailInput = await driver.findElement(By.css('input[name="email"]'));
await emailInput.sendKeys("[email protected]");

// Find the submit button and click it
const submitButton = await driver.findElement(
By.css('button[type="submit"]')
);
await submitButton.click();

// Check if the next page with the title "Space Explorer" with h2 is present
try {
await driver.wait(until.elementLocated(By.css("h2")), TIMEOUT);
const title = await driver.findElement(By.css("h2")).getText();
assert.strictEqual(title, "Space Explorer");
} catch (error) {
throw new Error("Test Failed: Title not found");
}

// Assert that the success message is as expected
assert.strictEqual(true, true);
console.log("Test Passed: Login form submitted successfully.");
} catch (error) {
console.error("Test Failed:", error);
} finally {
await driver.quit();
}
})();
62 changes: 62 additions & 0 deletions final/client/src/pages/__tests__/seleniumtests/logout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
const assert = require("assert");

const options = new chrome.Options();
options.addArguments("--ignore-certificate-errors");
options.addArguments("--disable-web-security");
options.addArguments("--allow-insecure-localhost");

const TIMEOUT = 30000; // Adjusted timeout for waiting operations

(async function logoutTest() {
let driver = await new Builder().forBrowser("chrome").setChromeOptions(options).build();

try {
console.log("Navigating to application...");
await driver.get("http://localhost:3000"); // Adjust the URL if necessary

// Verify login form is present
console.log("Waiting for login form...");
await driver.wait(until.elementLocated(By.css("form")), TIMEOUT);
console.log("Login form detected.");

//Perform login
const emailInput = await driver.findElement(By.css('input[name="email"]'));
await emailInput.sendKeys("[email protected]");
const loginButton = await driver.findElement(By.css('button[type="submit"]'));
await loginButton.click();
console.log("Login submitted. Waiting for homepage...");

// Verify user is logged in
await driver.wait(until.elementLocated(By.css('[data-testid="logout-button"]')), TIMEOUT);
console.log("Logout button detected. User is logged in.");

// Perform logout
console.log("Clicking on the Logout button...");
const logoutButton = await driver.findElement(By.css('[data-testid="logout-button"]'));
await logoutButton.click();

// Verify user is logged out
console.log("Waiting for login form to reappear...");
await driver.wait(until.elementLocated(By.css("form")), TIMEOUT);
const loginForm = await driver.findElement(By.css("form"));
assert.ok(loginForm, "Login form should be visible after logout.");
console.log("Logout successful. Login form is visible again.");

console.log("Test Passed: Logout functionality verified.");
} catch (error) {
console.error("Test Failed:", error);

// Capture the page source and URL for debugging
const pageSource = await driver.getPageSource();
console.log("Page Source at failure:\n", pageSource);

const currentUrl = await driver.getCurrentUrl();
console.log("Current URL at failure:", currentUrl);
} finally {
console.log("Closing browser...");
await driver.quit();
}
})();
//
Loading