Friday 2 August 2013

How to deal with multiple frames with dynamic Ids using webdriver.

Here I would like to address an issues that I faced while upgrading the existing automation framework of selenium webdriver  to support HTML5 UI.
Earlier Application Under Test (AUT) was developed in Icefaces means all tables, popups,controltypes were developed in icefaces. later on management decided to revamped the IceFaces UI with HTML 5.
To render the old UI of Icefaces on HTML5 , developers has deployed an additional Iframe for each view. And it has created a big challenge for me to keep existing tests running on the new UI without modifying the tests. I followed following approach in updating the framework/keywords to make sure test won’t need an update.

Main challenge :
In this assignment the main challenge was to attach an iframes with dynamic IDs .Switching between different iframes, once user has changed the Views.
In this I was not aware of the frame ID to whom I have to attaché webdirver , because each frame was having a dynamic ID starting with String ‘Frame’ for e.g Frame1256893
I could use this as an xpath :
String frameXpath = "//iframe[starts-with(@id,'Frame')]";


But it will not solve all my problems because there could be multiple views opened at a time with only one view in a focus.if I would have been used only above xpath then it would have always attach the webdriver control to first frame

How can we achiev it ?

protected void attachToVisibleViewFrame() {
  String frameXpath = "//iframe[starts-with(@id,'Frame')]";
  driver.switchTo().defaultContent();
  WebElement[] framesElement = wedriverInstance.findElements(
    By.xpath(frameXpath), 3);
  if (framesElement != null) {
   for (WebElement frameElement : framesElement) {
    if (frameElement.isDisplayed() && frameElement.isEnabled()) {
     System.out
       .println("PortalView : WebDriver control switched to a frame with label : "
         + frameElement.getAttribute("view-label"));
     driver.switchTo().frame(frameElement);

     break;
    }
   }
  }
 }
Above code additionally checks whether iframe for which webdriver is trying to get attach isDisplayed and isEnabled.so it will always attach to a frame which is in focus and not to a frame which is hidden.
Hope this will help you to avoid an overhead of multiple frames within your application.

No comments:

Post a Comment