Automation Using Selenium Webdriver

Saturday, 22 October 2016

Read background color of an element


Code For Backgroud color of an element
driver.get("http://www.google.co.in/"); String color = driver.findElement(By.name("btnK")).getCssValue("background-color"); System.out.println("The background color of Google search button"+color);















Friday, 21 October 2016

Download And Save Image

Download And Save Image Using Selenium WebDriver + Actions + Robot (1)

Interaction API tutorial section. For downloading Image, We will use same WebDriver Actions class with java robot class.
For saving Image from web page In selenium webdriver, We have to perform bellow given actions.

    Right click on Image
    Select "Save Image As" option from mouse right click context menu.
    Enter file name In save Image dialog
    Press Save button.

Right click on Image using contextClick() method of Actions class
As you know, "Save Image As" option will display when you right click on any Image. We will use contextClick() method of WebDriver Actions class to right click on Image as shown In bellow Image.

import java.awt.AWTException;
import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Actions_Test {

 WebDriver driver;
 @BeforeTest
 public void setup() throws Exception {
  driver =new FirefoxDriver();
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://any website");
 }

 @Test
 public void Save_Image() throws IOException, InterruptedException, AWTException {
  //Locate Image
  WebElement Image =driver.findElement(By.xpath("//img[@border='0']"));
  //Rihgt click on Image using contextClick() method.
  Actions action= new Actions(driver);
  action.contextClick(Image).build().perform();

  //To perform press Ctrl + v keyboard button action.
  action.sendKeys(Keys.CONTROL, "v").build().perform();

  Thread.sleep(3000);
  Robot robot = new Robot();
  // To press D key.
  robot.keyPress(KeyEvent.VK_D);
  // To press : key.
  robot.keyPress(KeyEvent.VK_SHIFT);
  robot.keyPress(KeyEvent.VK_SEMICOLON);
  robot.keyRelease(KeyEvent.VK_SHIFT);
  // To press \ key.
  robot.keyPress(KeyEvent.VK_BACK_SLASH);
  // To press "test" key one by one.
  robot.keyPress(KeyEvent.VK_T);
  robot.keyPress(KeyEvent.VK_E);
  robot.keyPress(KeyEvent.VK_S);
  robot.keyPress(KeyEvent.VK_T);
  // To press Save button.
  robot.keyPress(KeyEvent.VK_ENTER);
 }
}
}

Windows Handling in selenium webdriver


Selenium WebDriver assigns an alphanumeric id to each window as soon as the WebDriver object is instantiated. This unique alphanumeric id is called window handle. Selenium uses this unique id to switch control among several windows.

package com.windowshandling;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Windows_Handle {
public static WebDriver d;
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "d://chromedriver.exe");
d=new ChromeDriver();
d.navigate().to("https://www.facebook.com");
d.manage().window().maximize();
String mainwindow=d.getWindowHandle();
System.out.println("unique id for mainwindow:::"+mainwindow);
d.findElement(By.linkText("Terms")).click();
Set<String>windows=d.getWindowHandles();

for(String eachwindow:windows){
System.out.println("window id is:::"+windows);
if(!mainwindow.equals(eachwindow))
{
d.switchTo().window(eachwindow);
System.out.println("subwindow title is::"+d.getTitle());
d.findElement(By.xpath(".//*[@id='email']")).sendKeys("testing@gmail.com");
d.findElement(By.xpath(".//*[@id='pass']")).sendKeys("test12345");
d.findElement(By.xpath(".//*[@id='u_0_0']")).submit();

d.switchTo().window(mainwindow);
System.out.println("title is::"+d.getTitle());
d.findElement(By.xpath(".//*[@id='email']")).sendKeys("testing@gmail.com");
d.findElement(By.xpath(".//*[@id='pass']")).sendKeys("test12345");
d.findElement(By.xpath(".//*[@id='u_0_0']")).submit();
String text=d.findElement(By.xpath(".//*[@id='globalContainer']/div[3]/div/div/div/div[2]/a")).getText();
System.out.println("message is::"+text);
      }
       }
    }
}