Fixed the Input. React app relies on Input state not the DOM

This commit is contained in:
arul 2025-08-11 22:04:27 +05:30
parent 7d13ea9adf
commit ae3e1ee04d
8 changed files with 80 additions and 13 deletions

View File

@ -99,13 +99,13 @@ public static void fillQuestInputsAndRewards(
if (xpath.trim().endsWith("::input")) { if (xpath.trim().endsWith("::input")) {
// For input fields // For input fields
System.out.println("⌨️ Setting input (XPath: " + xpath + ") to value: " + value); System.out.println("⌨️ Setting input (XPath: " + xpath + ") to value: " + value);
String status = clearAndSetInputByXPathJS(driver, xpath, value); String status = setReactElementValueByXpathJS(driver, xpath, value);
System.out.println("Status: " + status); System.out.println("Status: " + status);
} }
else if (xpath.trim().endsWith("::select")) { else if (xpath.trim().endsWith("::select")) {
// For dropdown selects // For dropdown selects
System.out.println("🎯 Selecting dropdown (XPath: " + xpath + ") with option: " + value); System.out.println("🎯 Selecting dropdown (XPath: " + xpath + ") with option: " + value);
String status = selectDropdownOptionByXpathJS(driver, xpath, value); String status = setReactElementValueByXpathJS(driver, xpath, value);
System.out.println("Status: " + status); System.out.println("Status: " + status);
} }
else { else {
@ -274,6 +274,73 @@ public static void sleepInSeconds(long seconds) {
return (String) jsExecutor.executeScript(jsCode, inputXpath, value); return (String) jsExecutor.executeScript(jsCode, inputXpath, value);
} }
/**
* Set value in a React-controlled <input> or <select> by XPath using JS native setter.
*
* @param driver Selenium WebDriver instance
* @param xpath XPath of the target element
* @param value Value or visible option text to set
* @return Status message from JavaScript execution
*/
public static String setReactElementValueByXpathJS(WebDriver driver, String xpath, String value) {
String jsCode = """
{
const el = document.evaluate(
arguments[0],
document,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
if (!el) {
return "Element not found";
}
if (el.tagName === 'INPUT') {
// Native setter for input
const nativeSetter = Object.getOwnPropertyDescriptor(
window.HTMLInputElement.prototype, 'value'
).set;
nativeSetter.call(el, arguments[1]);
// Trigger React updates
el.dispatchEvent(new Event('input', { bubbles: true }));
el.dispatchEvent(new Event('change', { bubbles: true }));
return `Input value set to '${arguments[1]}'`;
}
if (el.tagName === 'SELECT') {
// Find matching option by visible text
const option = Array.from(el.options).find(opt => opt.text === arguments[1]);
if (!option) {
return `Option '${arguments[1]}' not found`;
}
// Native setter for select
const nativeSetter = Object.getOwnPropertyDescriptor(
window.HTMLSelectElement.prototype, 'value'
).set;
nativeSetter.call(el, option.value);
// Trigger React updates
el.dispatchEvent(new Event('change', { bubbles: true }));
return `Select option '${arguments[1]}' selected`;
}
return "Unsupported element type: " + el.tagName;
}
""";
JavascriptExecutor jsExecutor = (JavascriptExecutor) driver;
return (String) jsExecutor.executeScript(jsCode, xpath, value);
}
// Wait for Element to be clickable. // Wait for Element to be clickable.
public static WebElement waitForElementToBeClickable(WebDriver driver, WebElement element, int timeoutInSeconds) { public static WebElement waitForElementToBeClickable(WebDriver driver, WebElement element, int timeoutInSeconds) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutInSeconds)); WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeoutInSeconds));
@ -317,4 +384,4 @@ public static void sleepInSeconds(long seconds) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -1 +1 @@
{"localStorage":{"soloLevelUpUserStats":"{\"name\":\"Arul\",\"level\":1,\"exp\":0,\"expToNextLevel\":100,\"job\":null,\"title\":null,\"hp\":100,\"maxHp\":100,\"mp\":10,\"maxMp\":10,\"fatigue\":0,\"gold\":0,\"stats\":{\"str\":10,\"agi\":10,\"per\":10,\"int\":10,\"vit\":10},\"statPoints\":0,\"equipment\":[],\"quests\":[{\"title\":\"Offgrid Hermit - Disconnect from Internet\",\"description\":\"ghostTemple system forces user by disabling internet by default in the computer to go offgrid. Equiped with self designed /etc/host based porn blocker. It still provides user option to enable the internet at boot. asking user to enter password. to make user realize that their decision is conscious.\",\"reward\":\"Experience only\",\"difficulty\":\"A\",\"priority\":\"High\",\"expiry\":\"Daily\",\"expReward\":30,\"statPointsReward\":1,\"goldReward\":0,\"statRewards\":{\"str\":0,\"agi\":0,\"per\":0,\"int\":0,\"vit\":0},\"itemRewards\":[],\"id\":\"d110453f-877b-46fe-8622-771d7c5fe812\",\"active\":true,\"completed\":false,\"progress\":0,\"isCustom\":true,\"createdAt\":1754723908926},{\"title\":\"The Refiner - Appearance Leveling\",\"description\":\"Focusing on Skin health, Hair health, preventing Hair loss, Preventing Fat gain. Focused on Weight Loss and over all Hygiene the way we look. To Attract people and be charismatic.\",\"reward\":\"Experience only\",\"difficulty\":\"C\",\"priority\":\"High\",\"expiry\":\"Daily\",\"expReward\":30,\"statPointsReward\":1,\"goldReward\":0,\"statRewards\":{\"str\":0,\"agi\":0,\"per\":0,\"int\":0,\"vit\":0},\"itemRewards\":[],\"id\":\"aa280321-49ef-42d5-bbea-efb1d0612793\",\"active\":true,\"completed\":false,\"progress\":0,\"isCustom\":true,\"createdAt\":1754723916208},{\"title\":\"King of Deception - Leveling up Art of Attraction\",\"description\":\"Focused on building a solid character. that is strong.. Talented, Attract people and seductive at same time. Manipulative in good way. Positive Manipulation by doing a good job on something we do and gaining trust among other people.\",\"reward\":\"Experience only\",\"difficulty\":\"B\",\"priority\":\"Medium\",\"expiry\":\"Daily\",\"expReward\":30,\"statPointsReward\":1,\"goldReward\":0,\"statRewards\":{\"str\":0,\"agi\":0,\"per\":0,\"int\":0,\"vit\":0},\"itemRewards\":[],\"id\":\"e9eb7081-5c47-4fac-a333-dce855df5f64\",\"active\":true,\"completed\":false,\"progress\":0,\"isCustom\":true,\"createdAt\":1754723924455},{\"title\":\"The Architect of Himself - Leveling up strength both physically and mentally\",\"description\":\"1. Exercise everyday Morning 1 hr (Cardio + strength) 2. Gain Knowledge on New concept or Technology, Read books. 3. Train Martial arts - Armed or Unarmed combat styles. Focus on Leveling up your Intellect, your Strength, Your Martial arts skill and your Aesthetics eventually.\",\"reward\":\"Experience only\",\"difficulty\":\"S\",\"priority\":\"High\",\"expiry\":\"Daily\",\"expReward\":30,\"statPointsReward\":1,\"goldReward\":0,\"statRewards\":{\"str\":0,\"agi\":0,\"per\":0,\"int\":0,\"vit\":0},\"itemRewards\":[],\"id\":\"318cdaa2-7aaa-4a87-a735-1a3b26fe3d37\",\"active\":true,\"completed\":false,\"progress\":0,\"isCustom\":true,\"createdAt\":1754723933472},{\"title\":\"The Phoenix - Rise from Ashes\",\"description\":\"Keep Moving Forward, No matter what. No matter how much pain you face. you can do take breaks to learn from failures. But never quit . Never Settle. Accept your Pain. see it as a part of life. both physical and Mental Pain. If you can cure it you can cure them. but if you can't never use them as an excuse to be Lower. Always improve from pain and be strong if possible. the Key idea is not to stop at any cost and keep moving forward. Not to embrace pain or romanticize it.\",\"reward\":\"Experience only\",\"difficulty\":\"S\",\"priority\":\"High\",\"expiry\":\"Daily\",\"expReward\":30,\"statPointsReward\":1,\"goldReward\":0,\"statRewards\":{\"str\":0,\"agi\":0,\"per\":0,\"int\":0,\"vit\":0},\"itemRewards\":[],\"id\":\"70e20d9e-ac40-42ba-9c2d-0a6a4ad3767b\",\"active\":true,\"completed\":false,\"progress\":0,\"isCustom\":true,\"createdAt\":1754723944347}],\"completedQuests\":[],\"inventory\":[{\"id\":\"item-health-potion\",\"name\":\"Health Potion\",\"type\":\"Consumable\",\"rarity\":\"Common\",\"description\":\"Restores 100 HP when consumed.\",\"quantity\":3},{\"id\":\"item-mana-potion\",\"name\":\"Mana Potion\",\"type\":\"Consumable\",\"rarity\":\"Common\",\"description\":\"Restores 50 MP when consumed.\",\"quantity\":2}]}"},"sessionStorage":{}} {"localStorage":{"soloLevelUpUserStats":"{\"name\":\"Arul\",\"level\":1,\"exp\":0,\"expToNextLevel\":100,\"job\":null,\"title\":null,\"hp\":100,\"maxHp\":100,\"mp\":10,\"maxMp\":10,\"fatigue\":0,\"gold\":0,\"stats\":{\"str\":10,\"agi\":10,\"per\":10,\"int\":10,\"vit\":10},\"statPoints\":0,\"equipment\":[],\"quests\":[],\"completedQuests\":[],\"inventory\":[{\"id\":\"item-health-potion\",\"name\":\"Health Potion\",\"type\":\"Consumable\",\"rarity\":\"Common\",\"description\":\"Restores 100 HP when consumed.\",\"quantity\":3},{\"id\":\"item-mana-potion\",\"name\":\"Mana Potion\",\"type\":\"Consumable\",\"rarity\":\"Common\",\"description\":\"Restores 50 MP when consumed.\",\"quantity\":2}]}"},"sessionStorage":{}}

File diff suppressed because one or more lines are too long

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<testsuite tests="1" failures="0" name="com.ghost.temple.AppTest" time="0.001" errors="0" skipped="0"> <testsuite tests="1" failures="0" name="com.ghost.temple.AppTest" time="0.005" errors="0" skipped="0">
<properties> <properties>
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/> <property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
<property name="java.vm.version" value="17.0.15+6-Debian-1deb12u1"/> <property name="java.vm.version" value="17.0.15+6-Debian-1deb12u1"/>
@ -24,7 +24,7 @@
<property name="os.name" value="Linux"/> <property name="os.name" value="Linux"/>
<property name="classworlds.conf" value="/usr/share/maven/bin/m2.conf"/> <property name="classworlds.conf" value="/usr/share/maven/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/> <property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/tmp/.mount_eDEX-Ui96UNI/usr/lib::/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib"/> <property name="java.library.path" value="/tmp/.mount_eDEX-UzEzEGG/usr/lib::/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib"/>
<property name="maven.conf" value="/usr/share/maven/conf"/> <property name="maven.conf" value="/usr/share/maven/conf"/>
<property name="jdk.debug" value="release"/> <property name="jdk.debug" value="release"/>
<property name="java.class.version" value="61.0"/> <property name="java.class.version" value="61.0"/>
@ -58,5 +58,5 @@
<property name="sun.cpu.endian" value="little"/> <property name="sun.cpu.endian" value="little"/>
<property name="sun.stdout.encoding" value="UTF-8"/> <property name="sun.stdout.encoding" value="UTF-8"/>
</properties> </properties>
<testcase classname="com.ghost.temple.AppTest" name="basicTest" time="0.001"/> <testcase classname="com.ghost.temple.AppTest" name="basicTest" time="0.005"/>
</testsuite> </testsuite>

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<testsuite tests="1" failures="0" name="com.ghost.temple.TestRunner" time="67.44" errors="0" skipped="0"> <testsuite tests="1" failures="0" name="com.ghost.temple.TestRunner" time="71.286" errors="0" skipped="0">
<properties> <properties>
<property name="java.runtime.name" value="OpenJDK Runtime Environment"/> <property name="java.runtime.name" value="OpenJDK Runtime Environment"/>
<property name="java.vm.version" value="17.0.15+6-Debian-1deb12u1"/> <property name="java.vm.version" value="17.0.15+6-Debian-1deb12u1"/>
@ -24,7 +24,7 @@
<property name="os.name" value="Linux"/> <property name="os.name" value="Linux"/>
<property name="classworlds.conf" value="/usr/share/maven/bin/m2.conf"/> <property name="classworlds.conf" value="/usr/share/maven/bin/m2.conf"/>
<property name="sun.jnu.encoding" value="UTF-8"/> <property name="sun.jnu.encoding" value="UTF-8"/>
<property name="java.library.path" value="/tmp/.mount_eDEX-Ui96UNI/usr/lib::/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib"/> <property name="java.library.path" value="/tmp/.mount_eDEX-UzEzEGG/usr/lib::/usr/java/packages/lib:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib"/>
<property name="maven.conf" value="/usr/share/maven/conf"/> <property name="maven.conf" value="/usr/share/maven/conf"/>
<property name="jdk.debug" value="release"/> <property name="jdk.debug" value="release"/>
<property name="java.class.version" value="61.0"/> <property name="java.class.version" value="61.0"/>
@ -58,5 +58,5 @@
<property name="sun.cpu.endian" value="little"/> <property name="sun.cpu.endian" value="little"/>
<property name="sun.stdout.encoding" value="UTF-8"/> <property name="sun.stdout.encoding" value="UTF-8"/>
</properties> </properties>
<testcase classname="Solo Leveling Quest Management" name="Open Solo Leveling page and click Add Quest" time="67.44"/> <testcase classname="Solo Leveling Quest Management" name="Open Solo Leveling page and click Add Quest" time="71.286"/>
</testsuite> </testsuite>

View File

@ -1,4 +1,4 @@
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Test set: com.ghost.temple.AppTest Test set: com.ghost.temple.AppTest
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.003 sec Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.012 sec

View File

@ -1,4 +1,4 @@
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Test set: com.ghost.temple.TestRunner Test set: com.ghost.temple.TestRunner
------------------------------------------------------------------------------- -------------------------------------------------------------------------------
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 68.072 sec Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 71.941 sec