Wednesday, November 12, 2014


In this tutorial, I will tell you how to delete cookies -

1. We need to create an object of cookie and pass the cookie name and value to the cookie's constructor as an argument.

2. Add cookie using selenium webdriver.

3. Now delete cookies using selenium webdriver.





Let's See The Example


import java.util.Set;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();

  driver.navigate().to("http:// www.flipkart. com/");

  Cookie name = new Cookie("mycookie", "12345");
  driver.manage().addCookie(name);

  // Let's get the cookie's value
  Set<cookie> cookies = driver.manage().getCookies();
  for (Cookie getcookies : cookies) {
   System.out.println(getcookies);
  }

  /*
   * You can delete cookies in 3 ways 1. By name
   * driver.manage().deleteCookieNamed("CookieName");
   * 
   * 2. By Cookie driver.manage().deleteCookie(getcookies);
   * 
   * 3. All Cookies driver.manage().deleteAllCookies();
   */
  driver.manage().deleteAllCookies();

 }

}






In this tutorial, I will tell you how to add cookie -

1. We need to create an object of cookie and pass the cookie name and value to the cookie's constructor as an argument.

2. Now add cookie using selenium webdriver.





Let's See Example

import java.util.Set;

import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {

public static void main(String[] args) throws InterruptedException {

WebDriver driver = new FirefoxDriver();

driver.navigate().to("http:// www.flipkart. com/");

Cookie name = new Cookie("mycookie", "12345");
driver.manage().addCookie(name);

// Let's get the cookie's value
Set<cookie> cookies = driver.manage().getCookies();
for (Cookie getcookies : cookies) {
System.out.println(getcookies);
}

}

}







Output -



A cookie is a small file stored on the client computer by the server. If the user requests a web page then the server uses cookie information to identify the user. With selenium, you can create, get and delete cookies.

1. Create cookie - There are several ways to create cookie.

         a) Cookie name = new Cookie("cookieName", "cookieValue");
             driver.manage().addCookie(Cookie arg0);


Click here for example.





2. Get cookies data - There are several ways to get cookie.

         a) driver.manage().getCookies();
         b) driver.manage().getCookieNamed(String arg0);


Click here for example.


3. Delete cookie - There are several ways to delete cookie.

         a) driver.manage().deleteCookieNamed("String arg0");
         b) driver.manage().deleteCookie(Cookie arg0);
         c) driver.manage().deleteAllCookies();


Click here for example.






Tuesday, November 4, 2014


A JavaScript prompt box is used when you want the user to enter a value before going to a page.

When a prompt box pops up, the user will have to enter a value by using WebElement sendkeys("String str") methond and then click either "OK" or "Cancel" to proceed after entering an input value.

If the user clicks "OK" the prompt box will return the value. If the user clicks "Cancel" the box returns null.





JavaScript Prompt Box Example



import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PromptExample {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http : //w3schools. com/js/tryit.asp?filename=tryjs_prompt");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  alert.sendKeys("Hello Selenium");
  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();

 }

}







A JavaScript confirm popup box is used when you want the user to verify or accept something. In confirm box, the user can click either "OK" button or "Cancel" button to proceed.

If the user clicks on "OK" button, the confirm popup box will return true. If the user clicks on "Cancel" button, the confirm popup box will return false.





JavaScript Confirm Box Example


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class ConfirmExample {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www.w3schools. com/js/tryit.asp?filename=tryjs_confirm");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();

 }

}








A JavaScript alert box is used to provide some information to the user. Alert box provides only "OK" button. So, the user will have to click "OK" to proceed.





JavaScript Alert Box


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class AlertExample {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www.w3schools. com/js/tryit.asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();

 }

}






JavaScript alert, confirm and prompt popup is an web element. To handle JavaScript alert, confirm and prompt popup, Selenium comes with Alert interface.

Selenium Alert interface provides methods to

1. Click "OK" button

2. Click "Cancle" button

3. Get text from popup boxes

4. Enter text to popup boxes


Alert Interface & It's Methods







1. To move control from main window or web page to the JavaScript popup box.

Alert alert = driver.switchTo().alert();


2. Click on "OK" button.

alert.accept();


3. Click on "Cancel" button.

alert.dismiss();


4. To enter text to popup boxes.

alert.sendKeys();


5. To get text from popup boxes.

alert.getText();







Frames is a web page within another web page. A web page can have one or more frames.

Each frame has its own unique numeric id and name. Selenium WebDriver uses this unique id or name to switch control between frames.





Syntax -

1. driver.switchTo().frame(int arg0);

2. driver.switchTo().frame(String arg0);

3. driver.switchTo().frame(WebElement arg0);


How To Switch Between Many Frames


import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.navigate().to(
    "http:// www.w3schools. com/js/tryit.asp?filename=tryjs_alert");

  driver.switchTo().frame(0);

  Thread.sleep(5000);
  WebElement element = driver.findElement(By.tagName("button"));
  element.click();

  Alert alert = driver.switchTo().alert();

  System.out.println(alert.getText());
  Thread.sleep(5000);
  alert.accept();
  driver.switchTo().defaultContent();

 }

}






Web page may have multiple windows. Multiple windows means a link on a web page of another web page. Current web page is called main window and other windows called child window.

Each window has its own unique alphanumeric id. Selenium WebDriver uses this unique id to switch control between multiple windows.





How To Switch Between Multiple Windows



import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Training {

 public static void main(String[] args) throws InterruptedException {

  WebDriver driver = new FirefoxDriver();
  driver.get("http://reditblog.blogspot.in/p/selenium.html");
  driver.manage().window().maximize();
  Thread.sleep(5000);
  String mainWindow = driver.getWindowHandle();

  WebElement element = driver
    .findElement(By
      .linkText("Org.Openqa.Selenium.Firefox. NotConnectedException"));

  element.click();

  Thread.sleep(5000);
  for (String windowHandle : driver.getWindowHandles()) {
   if(windowHandle.equals(mainWindow)){
    System.out.println("Main Window - " + " "+ windowHandle);
    driver.switchTo().window(windowHandle);    
    System.out.println(driver.getTitle());
    
   }else{
    System.out.println("Child Window - " + " "+ windowHandle);
    Thread.sleep(5000);
    driver.switchTo().window(windowHandle);
    Thread.sleep(5000);
    System.out.println(driver.getTitle());
    driver.switchTo().defaultContent();   
    
   }
   
  }

 }

}