Monday, 7 August 2017

Code Example - iframe With Different Methods

package com.seleniumWebdriver;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 7-08-2017
 *
 */
public class IframeWithDifferentMethods {
static WebDriver driver;

public static boolean isElementPresent(WebElement element) {
boolean flag = false;
try {
if (element.isDisplayed() || element.isEnabled())
flag = true;
} catch (NoSuchElementException e) {
flag = false;
} catch (StaleElementReferenceException e) {
flag = false;
}
return flag;
}

public void switchToFrame(int frame) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with id " + 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());
}
}

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());
}
}

public void switchToFrame(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());
}
}

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());
}
}

public void switchtoDefaultFrame() {
try {
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

Code Example - File Download By Robot Class In Firefox Browser

Exported from Notepad++ package com . practiceCode ; import java . awt . AWTException ; import java . awt . Robot ; import j...