Sunday, 13 January 2013

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]);
        }
       
       
    }
   
   

}

3 comments:

  1. It was very nice article and it is very useful to Selenium learners.We also provide Cub training software online training.

    ReplyDelete

  2. Advanced Selenium Framework Training in Chennai by Vishwa

    Hi, Reach Mr.Vishwa for Best Selenium and Coded UI Training in Chennai with real time project assistance. This people will teach everything from the basics up to advanced level scenarios from frameworks.

    I learned from Mr.Vishwa and Raj they are very good automation people for Selenium Frameworks. After joined with them I learned many things and now I am writing automation scripts in own project. Vishwa 9003085882
    selenium training in chennai

    ReplyDelete