Detecting no.of popups , Fetching their titles and closing popups using webdriver
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.*;
// ---- Detecting no.of popups and fetching
// their titles and closing popups
// ---- closing all parent and child window with .quit() method.
public class WebDriverPopupDemo {
/**
* @param args
*/
public static WebDriver oBrowser;
public static String sUrl = "http://naukri.com";
// --- Reading existing text from TextBox
public static void main(String[] args)
{
boolean bIsBrowserOpened;
bIsBrowserOpened = OpenBrowser();
if (bIsBrowserOpened)
{
Get_Popup_Count();
Get_Popup_Title();
Closing_Popups();
CloseBrowser();
}
}
public static boolean OpenBrowser()
{
try
{
oBrowser = new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
return true;
}
public static void Get_BrowserInfo()
{
String sTitle, sSource;
sTitle = oBrowser.getTitle();
sSource = oBrowser.getPageSource();
System.out.println("Page Title ="+sTitle);
System.out.println("******");
System.out.println("Page Source....");
System.out.println(sSource);
}
public static void CloseBrowser()
{
// oBrowser.close(); // --- closes only parent
oBrowser.quit(); // --- closes all, including popups..
}
public static void Get_Popup_Count()
{
int iPopupCount;
iPopupCount = oBrowser.getWindowHandles().size();
if (iPopupCount == 1)
{
System.out.println("Only! parent window & no popups");
}
else
{
System.out.println("parent and popup both exist");
System.out.println("Total No of Windows = "+iPopupCount);
}
}
public static void Get_Popup_Title()
{
int iPopupCount, iPopup;
Object[] lsAllHandles;
String sWindowTitle;
lsAllHandles = oBrowser.getWindowHandles().toArray();
iPopupCount = lsAllHandles.length;
for (iPopup=0; iPopup<iPopupCount; iPopup++)
{
sWindowTitle = oBrowser.switchTo().window((String) lsAllHandles[iPopup]).getTitle().toString();
if (iPopup==0)
{
System.out.printf("\n Main window title = %s",
sWindowTitle);
}
else
System.out.printf("\n window (%d of %d) = %s",
iPopup, iPopupCount-1,sWindowTitle);
}
}
public static void Closing_Popups()
{
int iPopupCount, iPopup;
Object[] lsAllHandles;
String sWindowTitle;
lsAllHandles = oBrowser.getWindowHandles().toArray();
iPopupCount = lsAllHandles.length;
if (iPopupCount>0) // -- there are popups
{
for (iPopup=1; iPopup<iPopupCount; iPopup++)
{
sWindowTitle = oBrowser.switchTo().window((String) lsAllHandles[iPopup]).getTitle().toString();
System.out.printf("\n Closing Pop Window (%d of %d)=%s",
iPopup,iPopupCount-1,sWindowTitle);
oBrowser.switchTo().window((String)lsAllHandles[iPopup]).close();
}
oBrowser=oBrowser.switchTo().window((String)lsAllHandles[0]);
}
}
}
import org.openqa.selenium.firefox.*;
// ---- Detecting no.of popups and fetching
// their titles and closing popups
// ---- closing all parent and child window with .quit() method.
public class WebDriverPopupDemo {
/**
* @param args
*/
public static WebDriver oBrowser;
public static String sUrl = "http://naukri.com";
// --- Reading existing text from TextBox
public static void main(String[] args)
{
boolean bIsBrowserOpened;
bIsBrowserOpened = OpenBrowser();
if (bIsBrowserOpened)
{
Get_Popup_Count();
Get_Popup_Title();
Closing_Popups();
CloseBrowser();
}
}
public static boolean OpenBrowser()
{
try
{
oBrowser = new FirefoxDriver();
oBrowser.get(sUrl);
try
{
Thread.sleep(5000L);
}
catch (Exception e)
{
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println(e.getMessage());
return false;
}
return true;
}
public static void Get_BrowserInfo()
{
String sTitle, sSource;
sTitle = oBrowser.getTitle();
sSource = oBrowser.getPageSource();
System.out.println("Page Title ="+sTitle);
System.out.println("******");
System.out.println("Page Source....");
System.out.println(sSource);
}
public static void CloseBrowser()
{
// oBrowser.close(); // --- closes only parent
oBrowser.quit(); // --- closes all, including popups..
}
public static void Get_Popup_Count()
{
int iPopupCount;
iPopupCount = oBrowser.getWindowHandles().size();
if (iPopupCount == 1)
{
System.out.println("Only! parent window & no popups");
}
else
{
System.out.println("parent and popup both exist");
System.out.println("Total No of Windows = "+iPopupCount);
}
}
public static void Get_Popup_Title()
{
int iPopupCount, iPopup;
Object[] lsAllHandles;
String sWindowTitle;
lsAllHandles = oBrowser.getWindowHandles().toArray();
iPopupCount = lsAllHandles.length;
for (iPopup=0; iPopup<iPopupCount; iPopup++)
{
sWindowTitle = oBrowser.switchTo().window((String) lsAllHandles[iPopup]).getTitle().toString();
if (iPopup==0)
{
System.out.printf("\n Main window title = %s",
sWindowTitle);
}
else
System.out.printf("\n window (%d of %d) = %s",
iPopup, iPopupCount-1,sWindowTitle);
}
}
public static void Closing_Popups()
{
int iPopupCount, iPopup;
Object[] lsAllHandles;
String sWindowTitle;
lsAllHandles = oBrowser.getWindowHandles().toArray();
iPopupCount = lsAllHandles.length;
if (iPopupCount>0) // -- there are popups
{
for (iPopup=1; iPopup<iPopupCount; iPopup++)
{
sWindowTitle = oBrowser.switchTo().window((String) lsAllHandles[iPopup]).getTitle().toString();
System.out.printf("\n Closing Pop Window (%d of %d)=%s",
iPopup,iPopupCount-1,sWindowTitle);
oBrowser.switchTo().window((String)lsAllHandles[iPopup]).close();
}
oBrowser=oBrowser.switchTo().window((String)lsAllHandles[0]);
}
}
}
No comments:
Post a Comment