Thursday 22 March 2018

How Handle Wait Without Using Explicit and Implicit Wait in Selenium

Suppose you have one element on page and every time it will take different time to load on page. some time 1 sec, some time 5 sec and some time 8 sec. I want to click on that element.
Maximum time you have to wait for that element is 10 sec. If element do not appear after 10 sec than throw exception otherwise click on that element.

We need to use Thread.Sleep() function for this scenarios.

I have created below function for handle this scenario.

    public boolean mWaitAndClick(WebDriver driver, WebElement vElement) throws Exception
    {
     boolean bWaitAndClick = true;
        int MAXIMUM_WAIT_TIME = 10;  // this is seconds               
        int counter;
        
        try
        {
   vElement.click();
 }
        catch (Exception E) 
        {         
   for(counter=1; counter<=MAXIMUM_WAIT_TIME; counter++)
   {
    try
          {
     vElement.click();
     
     bWaitAndClick = true;     
     
     break;
          }
          catch(Exception e)
          {
           Thread.sleep(1000);
          }
   }
   
   if(counter>10)
   {
    bWaitAndClick = false;    
    throw new Exception("Element not found");    
   }
   
 }
        
        return bWaitAndClick;        
    }





No comments:

Post a Comment

Popular