From e4d11cb068e3086879a89ca0bb1840f01de0c97e Mon Sep 17 00:00:00 2001 From: Arul Date: Fri, 8 Aug 2025 21:13:20 +0530 Subject: [PATCH] refined code to save a quest --- cookies.data | Bin 0 -> 404 bytes .../java/com/ghost/temple/CookieManager.java | 25 +- .../java/com/ghost/temple/ExcelUtils.java | 27 ++ .../com/ghost/temple/ReusableFunctions.java | 247 +++++++++++++++++- .../java/com/ghost/temple/StorageManager.java | 55 ++++ .../java/com/ghost/temple/hooks/Hooks.java | 13 +- src/main/resources/.~lock.testData.xlsx# | 1 + src/main/resources/testData.xlsx | Bin 6181 -> 6496 bytes .../java/com/ghost/temple/pages/Quests.java | 26 +- .../ghost/temple/stepdefs/QuestsStepDefs.java | 1 - .../resources/features/questCreator.feature | 1 + storage.json | 1 + target/classes/.~lock.testData.xlsx# | 1 + .../com/ghost/temple/CookieManager.class | Bin 3067 -> 3084 bytes .../classes/com/ghost/temple/ExcelUtils.class | Bin 3090 -> 4159 bytes .../com/ghost/temple/ReusableFunctions.class | Bin 4543 -> 11173 bytes .../com/ghost/temple/StorageManager.class | Bin 0 -> 3337 bytes .../ghost/temple/driver/DriverManager.class | Bin 3237 -> 3264 bytes .../com/ghost/temple/hooks/Hooks.class | Bin 831 -> 1293 bytes target/classes/testData.xlsx | Bin 6181 -> 6496 bytes target/cucumber-report.html | 2 +- .../compile/default-compile/inputFiles.lst | 11 +- .../default-testCompile/inputFiles.lst | 14 +- .../TEST-com.ghost.temple.AppTest.xml | 8 +- .../TEST-com.ghost.temple.TestRunner.xml | 118 +++++++-- .../com.ghost.temple.AppTest.txt | 2 +- .../com.ghost.temple.TestRunner.txt | 39 ++- .../com/ghost/temple/pages/HomePage.class | Bin 1572 -> 1572 bytes .../com/ghost/temple/pages/Quests.class | Bin 2117 -> 3370 bytes .../temple/stepdefs/HomePageStepDefs.class | Bin 1230 -> 1230 bytes .../temple/stepdefs/QuestsStepDefs.class | Bin 1003 -> 952 bytes .../features/questCreator.feature | 1 + 32 files changed, 528 insertions(+), 65 deletions(-) create mode 100644 cookies.data create mode 100644 src/main/java/com/ghost/temple/StorageManager.java create mode 100644 src/main/resources/.~lock.testData.xlsx# create mode 100644 storage.json create mode 100644 target/classes/.~lock.testData.xlsx# create mode 100644 target/classes/com/ghost/temple/StorageManager.class diff --git a/cookies.data b/cookies.data new file mode 100644 index 0000000000000000000000000000000000000000..535b736b9d604c967ce7fdc4fc8adde7f4ae12b6 GIT binary patch literal 404 zcmZvY%}T>S6oqg48$?TWrwiY}3ALgY5e2&_Sagw<-N?{h(`nO5nz>2SjUZCoxK+9l zd;quN4?c`9;5#@&6gO^$GxrSVo^PJMpr|5P9`J;-n1s(-T=iX~_vY;G{`F-m50HkF zMF6NC9O@Gwr{Avyk;j4%<(M;tK1vaf*ijG+1vb`iJ|0@1=Q*%Dupm^6C_I(^v;($K zE_UMx+feESBQ7MtO51F9d@eo5rASC`PrI0gBAOCZGuQNTnw(I31<6M^hD*q69dhdb zXj|#IE9loNCftv+OBwNU1g*9?_EW|Q3?Tv^9ww3-3i0M_H}wKY!( zD!P}i0XKS>h%ORX4ng50kXn{|sD>1T%GAjHv&YQK6B9R|y*=AHu6!YU!boK~B}3gM M0^9t-K1whB2Amam(EtDd literal 0 HcmV?d00001 diff --git a/src/main/java/com/ghost/temple/CookieManager.java b/src/main/java/com/ghost/temple/CookieManager.java index 20bf063..1552bf9 100644 --- a/src/main/java/com/ghost/temple/CookieManager.java +++ b/src/main/java/com/ghost/temple/CookieManager.java @@ -8,34 +8,33 @@ import java.util.Set; public class CookieManager { - private static final String COOKIE_FILE = "cookies.ser"; + private static final String COOKIE_FILE = "cookies.data"; - // Save cookies to file public static void saveCookies(WebDriver driver) { try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(COOKIE_FILE))) { - Set cookies = driver.manage().getCookies(); - oos.writeObject(cookies); - System.out.println("Cookies saved 🍪✅"); + oos.writeObject(driver.manage().getCookies()); + System.out.println("🍪 Cookies saved successfully!"); } catch (IOException e) { e.printStackTrace(); } } - // Load cookies from file @SuppressWarnings("unchecked") public static void loadCookies(WebDriver driver, String url) { - File cookieFile = new File(COOKIE_FILE); - if (!cookieFile.exists()) return; + File file = new File(COOKIE_FILE); + if (!file.exists()) { + System.out.println("⚠ No cookies file found, starting fresh."); + return; + } - driver.get(url); // Need to open site before setting cookies - - try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(COOKIE_FILE))) { + try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) { Set cookies = (Set) ois.readObject(); + driver.get(url); // must open the domain before adding cookies for (Cookie cookie : cookies) { driver.manage().addCookie(cookie); } - driver.navigate().refresh(); // Apply cookies - System.out.println("Cookies loaded 🍪🔁"); + driver.navigate().refresh(); + System.out.println("✅ Cookies loaded successfully!"); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } diff --git a/src/main/java/com/ghost/temple/ExcelUtils.java b/src/main/java/com/ghost/temple/ExcelUtils.java index 8c1e4a8..b0d9a08 100644 --- a/src/main/java/com/ghost/temple/ExcelUtils.java +++ b/src/main/java/com/ghost/temple/ExcelUtils.java @@ -48,4 +48,31 @@ public class ExcelUtils { } +public static List> fetchQuestDropdownOptions(String sheetName) { + List> allDropdownOptions = new ArrayList<>(); + + try (FileInputStream fis = new FileInputStream("src/main/resources/testData.xlsx"); + Workbook workbook = new XSSFWorkbook(fis)) { + + Sheet sheet = workbook.getSheet(sheetName); + + for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) { + Row row = sheet.getRow(rowIndex); + if (row == null) continue; + + List dropdownRow = new ArrayList<>(); + for (int colIndex = 2; colIndex <= 12; colIndex++) { // columns C to M (dropdowns) + Cell cell = row.getCell(colIndex); + dropdownRow.add(cell != null ? cell.toString().trim() : ""); + } + allDropdownOptions.add(dropdownRow); + } + + } catch (Exception e) { + e.printStackTrace(); + } + + return allDropdownOptions; +} + } \ No newline at end of file diff --git a/src/main/java/com/ghost/temple/ReusableFunctions.java b/src/main/java/com/ghost/temple/ReusableFunctions.java index 415d041..d549ddf 100644 --- a/src/main/java/com/ghost/temple/ReusableFunctions.java +++ b/src/main/java/com/ghost/temple/ReusableFunctions.java @@ -3,11 +3,13 @@ package com.ghost.temple; import java.util.Map; import org.openqa.selenium.*; import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import java.time.Duration; import java.util.ArrayList; import java.util.List; - +import java.awt.Robot; +import java.awt.event.KeyEvent; public class ReusableFunctions { @@ -28,28 +30,151 @@ public class ReusableFunctions { } } + private static String normalizeValue(String value) { + try { + double d = Double.parseDouble(value); + if (d == (long) d) { + return String.valueOf((long) d); // Remove .0 for integers + } + return String.valueOf(d); // Keep decimal for non-whole numbers + } catch (NumberFormatException e) { + return value; // Not a number, return as is + } +} - public static void fillQuestInputs(WebElement titleElement, WebElement descElement, List> questData) { +public static void fillQuestInputsAndRewards( + WebDriver driver, + WebElement titleElement, + WebElement descElement, + String createQuest, + List> questData, + List dropdownXPaths, + List> dropdownOptionsPerRow // each row has a list of dropdown/input values +) { if (questData == null || questData.isEmpty()) { - System.out.println("No quests found to fill, sweetheart 😢"); + System.out.println("No quests found to fill"); return; } - for (List row : questData) { - if (row.size() < 2) { - System.out.println("⚠️ Skipping incomplete row: " + row); + if (dropdownOptionsPerRow == null || dropdownOptionsPerRow.isEmpty()) { + System.out.println("No dropdown/input options found to fill"); + return; + } + + if (questData.size() != dropdownOptionsPerRow.size()) { + System.out.println("⚠️ Number of quest rows and dropdown/input value rows mismatch!"); + return; + } + + for (int rowIndex = 0; rowIndex < questData.size(); rowIndex++) { + List questRow = questData.get(rowIndex); + List dropdownOptions = dropdownOptionsPerRow.get(rowIndex); + + if (questRow.size() < 2) { + System.out.println("⚠️ Skipping incomplete quest row: " + questRow); continue; } - String title = row.get(0); // Column A - String description = row.get(1); // Column B + if (dropdownXPaths.size() != dropdownOptions.size()) { + System.out.println("⚠️ Dropdown/Input XPaths and values count mismatch on row " + (rowIndex + 1)); + continue; + } - System.out.println("📝 Filling → Title: \"" + title + "\", Description: \"" + description + "\""); + String title = normalizeValue(questRow.get(0)); + String description = normalizeValue(questRow.get(1)); + System.out.println("📝 Filling quest #" + (rowIndex + 1) + + " → Title: \"" + title + "\", Description: \"" + description + "\""); + + // Fill title and description fillInput(titleElement, title); fillInput(descElement, description); + + // Process each dropdown or input for this quest + for (int i = 0; i < dropdownXPaths.size(); i++) { + String xpath = dropdownXPaths.get(i); + String value = normalizeValue(dropdownOptions.get(i)); + + if (xpath.trim().endsWith("::input")) { + // For input fields + System.out.println("⌨️ Setting input (XPath: " + xpath + ") to value: " + value); + String status = clearAndSetInputByXPathJS(driver, xpath, value); + System.out.println("Status: " + status); + } + else if (xpath.trim().endsWith("::select")) { + // For dropdown selects + System.out.println("🎯 Selecting dropdown (XPath: " + xpath + ") with option: " + value); + String status = selectDropdownOptionByXpathJS(driver, xpath, value); + System.out.println("Status: " + status); + } + else { + System.out.println("⚠️ Unknown element type for XPath: " + xpath); + } + } + + sleepInSeconds(4); + //Click Create Quest Button + //scrollIntoView(driver, createQuest); + //checkWebElement(createQuest); + clickElementByXPathJS(driver, createQuest); + } + + +} + +/** + * Scroll down inside the active window by sending PAGE_DOWN key presses using Robot. + * + * @param times Number of times to press PAGE_DOWN key (each press scrolls a bit) + */ + public static void robotscrollDown(int times) { + try { + Robot robot = new Robot(); + for (int i = 0; i < times; i++) { + robot.keyPress(KeyEvent.VK_PAGE_DOWN); + robot.keyRelease(KeyEvent.VK_PAGE_DOWN); + Thread.sleep(200); // small pause between scrolls for smoothness + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + + /** + * Click an element by XPath using JavaScript executor. + * + * @param driver WebDriver instance + * @param xpath XPath of the element to click + * @return Status message from JS execution + */ +public static String clickElementByXPathJS(WebDriver driver, String xpath) { + String script = + "var el = document.evaluate(\"" + xpath + "\", document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;" + + "if(el) { el.click(); return \"Clicked element with XPath: " + xpath + "\"; }" + + "else { return \"Element not found for XPath: " + xpath + "\"; }"; + + Object result = ((JavascriptExecutor) driver).executeScript(script); + return result != null ? result.toString() : "No result from JS execution"; +} + + + + /** + * Pause execution for the given number of seconds. + * + * @param seconds Time to sleep in seconds + */ +public static void sleepInSeconds(long seconds) { + try { + Thread.sleep(seconds * 1000); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + System.err.println("⚠️ Sleep interrupted: " + e.getMessage()); } } + + // Assuming fillInput is something like this: public static void fillInput(WebElement element, String value) { @@ -60,10 +185,92 @@ public class ReusableFunctions { System.out.println("DEBUG: Tried to fill input with null value!"); } } + + public static void scrollIntoView(WebDriver driver, WebElement element) { ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); } + public static void scrollIntoViewByXpath(WebDriver driver, String xpath) { + WebElement element = driver.findElement(By.xpath(xpath)); + ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element); + } + + + /** + * Select dropdown option by visible text using JavaScript executor. + * + * @param driver Selenium WebDriver instance + * @param selectXpath XPath locator of the