banner



How To Handle Popup Window In Selenium Webdriver Using Java

There are many cases, where a awarding displays multiple windows when y'all 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 i: After opening the website, we demand to get the principal window handle by using driver.getWindowHandle();
The window handle volition exist in a grade of lengthy alpha numeric
Step 2: We now need to get all the window handles by using commuter.getWindowHandles();
Pace 3: We volition compare all the window handles with the principal Window handles and perform the operation the window which nosotros need.

Click hither to view Performing operations on multiple windows using reusable methods.
The below instance shows how to handle multiple windows and close all the child windows which are not need. We demand to compare the chief window handle to all the other window handles and close them.

          package com.pack;  import java.util.Prepare; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Affirm; import org.testng.annotations.Test;  public grade WindowExamples { 	static WebDriver driver;  	@Exam 	public void test_CloseAllWindowsExceptMainWindow() { 		driver = new FirefoxDriver(); 		// It will open up Naukri website with multiple windows 		commuter.go("http://world wide web.naukri.com/"); 		 		// To become the main window handle 		Cord 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 shut all the other windows except the main window. 	public static boolean closeAllOtherWindows(String openWindowHandle) { 		Set<String> allWindowHandles = driver.getWindowHandles(); 		for (Cord currentWindowHandle : allWindowHandles) { 			if (!currentWindowHandle.equals(openWindowHandle)) { 				commuter.switchTo().window(currentWindowHandle); 				driver.close(); 			} 		} 		 		driver.switchTo().window(openWindowHandle); 		if (driver.getWindowHandles().size() == 1) 			return truthful; 		else 			return fake; 	} }        

The beneath image will show y'all the multiple windows that open in the application. It has now open total of three windows (I is principal window and other ii are child windows)

Multiple windows

The below image volition bear witness the multiple window handlers for kid windows and main window. We volition take all the window handles in one gear up and we use each of them to compare and perform performance on the required window.

Multiple window handlers

The below is the output of the programme:
{2b2577c4-bf92-4392-a93f-b0428a3d9aab}
Naukri.com – Jobs – Jobs in Republic of india – Recruitment – Job Search – Employment – Job Vacancies
it is the main window
Naukri.com – Jobs – Jobs in Bharat – Recruitment – Task Search – Employment – Job Vacancies
Barclays
Naukri.com – Jobs – Jobs in India – Recruitment – Task Search – Employment – Job Vacancies
HCL
Naukri.com – Jobs – Jobs in India – Recruitment – Job Search – Employment – Task Vacancies

Source: https://www.seleniumeasy.com/selenium-tutorials/how-to-handle-windows-popups-using-selenium-webdriver

Posted by: jeromefrovessiom.blogspot.com

0 Response to "How To Handle Popup Window In Selenium Webdriver Using Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel