Usage of Custom keywords || Enhanced version of click element


#1

for clicking of a web element like button…I created a new keyword as below:

@Keyword
	def static clickElement(TestObject to, int waitTime = 3) {
		try {
			//WebUI.scrollToElement(to, waitTime, FailureHandling.STOP_ON_FAILURE)
			WebUI.waitForElementPresent(to, waitTime, FailureHandling.STOP_ON_FAILURE)
			WebUI.verifyElementClickable(to, FailureHandling.STOP_ON_FAILURE)
			WebElement element = WebUiBuiltInKeywords.findWebElement(to);
			KeywordUtil.logInfo("Clicking element")
			element.click()
			KeywordUtil.markPassed("Element has been clicked")
		} catch (WebElementNotFoundException e) {
			KeywordUtil.logInfo("WebElementNotFoundException thrown: "+e.message)
			KeywordUtil.markFailed("Element not found")
		} catch (Exception e) {
			KeywordUtil.logInfo("Exception thrown: "+e.message)
			KeywordUtil.markFailed("Fail to click on element.")
		}
	}

could anyone review and make any suggestions for this keywork to work more efficiently…

Thank you in advance


#2

Looks good, thanks for sharing!


#3

@Marek_Melocik @Russ_Thomas @kazurayam @Andrej_Podhajsky @Brandon_Hein

Hi all, I have been using the below custom keyword for clicking.

I believe the StaleElementReferenceException block needs enhancement. Could you please give your thoughts on this.

@Keyword
public static clickElement(TestObject to, int timeout) {
try {

  	//WebUI.scrollToElement(to, timeout, FailureHandling.OPTIONAL) // Might cause failure in few cases, so commenting.
  	WebUI.waitForElementPresent(to, timeout, FailureHandling.OPTIONAL)
  	WebUI.waitForElementVisible(to, timeout, FailureHandling.OPTIONAL)			

  	WebUI.waitForElementClickable(to, timeout, FailureHandling.OPTIONAL)

  	////  IMPORTANT: Only if : FailureHandling.STOP_ON_FAILURE; the catch block will execute if this step fails.
  	WebUI.click((to),FailureHandling.STOP_ON_FAILURE)
  	KeywordUtil.logInfo("Click on the element using built-in keyword")

  	sleep (250)
  	WebUI.waitForPageLoad(120, FailureHandling.OPTIONAL)
  	WebUI.waitForJQueryLoad(120, FailureHandling.OPTIONAL)
  	sleep (250)		

  }

  catch(StaleElementReferenceException){
  
  	KeywordUtil.logInfo(" StaleElementReferenceException : Retry clicking on the element in the refreshed DOM")
  	
  	
  	sleep(1500)
  	WebUI.click(to)
  	KeywordUtil.logInfo("Element is clicked by using stale element reference exception method")
  	sleep (250)
  	WebUI.waitForPageLoad(120, FailureHandling.OPTIONAL)
  	WebUI.waitForJQueryLoad(120, FailureHandling.OPTIONAL)
  	sleep (250)

  }

  catch (Exception e) {			


  	WebDriver driver = DriverFactory.getWebDriver()
  	WebElement element = WebUiCommonHelper.findWebElement(to, timeout)
  	JavascriptExecutor executor = ((driver) as JavascriptExecutor)
  	executor.executeScript('arguments[0].click()', element)
  	
  	KeywordUtil.logInfo('Click on the element using JavaScript snippet')
  	
  	System.err.println(e)
  	
  	sleep (250)
  	WebUI.waitForPageLoad(120, FailureHandling.OPTIONAL)
  	WebUI.waitForJQueryLoad(120, FailureHandling.OPTIONAL)
  	sleep (250)		
  } }

#4

You need e?


#5

i’m probably wrong (my knowledge of groovy is not on level i wish it will be)
so 2nd catch will be never executed, since 1st one, the way it is written now, will catch all exceptions and errors

class Example {
   static void main(String[] args) {
      try {
         def arr = new int[3];
         arr[5] = 5;
      } catch(all){
        println "Gotta catch'em all"   
      } catch(ArrayIndexOutOfBoundsException ex) {
         println(ex.toString());
         println(ex.getMessage());
         println(ex.getStackTrace());  
      } catch(Exception ex) {
         println("Catching the exception");
      }finally {
         println("The final block");
      }
		
      println("Let's move on after the exception");
   } 
}


it seems ok.