Handling Frames, Multiple Browsers, Mouse hover in Selenium


Handling Frames, Multiple Browsers, Mouse hover in Selenium

Handling Frames

HTML Frames are used to divide the Browser window into multiple sections, whenever we access the page then focus on the top window.

Operating Elements in Frames

1) Manual Testing:

You can directly operate elements in any frame of the Page.

2) Test Automation / Automated Testing:

Switch from Top window to particular frame then operate elements.
-------------------------------------
Example:

Launch Web Page (It has 3 frames)

i) Click 3rd link in 3rd frame
ii) Click 2nd Link in 1st Frame
iii) Click 1st Link in 2nd frame
------------------------------------------
Test Automation:

a) Launch the web page
b) Switch to 3rd frame and operate the element
c) Switch to Top window
d) Switch to 1st frame and operate the element
e) Switch to Top window
f) Switch to 2nd frame and operate the element
-------------------------------------
Switch from Top window to a frame is done in two ways

i) Using Frame Index

Syntax:

driver.switchTo().frame(int index);

Example:

WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame(2);
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[5]/td[1]/a")).click();
-------------------------------------
ii) Using Frame Name

Syntax:
driver.switchTo().frame(String Name);



Example:

WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");
driver.switchTo().frame("classFrame");
driver.findElement(By.xpath("html/body/div[3]/table/tbody[2]/tr[5]/td[1]/a")).click();
--------------------------------------------------------
From a frame to Top window

Syntax:

driver.switchTo().defaultContent();
------------------------------------------
Handling Frames Example:
> Launch the Web Page
> Switch to 3rd Frame
> Operate an Element
> Back to Top Window
> Switch to 1st Frame
> Operate an Element
----------------------------------
a) Example using Frame Name

WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");

//Switch from Top window to 3rd Frame using Frame name
driver.switchTo().frame("classFrame");
driver.findElement(By.linkText("org.openqa.selenium")).click();
Thread.sleep(3000);
//Back to Top window
driver.switchTo().defaultContent();
Thread.sleep(3000);
//Switch from Top window to 1st Frame using Frame name
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("org.openqa.selenium.io")).click();
--------------------------------------------------------------
b) Example using Frame Index

WebDriver driver = new FirefoxDriver();
driver.get("http://seleniumhq.github.io/selenium/docs/api/java/index.html");

//Switch from Top window to 3rd Frame using Frame name
driver.switchTo().frame(2);
driver.findElement(By.linkText("org.openqa.selenium")).click();
Thread.sleep(3000);
//Back to Top window
driver.switchTo().defaultContent();
Thread.sleep(3000);
//Switch from Top window to 1st Frame using Frame name
driver.switchTo().frame(0);
driver.findElement(By.linkText("org.openqa.selenium.io")).click();
driver.switchTo().defaultContent();
Thread.sleep(3000);
driver.navigate().back();
driver.switchTo().frame(1);
driver.findElement(By.linkText("Action")).click();
-------------------------------------
Handling Mouse hover
 

WebDriver driver = new FirefoxDriver();
driver.get("https://www.carmax.com");

//Create Actions Instance by Passing the driver reference
Actions builder = new Actions(driver);

WebElement menu = driver.findElement(By.linkText("FIND YOUR CAR"));

builder.moveToElement(menu).build().perform();
driver.findElement(By.linkText("Transfers")).click();
-------------------------------------
Handling Multiple Browsers
 

We handle multiple browsers using Browser Window handles.

Example 1:

WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/LoginPage.html");

String parent = driver.getWindowHandle();
//System.out.println(parent);   
   
driver.findElement(By.linkText("Sign In")).click();   
   
Set <String> Browsers = driver.getWindowHandles();   
System.out.println(Browsers.size());   
   
for (String i : Browsers){
if (! i.equals(parent)){
    driver.switchTo().window(i);
    System.out.println(driver.getCurrentUrl());
}
}
driver.switchTo().window(parent);
System.out.println(driver.getCurrentUrl());
-------------------------------------
Example 2:
 

> Launch the Web Page
> Click Sign In Link (It opens new window)
> Enter some value into Login Username Edit box in 1st browser
> Enter some value into Enter Your details edit box in 2nd browser
------------------------------------------------------

Selenium WebDriver Test Script/Program

WebDriver driver = new FirefoxDriver();
driver.get("file:///E:/HTMLExamples/LoginPage.html");

driver.findElement(By.linkText("Sign In")).click();

driver.findElement(By.id("username")).sendKeys("India123");
Thread.sleep(3000);
String Browser1 = driver.getWindowHandle();

Set <String> Browsers = driver.getWindowHandles();

for (String windowHandle: Browsers){
if (!windowHandle.equals(Browser1)){
driver.switchTo().window(windowHandle);
driver.findElement(By.xpath(".//*[@id='signin']")).sendKeys("Selenium Testing");
}
}
Thread.sleep(3000);
driver.switchTo().window(Browser1);
driver.findElement(By.id("pass word")).sendKeys("abcd123");
}
}
-----------------------------------------------------------------

1 comments:

Hi Sir,

I am unable to copy the code mentioned above so can you please update it accordingly...It will be helpful.

Thanks!!

Post a Comment