Forum Discussion

nish_b's avatar
nish_b
Contributor
4 years ago
Solved

Exiting out of a frame using JavaScript

Hi,

 

I'm working on web application for GUI automation testing. The application web-page has few iFrames within which there are web-elements defined. For better performance of element identification, I switch to the frame and then search for the web-element. After this is completed, I would like to switch back to the default content (which is not an iFrame) and proceed with element identification and rest of the execution.

 

How to proceed in this scenario? The Frame method needs an index or name - https://support.smartbear.com/testcomplete/docs/reference/test-objects/members/web-objects/frame-method-web-object.html

 

But I can't use this to switch back to default content as it is not an iFrame. I have tried the below approaches but no success:

 

1) Store the current page before navigating to iFrame in a variable. Then I passed this variable to a function - getMainTabUsingPage(page) which gets the Main tab web object present outside the iFrame (default content).
 
Code snippet:
var page = Sys.Browser().Page("*");
 
getMainTabUsingPage(page) {
let xpath = "//div[@class=\"topmenu\"]/table/tbody/tr/td[contains(text(),'MainTab')]";
let myTabObj = page.FindChildByXPath(xpath);
return myTabObj;
}
 

 

2) Refresh the page after switching to iFrame, to reload the page with entire HTML DOM of the webpage so that the MainTab web element can be found in the DOM.

 

Please suggest/help. Thanks!

  • Hi,

     

    Like Marsha wrote...

    TestComplete, unlike Selenium, does not switch to iframe and does not need to switch back. It just assesses elements as they are displayed in the Object Browser.

    E.g. (pseudocode):

    page.frame(1).button('subscribe').Click();

    Log.Message(page.Panel('subscriptionCounter').wText);

    page.frame(2).button('sendInvite').Click();

    ...

     

     

3 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    Is the default content still on screen when you are in the other frames?  You could just add a click on a tab or a field that you can still see in order to change the focus.

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Like Marsha wrote...

    TestComplete, unlike Selenium, does not switch to iframe and does not need to switch back. It just assesses elements as they are displayed in the Object Browser.

    E.g. (pseudocode):

    page.frame(1).button('subscribe').Click();

    Log.Message(page.Panel('subscriptionCounter').wText);

    page.frame(2).button('sendInvite').Click();

    ...

     

     

    • nish_b's avatar
      nish_b
      Contributor

      Hi Marsha_R  and AlexKaras ,

       

      Thanks for your responses. Sorry I will add some more background details for a better understanding.

       

      I earlier tried directly identifying the elements and performing the click option. It works fine but the performance was very bad as there were similar objects with huge data in the same page, so it was taking long time to find the element. So the workaround for this was that I find the element using the specific iFrame in which the element belongs. This has improved the performance as the scope of searching for element reduced.

       

      Marsha_R  Yes the 2nd element (MainTab) is still visible after I switch the frame.

       

      AlexKaras and Marsha_R - I am not sure if the control switches to the iFrame when I find the element or it just looks within the specific frame without switching the control. Below is the detailed code flow I used to find the element in the iFrame named "iFrame_sr". After below code flow is when I'm trying to click on second element which is outside of iFrame scope.

       

      //get the frame object in which element is present, using the name of the iFrame.

      let frame = tcHelper.GetiFrameObject(this.getWebPage(),"iFrame_sr"); // refer below section for function body

       

      //get the web element object by searching in the frame object received from the above code using the ID of the webelement

      let webObj = tcHelper.GetWebObjectInFrameWithID(frame,webEleId); // refer below  section for function body

       

      //Args: webPageObj is the page in which iFrame is present. iframeID is the ID of the iframe being captured
      GetiFrameObject(webPageObj,iframeID){
      let iframe = webPageObj.contentDocument.getElementById(iframeID);
      if(iframe==null)
      Log.Error("Unable to find the iframe object with ID: "+iframeID+" in the webpage");
      else
      return iframe;
      }

      GetWebObjectInFrameWithID(frameObj,webElementID){
      let webObj = frameObj.contentDocument.getElementById(webElementID);
      if(webObj==null)
      Log.Error("Unable to find the web object with ID: "+webElementID+" in the specified frame of the webpage");
      else
      return webObj;
      }