Se#8 Handling New Window and Frame

Se#8 Handling New Window and Frame

What is covered in this article?

  1. Handling new window
  2. Handling frame in webpage

Handling new window?

Scenario 1 - Suppose you are working on application where when you click on some link it will open another application or page in new window or new tab. In Selenium when you perform click operation in application where it has opened new window or new tab, control of webdriver instance stays on first window. If we need to do something on new tab/window then we need to switch to that window and then we can perform webdriver actions.

Scenario 2 - We need to test a value in two different application, for example price of certain product on two different website. One approach is that we can launch one website get the price and then open another website in same window and then get price. Another option is we can open two website in separate window perform action while both application are opened.

We are going to see how to do such things using Selenium. Consider below simple application - https://the-internet.herokuapp.com/windows

When I click on Click Here on 1st screen it opens another tab having page title "New Window". We have to print the the title of second window.?

No alt text provided for this image

Before automating above example, lets first understand methods which will be used. Selenium provides nested interface in WebDriver which is WebDriver.TargetLocator. It is used to locate a given frame or window.?

No alt text provided for this image

Below are code snippet to demonstrate use of above methods?

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/selenium-learning/src/main/resources/chromedriver7");
WebDriver driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/windows");


//This returns current window handle
String currentWindow = driver.getWindowHandle();
System.out.println("Current window handle:" + currentWindow);


System.out.println("Available window handles before click");
Set<String> windowHandles = driver.getWindowHandles();
for(String handle : windowHandles)
System.out.println(handle);


driver.findElement(By.linkText("Click Here")).click();
System.out.println("Title before switching driver: "+driver.getTitle());


//Get handles of opened window
windowHandles = driver.getWindowHandles();
System.out.println("Available window handles after click");
for(String handle : windowHandles)
System.out.println(handle);


//Switching to new window
for(String handle : windowHandles){
if(handle!=currentWindow)
driver.switchTo().window(handle);
}
System.out.println("Title after switching driver to new window: "+driver.getTitle());


//Code to open new application in new tab from script
driver.switchTo().newWindow(WindowType.TAB).get("https://www.google.co.in");


driver.quit();        
}        

Output:?

No alt text provided for this image

Difference between driver.close() and driver.quit()

  1. close method only close current window where WebDriver has control.
  2. quit method closes all windows opened by WebDriver instance.?

Handling frame in webpage

What is frame in a webpage?

As per Wikipedia - In the context of a?web browser, a?frame?is a part of a?web page?or browser?window?which displays content independent of its container, with the ability to load content independently.?

Link - https://en.wikipedia.org/wiki/Frame_(World_Wide_Web)

Automating element inside frame

Basically it is html inside html page, to automate anything which is inside frame we need to first switch to that frame then we can perform actions on that. Selenium provides below method which are part of interface WebDriver.TargetLocator?

Methods?

No alt text provided for this image

To demonstrate above methods lets automate this webpage - https://theinternet.herokuapp.com/iframe

We need to get text highlighted in green in below window-?

No alt text provided for this image

Lets look at its html code to confirm it is inside frame -?

No alt text provided for this image

Now we can see that it is inside iframe tag with id "mce_0_ifr".?

Below are code snippet to demonstrate use of above methods?

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/selenium-learning/src/main/resources/chromedriver7");
WebDriver driver = new ChromeDriver();
driver.get("https://the-internet.herokuapp.com/iframe");
//System.out.println(driver.findElement(By.xpath("https://p")).getText()); This line will give exception Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"https://p"} because we are out of that frame


//Switching to given frame using id
driver.switchTo().frame("mce_0_ifr");
System.out.println(driver.findElement(By.xpath("https://p")).getText());
driver.quit();        
}        

Note - If you need to switch to parent frame or default frame you need to use below 2 methods -

  1. WebDriver parentFrame() - Change focus to the parent context.
  2. WebDriver defaultContent() - Selects either the first frame on the page, or the main document when a page contains iframes.


要查看或添加评论,请登录

Subodh Kumar Singh的更多文章

  • Java #7 - Array (Part-2)

    Java #7 - Array (Part-2)

    Topics Multi-dimensional array (2-Dimensional Array) Array manipulations Multi-dimensional array (2-Dimensional Array)…

  • Java #6 - Array (Part-1)

    Java #6 - Array (Part-1)

    Topics What is an array? One-dimensional array Where is the array saved in memory? What is an array? A variable can…

  • Java #5 - Variables

    Java #5 - Variables

    Topics Variable Types of variable Type Conversion and Casting Type inference - var Variable A variable is a name for a…

  • Java #4 - Data Type

    Java #4 - Data Type

    Topics Where does storage live? Where are primitive types and objects stored in memory? Primitive Types: Data Types in…

  • RestAssured#3-Why to automate webservices/REST API?

    RestAssured#3-Why to automate webservices/REST API?

    Before diving into what and how to test REST API. In this article first lets see why we should bother to test/automate…

    4 条评论
  • RestAssured#2 - Introduction to REST API

    RestAssured#2 - Introduction to REST API

    Topics What is REST API? REST API Principles HTTP Methods - GET, POST, PUT, PATCH and DELETE What is REST API? REST or…

  • RestAssured#1 - Introduction To API

    RestAssured#1 - Introduction To API

    Topics What is API? Examples of API Usage How do API work? Types of API What is webservice and its type? Difference…

    2 条评论
  • Java #3 - Introduction to OOP

    Java #3 - Introduction to OOP

    In this article, will discuss on below topics:- What are programming paradigm? What is OOP? Features of OOP? Is Java a…

  • Java #2 - Java Overview

    Java #2 - Java Overview

    In this article, will discuss on below topics:- How Java is platform independent and secure? Writing First Java Program…

  • Java #1 - History of Java

    Java #1 - History of Java

    In this article, will discuss on below topics:- A brief history of C and C++ History of Java Why Java was created? A…

社区洞察

其他会员也浏览了