Friday 8 May 2015

Handle Frames through Selenium Webdriver API



One question is arise-What is and iFrame? An iFrame (Inline Frame) is a Hyper Text Markup Language document embedded inside the current HyperText Markup Language document on a website. iFrame HTML element is used to insert content from another source, such as an advertisement, into a Web page. A Web designer can change an iFrame's content without making them reload the complete website. A website can have multiple frames on a single page. And a frame could have inner frames (Frame inside a Frame) for ie-
Frame 1|Frame3
Frame2
In Selenium web driver API to work with iFrames, we have different ways to handle frame depending on the need. Please look at the below ways of handling frames
Syntex-driver.switchTo().frame(int arg0);
Choose a frame by their (zero-based) index. That is, if a page has multiple frames (more than one), the first frame start at index "0", the second start index "1" and so on...
If the frame is selected or navigated, all subsequent calls on the WebDriver API interface are made to that frame. i.e the driver focus shall be now on the frame. Whatever operations we try to perform on pages do not work and throws element not found exception as we navigated or switched to Frame.
Parameters: Index - index(0-based)
Returns: driver focused on the given frame (which is current frame)
Throws: NoSuchFrameException - If the frame does not found.
ie: if iframe id=webklipper-publisher-widget-container-frame, it can be written as driver.switchTo().frame("webklipper-publisher-widget-container-frame"); below is the code example to work with switchToFrame using frame id.
public void switchToFramefunction(int frame) 
{
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with their id " + frame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with the id " + frame
+ e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with the id " + frame
                                      + e.getStackTrace());
               }
        }

driver.switchTo().frame(String arg0);
Choose a frame by its name / ID. The frames located by matching name attributes are always given precedence over those matched by ID.

Parameters: Name or Id - the name of the frame or the id of the frame element.
Returns: driver focused on the given frame (which is current frame)
Throws: NoSuchFrameException - If the frame does not found
Below is the example good code example using frame name.
public void switchToFrame(String frame) {
               try {
                       driver.switchTo().frame(frame);
                       System.out.println("Navigated to frame with name " + frame);
               } catch (NoSuchFrameException e) {
                       System.out.println("Unable to locate frame with id " + frame
                                      + e.getStackTrace());
               } catch (Exception e) {
                       System.out.println("Unable to navigate to frame with id " + frame
                                      + e.getStackTrace());
               }
        }
driver.switchTo().frame(WebElement frameElement);
Choose a frame using their previously located by WebElement.
Parameters: frameElement - The particular frame element to switch to.
Returns: Driver focused on the given frame (which is current frame).
Throws: NoSuchFrameException - If the given element is neither an iframe nor a frame element. and StaleElementReferenceException exception is arise - If particular Web Element has gone stale.
Below is the example of code to send an Element and  to switch.
public void switchToFrameExample(WebElement frameElement) {
try {
if (isElementPresent(frameElement)) {
driver.switchTo().frame(frameElement);
System.out.println("Navigated to frame with element "+ frameElement);
} else {
System.out.println("Unable to navigate to frame with element "+ frameElement);
                       }
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace());
} catch (StaleElementReferenceException e) {
System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace());
               }
        }
if there are any case when multiple Frames exists (Frame inside a frame), we need to first switch to the parent frame and then we need to switch to the child frame. Below is the code example to work with multiple frames.
public void switchToFrame(String ParentFrame, String ChildFrame) {
try {
driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
System.out.println("Navigated to innerframe with id " + ChildFrame
+ "which is present on frame with id" + ParentFrame);        } catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + ParentFrame
+ " or " + ChildFrame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to innerframe with id "
+ ChildFrame + "which is present on frame with id"
+ ParentFrame + e.getStackTrace());
               }
        }
After working with these frames, main important is to return back to the web page. If you do not return back to the default page, driver is throwing an exception. Below is the code example to return back to the default content.
public void switchtoDefaultFrame() {
driver.switchTo().defaultContent();
System.out.println("Navigated back to webpage from frame");
} catch (Exception e) {
System.out.println("unable to navigate back to main webpage from frame"
                                                     + e.getStackTrace());
}
}

No comments:

Post a Comment