Friday, 4 August 2017

Code Example - Add Cookie

package com.seleniumWebdriver;

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

import org.openqa.selenium.Cookie;
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;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class AddCookie {

WebDriver driver;
String URL = "http://flipkart.com/";

@BeforeClass
public void preCondtion() {
try {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

} catch (Exception e) {
org.testng.Assert.fail("Test failed");
}

}

@Test
public void addcookie() {

driver.navigate().to(URL);
// we should pass name and value for cookie as parameters in this example we are passing, name=mycookie and value=123456789123
Cookie name = new Cookie("mycookie", "123456789123");
driver.manage().addCookie(name);

// After adding the cookie we will check that by displaying all the cookies.
Set<Cookie> cookiesList = driver.manage().getCookies();
for (Cookie getcookies : cookiesList) {
System.out.println(getcookies);
}
}

@Test
public void deleteCookieNamedExample() {
driver = new FirefoxDriver();
String URL = "http://www.flipkart.com";
driver.navigate().to(URL);
driver.manage().deleteCookieNamed("__utmb");
}

@Test
public void deleteAllCookiesExample() {
driver = new FirefoxDriver();
String URL = "http://www.flipcart.com";
driver.navigate().to(URL);
driver.manage().deleteAllCookies();
}

@AfterClass
public void end() {
driver.quit();
}

}

No comments:

Post a Comment

Code Example - File Download By Robot Class In Firefox Browser

Exported from Notepad++ package com . practiceCode ; import java . awt . AWTException ; import java . awt . Robot ; import j...