Forum Discussion

TestingHobo's avatar
TestingHobo
Contributor
10 years ago

Wait for Xpath

Hi could someone confirm if this is possible. Coming from a Selenium background all my scripts use xpaths, however i have to hardcode waits in to most of my steps to account for PC performance, network, server etc so they dont fall over.



Example:

aqUtils.Delay(10000);


Page.FindChildByXPath("//*[@id='dvActionbtnArea']//a[node()[contains(., 'Add')]]").Click();


aqUtils.Delay(10000);



Is there anyway to Testcomplete to wait for the xpath but as soon as its visible click as it could take 1 second to load it could take 10 seconds

3 Replies

  • dfarr's avatar
    dfarr
    Occasional Contributor
    I belive you'll have to write your own timing loop. Is there a reason you couldn't do something roughly like this:



    var refreshMillisec = 500;

    var maxWait = 10000;

    var myControl; 



    do {

         Delay(refreshMilliSec);

         elapsedTime += refreshMilliSec;  

         myControl = Page.FindChildByXPath("//*[@id='dvActionbtnArea']//a[node()[contains(., 'Add')]]");

       } while (!myControl.Exists && elapsedTime <= maxWait);



    if ( myControl.Exists && myControl.Enabled ) myControl.Click();





     --Or this nice timing mechanism using aqDate I copied some time ago from others:

     

    var now = aqDateTime.Now();

    var giveUp = aqDateTime.AddSeconds(now,(maxWait/1000));



    do {

         Delay(1000);

         myControl = Page.FindChildByXPath("//*[@id='dvActionbtnArea']//a[node()[contains(., 'Add')]]");

         now = aqDateTime.Now();

       } while ( !myControl.Exists && now <= giveUp);



    The timing variables could be parameterized and the logic broken into a general function.
  • ArtemS's avatar
    ArtemS
    SmartBear Alumni (Retired)
    Hello, Richard

    Try using the Page.Wait() method:



    Page.Wait(10000);

    Page.FindChildByXPath("//*[@id='dvActionbtnArea']//a[node()[contains(., 'Add')]]").Click();

    Page.Wait(10000);





    The Page.Wait pauses script till the page is loaded or till the timeout elapses (whichever occurs first). The drawback of this method is that it may be ineffective for pages with dynamic content.  In this case try waiting for the element as Donald suggested or as described in Waiting For Web Pages.