Monday, December 2, 2013

Selenium:Simple Webdriver program to open Browser,print browser info and close Browser

Simple Webdriver program to open Browser,print browser info and close Browser

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;


public class WebDriverDemo {


    public static WebDriver oBrowser;
    public static String sUrl="http://www.bing.com";
       
   
    public static void main(String[] args)
    {   
        boolean bIsBrowserOpened;
       
        bIsBrowserOpened=OpenBrowser();
        if(bIsBrowserOpened)
        {
            Get_BrowserInfo();
            CloseBrowser();
        }
       
    }
    public static boolean OpenBrowser()
    {
        try
        {
            oBrowser=new FirefoxDriver();
            oBrowser.get(sUrl);
            try
            {
                Thread.sleep(5000L);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return true;
        }
        catch(Exception e)
        {
            System.err.println(e.getMessage());
            return false;
        }
    }
   
    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();
    }
   

Selenium:Detecting no.of popups , Fetching their titles and closing popups using webdriver

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

Selenium: WebDriver with JUNIT

 WebDriver with JUNIT


import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import  org.junit.Assert.*;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;


public class WebDriverJunit {

public static WebDriver oBrowser;

@BeforeClass
public static void Selenium_init()
{
oBrowser=new FirefoxDriver();
oBrowser.manage().window().maximize();
oBrowser.manage().timeouts().pageLoadTimeout(15, TimeUnit.SECONDS);
oBrowser.get("http://www.bing.com");
}

@AfterClass
public static void Selenium_End()
{
oBrowser.quit();
}

@Test
public void Validate_Edit()
{
WebElement oEdit;

try
{
oEdit=oBrowser.findElement(By.id("sb_form_q"));
Assert.assertEquals("", oEdit.getText());
}
catch(Exception e)
{
Assert.fail(e.getMessage());
}
}

@Test
public void Search()
{
WebElement oEdit,oButton;
String sPageSource;

try
{
oEdit=oBrowser.findElement(By.id("sb_form_q"));
oButton=oBrowser.findElement(By.id("sb_form_go"));
oEdit.sendKeys("Selenium");
oButton.click();
sPageSource=oBrowser.getPageSource();
oBrowser.navigate().back();
if (sPageSource.lastIndexOf("Selenium")<100)
{
Assert.fail("No Proper result");
}
}
catch(Exception e)
{
Assert.fail(e.getMessage());
}
}
}

Telugu Novels:Pakudu Rallu by Ravuri Bharadhwaja

Telugu Novels:Prardhana by yandamuri veerendhranath

English Novels:Chetan Bhagat - 2 States The Story of My Marriage


English Novels: A Tale Of Two Cities


Related Posts Plugin for WordPress, Blogger...