Skip to content

Commit

Permalink
Merge pull request #9 from znsio/codeRefactor
Browse files Browse the repository at this point in the history
Refactoring basepage code
  • Loading branch information
anandbagmar committed Oct 12, 2023
2 parents f316036 + e5d0ca2 commit 2cb6497
Showing 1 changed file with 32 additions and 32 deletions.
64 changes: 32 additions & 32 deletions src/main/java/com/znsio/rpap/pages/BasePage.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,51 @@
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.HashMap;
import java.util.Map;

public class BasePage extends Page {

public BasePage(WebDriver driver, WebDriverWait wait) {
super(driver, wait);
}

//TODO: Simplify this method. Avoid the usage of Nested else-if
public void inputDataToElement(By by, String dataToInput) {

WebElement ele = driver.findElement(by);
String tagName = ele.getTagName();
scrollToView(by);
Map<String, Runnable> inputHandlers = new HashMap<>();
inputHandlers.put("select", () -> selectInputFromDropdown(ele, dataToInput));
inputHandlers.put("textarea", () -> enterTextInTextarea(ele, dataToInput));
inputHandlers.put("input", () -> enterTextInTextField(ele, dataToInput));
Runnable handler = inputHandlers.get(tagName.toLowerCase());
if (handler != null) {
handler.run();
} else {
throw new UnsupportedOperationException("Unsupported input type: " + tagName);
}
}

if (tagName.equals("select")) {

scrollToView(by);
Select dropdown = new Select(ele);
dropdown.selectByVisibleText(dataToInput);

} else if (tagName.equals("textarea")) {

scrollToView(by);
ele.clear();
ele.sendKeys(new CharSequence[]{dataToInput});
ele.sendKeys(new CharSequence[]{Keys.TAB});
private void selectInputFromDropdown(WebElement element, String dataToInput) {
Select dropdown = new Select(element);
dropdown.selectByVisibleText(dataToInput);
}

} else if (tagName.equals("input")) {
String inputType = ele.getAttribute("type");
scrollToView(by);
private void enterTextInTextarea(WebElement element, String dataToInput) {
element.clear();
element.sendKeys(dataToInput);
element.sendKeys(Keys.TAB);
}

if ((inputType.equals("text")) || (inputType.equals("email")) || (inputType.equals("password"))) {
ele.clear();
ele.sendKeys(new CharSequence[]{dataToInput});
ele.sendKeys(new CharSequence[]{Keys.TAB});
} else if (inputType.equals("checkbox")) {
if ((!ele.isSelected()) && (dataToInput.equals("1"))) {
ele.click();
private void enterTextInTextField(WebElement element, String dataToInput) {
String inputType = element.getAttribute("type");

} else if ((ele.isSelected()) && (dataToInput.equals("0"))) {
ele.click();
}
} else if (inputType.equals("radio")) {
if ((!ele.isSelected()) && (dataToInput.equals("1"))) {
ele.click();
}
}
if (inputType.equals("text") || inputType.equals("email") || inputType.equals("password")) {
element.clear();
element.sendKeys(dataToInput);
element.sendKeys(Keys.TAB);
} else {
throw new UnsupportedOperationException("Unsupported input type: " + inputType);
}
}

Expand Down

0 comments on commit 2cb6497

Please sign in to comment.