diff --git a/src/main/java/com/ghost/temple/ExcelUtils.java b/src/main/java/com/ghost/temple/ExcelUtils.java index b006b2c..8c1e4a8 100644 --- a/src/main/java/com/ghost/temple/ExcelUtils.java +++ b/src/main/java/com/ghost/temple/ExcelUtils.java @@ -2,26 +2,50 @@ package com.ghost.temple; import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import java.io.File; + import java.io.FileInputStream; -import java.util.HashMap; -import java.util.Map; +import java.util.*; public class ExcelUtils { - public static Map fetchData(String sheetName) { - Map data = new HashMap<>(); - try (FileInputStream fis = new FileInputStream("src/main/resources/testData.xlsx"); - Workbook workbook = new XSSFWorkbook(fis)) { - Sheet sheet = workbook.getSheet(sheetName); - Row header = sheet.getRow(0); - Row values = sheet.getRow(1); - for (int i = 0; i < header.getPhysicalNumberOfCells(); i++) { - data.put(header.getCell(i).getStringCellValue(), values.getCell(i).getStringCellValue()); + private static final boolean DEBUG = true; // your debug flag + + public static List> fetchQuestDataByColumns(String sheetName) { + + List> questData = 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++) { // start from 0! + Row row = sheet.getRow(rowIndex); + if (row == null) continue; + + List columns = new ArrayList<>(); + for (int colIndex = 0; colIndex < 2; colIndex++) { // Only Column A and B + Cell cell = row.getCell(colIndex); + columns.add(cell != null ? cell.toString().trim() : ""); } - } catch (Exception e) { - e.printStackTrace(); + + if (DEBUG) { + System.out.println("📄 Row " + rowIndex + ": " + columns); + } + + questData.add(columns); } - return data; + + } catch (Exception e) { + e.printStackTrace(); } + + if (DEBUG) { + System.out.println("📦 Total quests fetched: " + questData.size()); + } + + return questData; +} + + } \ 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 a202bde..415d041 100644 --- a/src/main/java/com/ghost/temple/ReusableFunctions.java +++ b/src/main/java/com/ghost/temple/ReusableFunctions.java @@ -1,9 +1,19 @@ 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.WebDriverWait; +import java.time.Duration; +import java.util.ArrayList; +import java.util.List; + + public class ReusableFunctions { + private static final boolean DEBUG = true; // debug flag + public static boolean checkWebElement(WebElement element) { try { return element.isDisplayed() && element.isEnabled(); @@ -18,18 +28,55 @@ public class ReusableFunctions { } } - public static void fillInput(WebElement element, String value) { - if (checkWebElement(element)) { - element.clear(); - element.sendKeys(value); - } + + public static void fillQuestInputs(WebElement titleElement, WebElement descElement, List> questData) { + if (questData == null || questData.isEmpty()) { + System.out.println("No quests found to fill, sweetheart 😢"); + return; } + for (List row : questData) { + if (row.size() < 2) { + System.out.println("⚠️ Skipping incomplete row: " + row); + continue; + } + + String title = row.get(0); // Column A + String description = row.get(1); // Column B + + System.out.println("📝 Filling → Title: \"" + title + "\", Description: \"" + description + "\""); + + fillInput(titleElement, title); + fillInput(descElement, description); + } +} + + // Assuming fillInput is something like this: + public static void fillInput(WebElement element, String value) { + if (value != null) { + element.clear(); + element.sendKeys(value); + } else if (DEBUG) { + 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); } - // 🌐 NEW: Open a specific URL + // Wait for Element to be clickable. + public static WebElement waitForElementToBeClickable(WebDriver driver, WebElement element, int timeoutInSeconds) { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutInSeconds)); + return wait.until(ExpectedConditions.elementToBeClickable(element)); + } + + // Wait for Element Visibility + public static WebElement waitForElementToBeVisible(WebDriver driver, WebElement element, int timeoutInSeconds) { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutInSeconds)); + return wait.until(ExpectedConditions.visibilityOf(element)); + } + + // 🌐 Open a specific URL public static void openUrl(WebDriver driver, String url) { try { driver.get(url); diff --git a/src/main/java/com/ghost/temple/hooks/Hooks.java b/src/main/java/com/ghost/temple/hooks/Hooks.java index c92f91d..0e931c1 100644 --- a/src/main/java/com/ghost/temple/hooks/Hooks.java +++ b/src/main/java/com/ghost/temple/hooks/Hooks.java @@ -20,6 +20,6 @@ public class Hooks { e.printStackTrace(); } - DriverManager.quitDriver(); // Then quit driver + //DriverManager.quitDriver(); // Then quit driver } } diff --git a/src/main/resources/testData.xlsx b/src/main/resources/testData.xlsx index 1e67f50..a68ba14 100644 Binary files a/src/main/resources/testData.xlsx and b/src/main/resources/testData.xlsx differ diff --git a/src/test/java/com/ghost/temple/pages/QuestCreator.java b/src/test/java/com/ghost/temple/pages/HomePage.java similarity index 92% rename from src/test/java/com/ghost/temple/pages/QuestCreator.java rename to src/test/java/com/ghost/temple/pages/HomePage.java index d95ec3e..6898231 100644 --- a/src/test/java/com/ghost/temple/pages/QuestCreator.java +++ b/src/test/java/com/ghost/temple/pages/HomePage.java @@ -11,12 +11,12 @@ import com.ghost.temple.ReusableFunctions; import java.time.Duration; -public class QuestCreator { +public class HomePage { WebDriver driver; private By addQuestBtnLocator = By.xpath("//a[@href='/quests']/button[contains(text(),'View All')]"); - public QuestCreator(WebDriver driver) { + public HomePage(WebDriver driver) { this.driver = driver; } diff --git a/src/test/java/com/ghost/temple/pages/Quests.java b/src/test/java/com/ghost/temple/pages/Quests.java new file mode 100644 index 0000000..e36dee4 --- /dev/null +++ b/src/test/java/com/ghost/temple/pages/Quests.java @@ -0,0 +1,56 @@ +package com.ghost.temple.pages; + +import org.openqa.selenium.By; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.WebElement; +import org.openqa.selenium.support.FindBy; +import org.openqa.selenium.support.PageFactory; +import org.openqa.selenium.support.ui.ExpectedConditions; +import org.openqa.selenium.support.ui.WebDriverWait; + +import com.ghost.temple.ReusableFunctions; + +import com.ghost.temple.ExcelUtils; +import java.time.Duration; + +public class Quests { + + WebDriver driver; + + @FindBy(xpath = "//div[@class='mb-6 flex justify-center']/button[contains(text(),'Create Custom Quest')]") + WebElement createCustomQuest; + + @FindBy(xpath = "//input[@id='title']") + WebElement questTitle; + + @FindBy(xpath = "//textarea[@id='description']") + WebElement questDescription; + + public Quests(WebDriver driver) { + this.driver = driver; + PageFactory.initElements(driver, this); + } + + public void clickcreateCustomQuest() { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + + // Wait for the element to be clickable + ReusableFunctions.waitForElementToBeClickable(driver, createCustomQuest, 5); + + // Scroll into view using your reusable method + ReusableFunctions.scrollIntoView(driver, createCustomQuest); + + // Use your reusable click + ReusableFunctions.click(createCustomQuest); + } + + + public void createQuests() { + WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10)); + + // Wait for the element to be clickable + ReusableFunctions.waitForElementToBeVisible(driver, questTitle, 5); + ReusableFunctions.fillQuestInputs(questTitle, questDescription, ExcelUtils.fetchQuestDataByColumns("Quests")); + } + +} diff --git a/src/test/java/com/ghost/temple/stepdefs/QuestCreatorStepDefs.java b/src/test/java/com/ghost/temple/stepdefs/HomePageStepDefs.java similarity index 71% rename from src/test/java/com/ghost/temple/stepdefs/QuestCreatorStepDefs.java rename to src/test/java/com/ghost/temple/stepdefs/HomePageStepDefs.java index 9f60940..a5b574b 100644 --- a/src/test/java/com/ghost/temple/stepdefs/QuestCreatorStepDefs.java +++ b/src/test/java/com/ghost/temple/stepdefs/HomePageStepDefs.java @@ -1,20 +1,20 @@ package com.ghost.temple.stepdefs; import com.ghost.temple.driver.DriverManager; -import com.ghost.temple.pages.QuestCreator; +import com.ghost.temple.pages.HomePage; import com.ghost.temple.ReusableFunctions; import io.cucumber.java.en.Given; import io.cucumber.java.en.When; import org.openqa.selenium.WebDriver; -public class QuestCreatorStepDefs { +public class HomePageStepDefs { WebDriver driver; - QuestCreator questPage; - - public QuestCreatorStepDefs() { + HomePage homePage; + + public HomePageStepDefs() { this.driver = DriverManager.getDriver(); - this.questPage = new QuestCreator(driver); + this.homePage = new HomePage(driver); } @Given("I open the solo level page") @@ -24,6 +24,6 @@ public class QuestCreatorStepDefs { @When("I click the Add Quest button") public void i_click_the_add_quest_button() { - questPage.clickAddQuest(); + homePage.clickAddQuest(); } } diff --git a/src/test/java/com/ghost/temple/stepdefs/QuestsStepDefs.java b/src/test/java/com/ghost/temple/stepdefs/QuestsStepDefs.java new file mode 100644 index 0000000..c2ab575 --- /dev/null +++ b/src/test/java/com/ghost/temple/stepdefs/QuestsStepDefs.java @@ -0,0 +1,28 @@ +package com.ghost.temple.stepdefs; + +import org.openqa.selenium.WebDriver; +import com.ghost.temple.driver.DriverManager; +import com.ghost.temple.pages.Quests; + +import io.cucumber.java.en.When; + +public class QuestsStepDefs { + + + WebDriver driver; + Quests questPage; + + public QuestsStepDefs() { + this.driver = DriverManager.getDriver(); + this.questPage = new Quests(driver); + } + + @When("I fill all the quests from Excel") + public void i_fill_all_the_quests_from_excel() { + // Create Quests + questPage.clickcreateCustomQuest(); + questPage.createQuests(); + throw new io.cucumber.java.PendingException(); + } + +} diff --git a/src/test/resources/features/questCreator.feature b/src/test/resources/features/questCreator.feature index 6bf5f4e..f6c8ba3 100644 --- a/src/test/resources/features/questCreator.feature +++ b/src/test/resources/features/questCreator.feature @@ -3,3 +3,4 @@ Feature: Solo Leveling Quest Management Scenario: Open Solo Leveling page and click Add Quest Given I open the solo level page When I click the Add Quest button + When I fill all the quests from Excel diff --git a/target/classes/com/ghost/temple/CookieManager.class b/target/classes/com/ghost/temple/CookieManager.class index 944f3b5..36d4703 100644 Binary files a/target/classes/com/ghost/temple/CookieManager.class and b/target/classes/com/ghost/temple/CookieManager.class differ diff --git a/target/classes/com/ghost/temple/ExcelUtils.class b/target/classes/com/ghost/temple/ExcelUtils.class index cd81ef2..bc414d1 100644 Binary files a/target/classes/com/ghost/temple/ExcelUtils.class and b/target/classes/com/ghost/temple/ExcelUtils.class differ diff --git a/target/classes/com/ghost/temple/ReusableFunctions.class b/target/classes/com/ghost/temple/ReusableFunctions.class index 3369b22..d6b0c90 100644 Binary files a/target/classes/com/ghost/temple/ReusableFunctions.class and b/target/classes/com/ghost/temple/ReusableFunctions.class differ diff --git a/target/classes/com/ghost/temple/hooks/Hooks.class b/target/classes/com/ghost/temple/hooks/Hooks.class index 8c53306..221d656 100644 Binary files a/target/classes/com/ghost/temple/hooks/Hooks.class and b/target/classes/com/ghost/temple/hooks/Hooks.class differ diff --git a/target/classes/testData.xlsx b/target/classes/testData.xlsx index 1e67f50..a68ba14 100644 Binary files a/target/classes/testData.xlsx and b/target/classes/testData.xlsx differ diff --git a/target/cucumber-report.html b/target/cucumber-report.html index f3eb51f..c4bd36a 100644 --- a/target/cucumber-report.html +++ b/target/cucumber-report.html @@ -40,7 +40,7 @@ body{padding:0;margin:0}.html-formatter{max-width:1600px;min-height:100vh;margin