Forum Discussion

duncanbrown's avatar
duncanbrown
Occasional Contributor
15 years ago

Variables referring to dynamic web page elements

I'm working on some tests for an ASP.NET website and have come across something I'd be grateful to have some assistance understanding.



On a particular page on the site is a datepicker control (jQuery ui I think), the header of which displays the selected month and year. To get the control's current month and year I set a variable to refer to that header and get its innerText property. The first time I do this, it works fine, but if I then click one of the control's navigation buttons and retrieve the innerText again, I get the original value, not the text that is currently being displayed on screen. Here's an example:




function getDate()

{

  var header = Sys.Process("iexplore").Page(urlhere).Panel(1).Panel(0).Form(0).Panel(2).Table(0).Cell(3, 1).Panel(1).Panel(0).Panel(0).Panel(0);

  ShowMessage(header.innerText); //September 2011 is shown, as expected

  clickNextMonth(); //Screen now shows October 2011

  ShowMessage(header.innerText); //September 2011 is shown, still

}


I get the same behavior if I set the header variable to refer to the page element via NameMapping or an Alias, even if I call the variable's RefreshMappingInfo() method before getting the innerText the second time, and adding delays doesn't help.



I have found a few ways to get the correct date to be shown the second time:


  • Don't use a variable at all; refer to the element by the full name (Sys.Process("iexplore").Page....) each time; or

  • Call the Page's Refresh() method after changing the date; or

  • Insert this after changing the date: header = eval(header.FullName);


If necessary, I will use one of these workarounds, but I'd be interested to know of any better ones. Moreover, it would be helpful to know what is causing this issue in the first place, and it what circumstances one needs to watch out for it.



Thanks in advance for any help.



Duncan

5 Replies

  • duncanbrown's avatar
    duncanbrown
    Occasional Contributor
    A clear example of this problem can be seen by going to http://jqueryui.com/demos/datepicker/ in IE8, clicking in the 'Date' box to bring up the datepicker, then running the following script:





    function test()


    {


      var monthNode =  Sys.Process("iexplore").Page("http://jqueryui.com/demos/datepicker/").Panel("ui_datepicker_div").Panel(0).Panel(0).TextNode(0);


      var prevLink = Sys.Process("iexplore").Page("http://jqueryui.com/demos/datepicker/").Panel("ui_datepicker_div").Panel(0).Link(0).TextNode(0);


      for (var i = 0; i < 3; i++)


      {


        ShowMessage(monthNode.innerText);


        prevLink.FireEvent("onclick");


      }


    }


    If you have the same issue I do, the month displayed on-screen will change but the dialogue will always show the same month.

    (using FireEvent() instead of TC's Click() method is necessary because, after the first click, TC claims that the object has no dimensions; I guess that is maybe related to the main issue).
  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Duncan,


    Try using the following code snippet:



    function test()

    {

      var datepicker = Sys.Process("iexplore", 2).Page("http://jqueryui.com/demos/datepicker/").Panel("ui_datepicker_div");

      var prevLink = datepicker.Panel(0).Link(0).TextNode(0);

      for (var i = 0; i < 3; i++)

      {

        // I recommend that you obtain this object each time in the loop

        // It seems to me that this object is dynamic

        ShowMessage(datepicker.Panel(0).Panel(0).TextNode(0).innerText);

        prevLink.FireEvent("onclick");

      }

    }



    Does this help?


    using FireEvent() instead of TC's Click() method is necessary because, after the first click, TC claims that the object has no dimensions; I guess that is maybe related to the main issue.


    I failed to reproduce this behavior. The Click method works fine in my script.

  • duncanbrown's avatar
    duncanbrown
    Occasional Contributor
    Irina,



    Thanks for the reply: your solution does work for me (though I do still need to use FireEvent(), oddly).



    Out of interest, do you know when this kind of workaround is necessary, in general? In trying to understand it myself I've made a couple of test pages with more simple dynamic elements, and -  when they're referred to straightforwardly by variables or name-mapping or aliases or whatever -  TC seems always to return the correct properties for them, even after they've changed; what is it that makes this case different, do you think?



    Duncan
  • irina_lukina's avatar
    irina_lukina
    Super Contributor

    Hi Duncan,


    I cannot say for sure. Generally, TestComplete works fine with dynamic controls, it can recognize and update their properties and methods (as you have mentioned). In fact, it's the first time I've faced such behavior.


    I have only one assumption: it may be caused by the internal architecture of the tested Datapicker control. As we have no access to its source code, it is difficult to say whether the name of the month is dynamically changed or there is another approach used for this procedure.

  • duncanbrown's avatar
    duncanbrown
    Occasional Contributor
    Irina,



    Okay, thanks for your help: I do appreciate it.



    Duncan