Hello everyone. Im not quite sure if this is a good practice to write keywords this way. I had some troubles to check a radio input element with the built in keyword. So i’ve written a custom keyword now to use a simple javascript click on that object in case the built in check keyword fails.
Im not that experienced with writing code in general and i have my doubts that my code is stable and it could potentially break.
Can someone tell me if this code is okay or am i missing some potential problems with using javascript click
/**
* Custom Check keyword: Try to check with standard keyword. If check fails due to not clickable exception: Retry with JavaScript click()
* @param to - Represents a TestObject
*/
@Keyword
def customCheck(TestObject to) {
WebElement webElement = WebUiCommonHelper.findWebElement(to, 0)
def isNotSelected = WebUI.verifyElementNotChecked(to,15)
//Try to click with current test object if not selected
try{
if (isNotSelected) {
WebUI.check(to)
} else {
println "Input element is already checked, therefore check action will not be executed"
}
} catch (Exception ex) {
if (isNotSelected) {
println("--- Standard check failed -> Catch block execution started: Check (click) via JavaScript ---")
WebDriver driver = DriverFactory.getWebDriver()
JavascriptExecutor executor = ((driver) as JavascriptExecutor)
executor.executeScript('arguments[0].click()', webElement)
}
def isSelected = WebUI.verifyElementChecked(to,15)
if (isSelected) {
KeywordUtil.markPassed("Element was selected (checked) successfully.")
} else {
KeywordUtil.markFailed("Checking element failed!")
}
}
}