MS Dynamics CRM – Automation Testing: Handling Iframes

Iframes are quite tricky to play with sometimes. You can get into an iframe if you are unable to identify the element even if you are hitting it with the right identifier through selenium c#. Iframe is an embedded document within HTML which is holding some elements in order to improve the performance. If we have a web application, then just by inspecting through any browser and searching for the tag <iframe> we can get an idea whether iframes are present in that web application or not. So, to detect the element, we must first switch to the containing iframe and then look for the desired element. After the element is found and designated action is performed, we can switch back to the parent/default container (document). 

Learn more about our Microsoft Dynamics 365 services

Explore More

 

Selenium C#- there are several methods to handle iframes with IWebDriver.SwitchTo() method. Following contains the details for those; 

  • Driver.SwitchTo().DefaultContent() :- This will select the main document when a page contains multiple iframes, or the first frame that is present. 
  • Driver.SwitchTo().Frame(int frameIndex) :- This will select the frame based on index starting from 0. 
  • Driver.SwitchTo().Frame(string frameID) :- This will select the iframe based on its ID or Name. 
  • Driver.SwitchTo().Frame(IWebElement frameElement) :- This will select an iframe using its previously located OpenQA. Selenium.IWebelement 
  • Driver.SwitchTo().ParentFrame() :- This will select the Parent of current selected iframe. 

Switch to Iframe by Index/ ID: 

There could be 2 cases within this:  

  1. We know the exact index of iframe on which we want to switch 
  2. We don’t know the exact index of iframe on which we want to switch. 

In first case, when we simply know the exact index of iframe on which we want to switch then we can pass that to the “Driver.SwitchTo().Frame(index of iframe)”. Remember Index for iframe always starts with 0. 

In second case, when we don’t know how many iframes are involved in the web application, then we can get total number of iframes and then switch to the desired one as per our requirement.

List frames = new List(Browser.Driver.FindElements(By.TagName("iframe")));
int totalFrames = frames.Count;
Dictionary<int, string> frameNamesIDs = new Dictionary<int, string>();
for (int i = 0; i < frames.Count; i++)
{
frameNamesIDs.Add(i, frames[i].GetAttribute("id").ToString());
}

If we traverse frameNamesIDsit contains the frameID with respect to the FrameID. We can switch to desired frame either through index or through ID as “Driver.SwitchTo().Frame(index/ ID of iframe)” 

Switch to Iframe by IWebElement: 

Here is another method through which we can switch to Iframe 

IWebElement iframe = Browser.Driver.FindElement(By.Id("iframe1"));
Browser.Driver.SwitchTo().Frame(iframe);

Switch to Iframe by getting the Parent of current Iframe: 

Whenever we need to get the elements in the default content or from the Parent Iframe of the current iframe on which we are accessing the elements, then we can use the following commands;

Browser.Driver.SwitchTo().DefaultContent();
Browser.Driver.SwitchTo().ParentFrame();

Must have Methods to deal with Iframes in MS Dynamics CRM: 

As in MS Dynamics CRM, manipulation of iframes is quite tricky, here are 2 helping methods which would help a lot in building Automation Framework for it.  

    1. The first method is little generic in which you can either switch to iframe using ID, or you can pass the index of Iframe to which we need to switch in order to search for the desired elements. As due to network there might be a delay in loading the iframes, so we have used Explicit Wait for the desired operation
      *Selenium Webdriver waits are explained in the blog below;https://www.alphabold.com/automation-testing-for-dynamics-365-using-selenium/public static void SwitchToIFrame(string iFrameId)
      {
      if (int.TryParse(iFrameId, out int frame))
      {
      new WebDriverWait(Browser.Driver, TimeSpan.FromSeconds(60)).Until(drv =>
      Browser.Driver.SwitchTo().Frame(frame));
      }
      else
      {
      new WebDriverWait(Browser.Driver, TimeSpan.FromSeconds(60)).Until(drv =>
      Browser.Driver.SwitchTo().Frame(drv.FindElement(By.XPath(".//iframe[@id='" + iFrameId + "']"))));
      }
      }

 

  1. This method uses method 1 to switch to the Iframe which is currently displayed.public static void getSwitchIframe()
    {
    string iframeID = string.Empty;
    bool visibleFlag = false;
    IList IFrame = Browser.Driver.FindElements(By.XPath(@".//iframe"));
    foreach (var iframe in IFrame)
    {
    iframeID = iframe.GetAttribute("id");
    visibleFlag = iframe.Displayed;
    if (visibleFlag)
    {
    SwitchToIFrame(iframeID);
    break;
    }
    }
    }
     

As there would be multiple iframes on the current webpage and we don’t know exactly which iframe is displayed currently, so just by calling the second method we can switch to the currently displayed iframe.

If you have any question or queries, do not hesitate to reach out to us