Search
 
 

Display results as :
 


Rechercher Advanced Search

Navigation
 Portal
 Index
 Memberlist
 Profile
 FAQ
 Search

wait for control presence

View previous topic View next topic Go down

wait for control presence

Post by ramandas on Wed Sep 26, 2012 2:09 pm

i have a scenario :
i have to wait for a control enable in specific time or if control is enable within time then go to next step.

EX : suppose a textbox should enable in 1 min and if text box is enable in 30 sec tn dont wait for 1 min. i used Thread.sleep() for wait.

ramandas

Posts : 2
Join date : 2012-09-03

Back to top Go down

Re: wait for control presence

Post by popsha on Thu Sep 27, 2012 1:34 pm

Code:
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MINUTES);

Here implicitlyWait waits maximum of 1 minute. If the web element enabled/displayed within 1 minute, then it executes next line without waiting for remaining time.

popsha
Amateur
Amateur

Posts : 60
Join date : 2012-04-14

Back to top Go down

Re: wait for control presence

Post by rohit kumar gupta on Sun Oct 21, 2012 6:17 pm

Hi popsha,

I had a doubt in the above code where are we specifying the web element which has to be enabled.

rohit kumar gupta
Active particpant
Active particpant

Posts : 24
Join date : 2012-09-19
Age : 26
Location : abc

Back to top Go down

Re: wait for control presence

Post by anemuday on Mon Oct 22, 2012 1:29 pm

I wrote my own custom method. This may help:
str_xPath = xpath of the object
MaxTime= maximum time to wait, which you have to pass in "Seconds"

Code:
public static void WaitTillButtonEnabled(String str_xPath,int MaxTime)
{



 long StartTime,EndTime;
 StartTime=new Date().getTime();
 long MaxT=MaxTime*1000;

 while(!driver.findElement(By.xpath(str_xPath)).isEnabled())
 {
  try
  {
  Thread.sleep(1000);
  }
  catch (InterruptedException e)
  {
  e.printStackTrace();
  }
  EndTime=new Date().getTime();
  if(EndTime-StartTime>MaxT) //The difference is returned in milliseconds
  break;
 }

}

anemuday
Active particpant
Active particpant

Posts : 11
Join date : 2012-10-11

Back to top Go down

Re: wait for control presence

Post by popsha on Mon Oct 22, 2012 3:17 pm

@ Rohit

Web elements are the one, we are dealing with in Selenium Webdriver directly. Components which are in a web page are called web element.
So, to accomplish the functionality of each web element, we use implicitlywait.

@ Anemuday

We cannot use your way of approach for every action in Webdriver. It will lead to brittle the script since Thread.sleep should be used in often.
Also, you didn't define the value for MaxTime.

Where as "implicitlyWait" universal waitr comand.

popsha
Amateur
Amateur

Posts : 60
Join date : 2012-04-14

Back to top Go down

Re: wait for control presence

Post by anemuday on Mon Oct 22, 2012 6:26 pm

One question popsha,

With your line of code, where you are associating with the control? What i mean is, Say i have 1000 Web Elements in my page and my control is 999th, and once all other controls loads then this control will be loaded.

And in another scenario, the webelement will be enabled only after some time gap say 10 Scec. Ex. i have few controls in my page and say upon clicking on Button1 all controls will be loaded, but after 10 secs my Button2 will be enabled. How to validate this?

I am new to Selenium/Java, i dont know the downside of Thread.sleep.

And in my function MaxTime is a parameter, that you have to pass to the function

anemuday
Active particpant
Active particpant

Posts : 11
Join date : 2012-10-11

Back to top Go down

Re: wait for control presence

Post by popsha on Tue Oct 23, 2012 8:25 am

Hi Anemuday,

Your question is perfectly correct for which implicitlyWait is made for. I briefed it in my first post in this thread. Let me explain it again.

Let's take your scenario, in that 500 web elements are static and remaining are dynamic. Here, browser's load time for each web element differs on their functionality. So, we use implicit wait command for all the web elements at single stretch ie., in @Before annotation rather than using explicit wait concept for each 500 web elements.
Code:
 @Before
    public void setUp() throws Exception {
       
           
  //    File file = new File("D:/Automate/Setup/IEDriverServer_Win32_2.24.2/IEDriverServer.exe");
    //    System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
       
          driver = new InternetExplorerDriver();
       
          baseUrl = "http://xx.xx.xx.xx";
       
       
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
       
        driver.manage().window().maximize();
       
    }
In the above code, WebDriver wait for the web element to enable within 30 seconds. If the web element enabled less than 30 secs, let's say 10 secs, it will not wait for remaining 20 secs. It will move to next control.


For further more reference, have a look at below link
https://groups.google.com/forum/?fromgroups=#!topic/selenium-users/_Qy1m5U_XpQ

Yes, i didn't notice the parameters passed to the method.

popsha
Amateur
Amateur

Posts : 60
Join date : 2012-04-14

Back to top Go down

Re: wait for control presence

Post by rohit kumar gupta on Wed Oct 24, 2012 5:35 pm

Hi Popsha,

This is how implicit wait is defined in the documentation.

An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object instance.
Sample code given there:-WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://somedomain/url_that_delays_loading");
WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement"));
****My Understanding:-myDynamicElement statement will wait until it finds the element ****My Doubt:-What if i clik a button and a new field appears. Then when i use a find element on that will the implict statement provide the time even for this element or will I have to define a new implict time out statement.because according to documentation(Once set, the implicit wait is set for the life of the WebDriver object instance.)

rohit kumar gupta
Active particpant
Active particpant

Posts : 24
Join date : 2012-09-19
Age : 26
Location : abc

Back to top Go down

Re: wait for control presence

Post by popsha on Thu Oct 25, 2012 9:23 am

Yes Rohit, as you mentioned it briefly. implcitWait set for life. Thus, no need of explicit wait again.
Usage of implicitWait is explained in my previous post for your perusal.

popsha
Amateur
Amateur

Posts : 60
Join date : 2012-04-14

Back to top Go down

Re: wait for control presence

Post by Sponsored content Today at 7:28 am


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum