Forum Discussion

vex's avatar
vex
Contributor
14 years ago

How can I get TC to get a destination property of a link handled by javascript?

We have a link on a webpage that will dynamically change between Mac and Windows, depending on what is detected.



The HTML code for this link is here:





  <div id="download_but">

    <div class="button_large_download" onclick="download_clicked('windows', false, '');"><a href='#'>Download for Windows</a><span></span></div>
</div>





And for download_clicked, it shows:





if (platform == 'mac') { window.location = 'https://192.168.0.1/download/mac.pkg'; }
if (platform == 'linux') { window.location = 'http://192.168.0.1/products/latest.bin'; }
if (platform == 'windows') { window.location = 'https://192.168.0.1/download/win.exe'; }





In TC I'm unable to pull this location out of this button.  I'm wondering if there is a way to?   In other words - I want a way to find out where clicking this button is ultimately going to take me, which in the windows case should be https://192.168.0.1/download/win.exe



Thanks :)

1 Reply

  • Hi Vince,


    Once the destination of a link is set during the on-page routine execution, there's no way to read it, except for running this or a similar on-page routine. TestComplete can call routines from web pages, but it does not have access to the internals of these routines.




    You may insert a helper function into the web page's HTML that would calculate the target location of the download link in the same way as the download_clicked function does, and return the destination as a result. 





    function getlocation(){


    ...


    if (platform == 'mac') { return 'https://192.168.0.1/download/mac.pkg'; }


    if (platform == 'linux') { return 'http://192.168.0.1/products/latest.bin'; }


    if (platform == 'windows') { return 'https://192.168.0.1/download/win.exe'; }




    Then, during the test, you can call this web page function from TestComplete (as described in the Calling Scripts Located on a Web Page From TestComplete Scripts topic) and store its result:





    ...


    LinkTarget  =  page.Application.Document.Script.getlocation();


    ...









    A workaround is to analyze the content of the parent page. In the HTML code you provided, the link text says "Download for Windows", hence, the page has some sort of an on-load event that checks the current platform and modifies the link caption. Thus, you can analyze this caption and calculate the link destination on TestComplete's side:





    ...


    LinkCaption = page.Panel("download_but").Panel(0).Link(0).innerText;


    switch (LinkCaption)


      {


        case "Download for Windows":


        {


          LinkTarget = "https://192.168.0.1/download/win.exe";


          break;


        }


        case "Download for Macintosh":


        {


          LinkTarget = "https://192.168.0.1/download/mac.pkg";


          break;


        }


        case "Download for Linux":


        {


          LinkTarget = "http://192.168.0.1/products/latest.bin";


          break;


        }


     }


    ...









    Regards.