TESTEVERYTHING

Thursday 2 August 2012

Set cookies with domain using webdriver

using web driver I have created the script for adding the cookies using domain. Basically here is the things that you must know first open the URL then add the cookies after that again open the URL now it will add.

 ______________________________________________________
package webdriver;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.Select;

public class RuntestCase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
       
       
        WebDriver driver;


        System.out.println("firefox");

        DesiredCapabilities capability = DesiredCapabilities.firefox();

        String appUrl = "http://beta.abc.com/mlb/sweepstakes/y2012/sd/alaska_form.jsp";

        capability.setBrowserName("firefox");
        capability.setPlatform(org.openqa.selenium.Platform.ANY);
        driver = new FirefoxDriver(capability);

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
       
        driver.get(appUrl);

        Calendar c = new GregorianCalendar();
        int year = 2014;
        int month = 6;
        int day = 7;
        int hour = 10;
        int minute = 26;
        int second = 47;
        c.set(year, month, day, hour, minute, second);
        Date expiry = c.getTime();
        String name = "betaaccess";
        String value = "true";
        String domain = ".abc.com";
        String path = "/";
        Cookie cookie = new Cookie(name, value, domain, path, expiry, false);
        driver.manage().addCookie(cookie);
        driver.get(appUrl);
               
            }
    }

___________________________________________________________________
// Go to the correct domain
driver.get("http://www.example.com");

// Now set the cookie. This one's valid for the entire domain
Cookie cookie = new Cookie("key", "value");
driver.manage().addCookie(cookie);

// And now output all the available cookies for the current URL
Set<Cookie> allCookies = driver.manage().getCookies();
for (Cookie loadedCookie : allCookies) {
    System.out.println(String.format("%s -> %s", loadedCookie.getName(), loadedCookie.getValue()));
}

// You can delete cookies in 3 ways
// By name
driver.manage().deleteCookieNamed("CookieName");
// By Cookie
driver.manage().deleteCookie(loadedCookie);
// Or all of them
driver.manage().deleteAllCookies();


No comments:

Post a Comment

Which one is right ?

Translate







Tweet