Wednesday 2 September 2015

How to perform switching between 3 windows

package com.multiplewindows;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class MultiWindows22 {

    WebDriver d;
    public String mainWindow;
    public String childWindow;
    public String child1;
    public String child2;
    @Test
    public void testscript() throws InterruptedException {
        d.get("http://www.bing.com/");
        //d.findElement(By.xpath(".//*[@id='msn']/a")).click(); 
        d.findElement(By.xpath(".//*[text()='MSN']")).click();
        //Thread.sleep(3000);
         //d.findElement(By.xpath(".//*[@id='office']/a")).click();
        //d.findElement(By.xpath(".//*[text()='Office Online']")).click();
        Thread.sleep(3000);
        mainWindow=d.getWindowHandle();
        Set<String> wids=d.getWindowHandles();
        Iterator i=wids.iterator();
        while (i.hasNext()) {
           
            String windows=(String)i.next();
            System.out.println("windows are"+windows);
             if (!windows.equalsIgnoreCase(mainWindow)) {
                 d.switchTo().window(windows);
                 Thread.sleep(2000);
                 childWindow=d.getWindowHandle();
                    Thread.sleep(2000);
                    d.findElement(By.id("q")).sendKeys("selenium");   
                    d.findElement(By.id("sb_form_go")).click();
                    Thread.sleep(6000);
                    break;   
            }
        }   
        Thread.sleep(5000);
        Set<String> wids1=d.getWindowHandles();
        for (String string : wids1) {

            if (!mainWindow.equalsIgnoreCase(string)&&!childWindow.equalsIgnoreCase(string)) {
                d.switchTo().window(string);
                Thread.sleep(6000);
                child1=d.getWindowHandle();
                d.findElement(By.xpath(".//*[@id='id_sc']")).click();
                Thread.sleep(6000);
                break;
            }
        }
        Thread.sleep(5000);
       
       
    }
   
    @BeforeClass
    public void beforeClass() {
        d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
    @AfterClass
    public void afterClass() {
        d.quit();
    }
   
}

How to find number of links in selenium

public class NumberofLinks {
    public static void main(String[] args) throws InterruptedException {
        int enable=0;
        int disable=0;
    
        WebDriver d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
       
        d.get("https://www.google.com");
       
        d.findElement(By.name("q")).sendKeys("selenium");
        Thread.sleep(2000);
       
        List<WebElement> elements=d.findElements(By.tagName("a"));
        System.out.println(elements.size());
        for (WebElement webElement : elements) {
            if (webElement.isEnabled()) {
                enable++;
            }
           
            else {
               
                disable++;
            }
    }
        System.out.println("enable links are:"+enable);
        System.out.println("disable links are:"+disable);
        Thread.sleep(5000);
    }
   
}

How to perform Key Board Operatiionsssss

public class KeyboardOperations {

public static void main(String[] args) throws InterruptedException {
    
        WebDriver d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
        d.get("https://www.google.com");
        Thread.sleep(2000);
        d.findElement(By.name("q")).sendKeys("selenium");
        Thread.sleep(2000);
        d.findElement(By.xpath(".//*[@class='sbsb_b']/li[2]/div/div[2]")).click();
        Thread.sleep(2000);
        Actions a = new Actions(d);
       
        a.sendKeys(Keys.END).build().perform();;
        Thread.sleep(2000);
        a.sendKeys(Keys.HOME).build().perform();
        Thread.sleep(2000);
       
        a.sendKeys(Keys.ARROW_DOWN).build().perform();
        Thread.sleep(2000);
        a.sendKeys(Keys.ARROW_UP).build().perform();
        Thread.sleep(2000);
   
        a.sendKeys(Keys.F5).build().perform();
        Thread.sleep(2000);
       
        /*a.sendKeys(Keys.chord(Keys.CONTROL,"s")).build().perform();;
        Thread.sleep(2000);*/
       
        a.sendKeys(Keys.chord(Keys.CONTROL,Keys.SHIFT,"s")).build().perform();
        Thread.sleep(5000);

}
   
}

Right Click Operations

public class RightClick {

    public static void main(String[] args) throws InterruptedException {
    
        WebDriver d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
        d.get("https://www.google.com");
        Thread.sleep(2000);
        d.findElement(By.name("q")).sendKeys("selenium");
        Thread.sleep(2000);
       
        d.findElement(By.xpath(".//*[@id='sblsbb']/button/span")).click();
       
           WebElement e=d.findElement(By.linkText("Selenium - Web Browser Automation"));
       
        Actions a = new Actions(d);
       
//a.contextClick(d.findElement(By.linkText("Selenium - Web Browser Automation"))).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();;
       
        Actions action = new Actions(d).contextClick(e);
        Thread.sleep(6000);
        action.sendKeys(Keys.ARROW_DOWN);
        Thread.sleep(6000);
        action.sendKeys(Keys.ENTER).build().perform();;
       
    }
   
   
}

File Upload using sendkeys

public class FileUpload {

    public static void main(String[] args) throws InterruptedException {
     
        WebDriver d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
       
        d.get("https://www.sendspace.com");
       
        d.findElement(By.id("upload_file")).sendKeys("C:\\Users\\Desktop\\aa.txt");
        Thread.sleep(5000);
        d.quit();
    }
   
}

Explicit Wait Exmple

public class ExplicitDemo {

    public static void main(String[] args) throws InterruptedException {
   
        WebDriver d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(45, TimeUnit.SECONDS);
       
        //d.get("https://www.gmail.com");
        d.navigate().to("https://www.gmail.com");
        Thread.sleep(2000);
        //enter text into username field
        d.findElement(By.name("Email")).sendKeys("rameshatbtech@gmail.com");
        Thread.sleep(2000);
               
                //click on next button    
                //d.findElement(By.id("next")).click();
               
        //explicit wait for search field
        WebDriverWait wait = new WebDriverWait(d, 10);
       // wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("next")));
       
        wait.until(ExpectedConditions.elementToBeClickable(By.id("next")));
       
        d.findElement(By.id("next")).sendKeys(Keys.ENTER);
        Thread.sleep(3000);
    }
    }

Cookie Management System

public class CookieDemo {
WebDriver d;
Set<Cookie> s;
    @Test
    public void testExecution() {
       
        d= new FirefoxDriver();
   
        d.get("https://www.flipkart.com");
        //getting all the cookies and retrieving
        s=d.manage().getCookies();
        for (Cookie cookie : s) {
            System.out.println("cookie name :"+cookie.getName());
            System.out.println("cookie values:"+cookie.getValue());
        }
              d.manage().deleteAllCookies();
       
            }
}