SDET- QA Automation Techie

Software Testing Blog

  • Home
  • Training
    • Online
    • Self-Paced
  • Video Tutorials
  • Interview Skills
    • HR Interview Questions Videos
    • Domain Knowledge
  • Career Guidance
  • Home
  • Software Testing
    • Manual Testing Tutorials
    • Manual Testing Project
    • Manaul Testing FAQS
    • ISTQB
    • AGILE
  • Web Automation Testing
    • Java Programmng
    • Python Programmng
    • Selenium with Java
    • Selenium with Python
    • Robot Framework(Selenium with Python)
    • selenium with Cucumber
    • TestNG+IntelliJ
    • Mobile App Testing(Appium)
    • JMeter
  • API Automation Testing
    • Rest Assured API Testing (BDD)
    • Rest Assured API Testing (Java+ TestNG)
    • Robot Framework(Rest API Testing with Python)
    • Postman
    • SoapUI
    • API Testing(FAQ's)
  • SDET|DevOps
    • Continuos Integration
    • SDET Essentials
    • AWS For Testers
    • Docker
  • SQL
    • Oracle(SQL)
    • MySQL for Testers
    • NoSQL
  • Unix/Linux
    • UNIX TUTORIALS
    • Linux Shell Scripting
  • ETL Testing
    • ETL Data warehouse Tutorial
    • ETL Concepts Tools and Templates
    • ETL Testing FAQ's
    • ETL Testing Videos
  • Big Data Hadoop
  • Video Tutorials
  • ApachePOI Video Tutorials
  • Downloads
    • E-Books for Professionals
    • Resumes
  • Automation Essencials
    • Cloud Technologies
      • Docker For Testers
      • AWS For Testers
      • Sub Child Category 3
    • Java Collections
    • Selenium Locators
    • Frequently Asked Java Programs
    • Frequently Asked Python Programs
    • Protractor
    • Cypress Web Automation

Handle windows popups using Selenium Webdriver

 Handle windows popups using Selenium Webdriver   

There are many cases, where a application displays multiple windows when you open a website. Those are may be advertisements or may be a kind of information showing on popup windows. We can handle multiple windows using Windows Handlers in selenium webdriver.
Step 1: After opening the website, we need to get the main window handle by using driver.getWindowHandle();
The window handle will be in a form of lengthy alpha numeric
Step 2: We now need to get all the window handles by using driver.getWindowHandles();
Step 3: We will compare all the window handles with the main Window handles and perform the operation the window which we need.

Click here to view Performing operations on multiple windows using reusable methods.
The below example shows how to handle multiple windows and close all the child windows which are not need. We need to compare the main window handle to all the other window handles and close them.
package com.pack;

import java.util.Set;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

public class WindowExamples {
 static WebDriver driver;

 @Test
 public void test_CloseAllWindowsExceptMainWindow() {
  driver = new FirefoxDriver();
  // It will open Naukri website with multiple windows
  driver.get("http://www.naukri.com/");
  
  // To get the main window handle
  String windowTitle= getCurrentWindowTitle();
  String mainWindow = getMainWindowHandle(driver);
  Assert.assertTrue(closeAllOtherWindows(mainWindow));
  Assert.assertTrue(windowTitle.contains("Jobs - Recruitment"), "Main window title is not matching");
 }
  
 public String getMainWindowHandle(WebDriver driver) {
  return driver.getWindowHandle();
 }

 public String getCurrentWindowTitle() {
  String windowTitle = driver.getTitle();
  return windowTitle;
 }
 
 //To close all the other windows except the main window.
 public static boolean closeAllOtherWindows(String openWindowHandle) {
  Set allWindowHandles = driver.getWindowHandles();
  for (String currentWindowHandle : allWindowHandles) {
   if (!currentWindowHandle.equals(openWindowHandle)) {
    driver.switchTo().window(currentWindowHandle);
    driver.close();
   }
  }
  
  driver.switchTo().window(openWindowHandle);
  if (driver.getWindowHandles().size() == 1)
   return true;
  else
   return false;
 }
}
The below image will show you the multiple windows that open in the application. It has now open total of three windows (One is main window and other two are child windows)
Multiple windows
The below image will show the multiple window handlers for child windows and main window. We will have all the window handles in one set and we use each of them to compare and perform operation on the required window.
Multiple window handlers
The below is the output of the program:
{2b2577c4-bf92-4392-a93f-b0428a3d9aab}
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
it is the main window
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
Barclays
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
HCL
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Job Vacancies
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to TwitterShare to Facebook
Newer Post Older Post Home
popup

Popular Posts

  • How To Explain Project In Interview Freshers and Experienced
    “ Describe an important project you’ve worked on ” is one of the most common questions you can expect in an interview. The purpose of a...
  • MANUAL TESTING REAL TIME INTERVIEW QUESTIONS & ANSWERS
    1. How will you receive the project requirements? A. The finalized SRS will be placed in a project repository; we will access it fr...
  • API/Webservices Testing using RestAssured (Part 1)
    Rest Assured : Is an API designed for automating REST services/Rest API's Pre-Requisites Java Free videos: https://www.you...

Facebook Page

Pages

  • Home
  • Resumes
  • Job Websites India/UK/US
  • ISTQB
  • Selenium with Java
  • E-Books for Professionals
  • Manual Testing Tutorials
  • Agile Methodology
  • Manual Testing Projects

Live Traffic

YouTube


Blog Visitors

Copyright © SDET- QA Automation Techie | Powered by Blogger
Design by SDET | Blogger Theme by | Distributed By Gooyaabi Templates