How To Add New Cookie When Executing Selenium WebDriver Test

Earlier nosotros learnt how to extract together with impress electrical current domain cookie during seek out execution using selenium WebDriver In previous post. Sometimes you lot bring a requirement similar you lot bring to add novel cookie for website nether seek out during selenium WebDriver seek out execution for around sort of testing
purpose. Is It possible?

Yes, Selenium WebDriver has Inbuilt shape named Cookie which contains many useful functions to play amongst cookie of website nether test. You tin toilet practise novel cookie amongst your desired mention together with value together with and hence you lot tin toilet use addCookie method to add together novel cookie In browser for your seek out domain. Syntax to add together novel cookie Is equally bellow.

//Set cookie value together with and hence add together It for electrical current domain.
Cookie mention = novel Cookie("testCookie", "WSfed-ffsd-234DFGe-YUTYU");
driver.manage().addCookie(name);

Full instance to add together novel cookie together with and hence impress It In console to verify that novel added cookie Is proper or non Is equally bellow.

Note : Add URL of your website nether seek out In bellow given example.

package Testing_Pack;  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.BeforeTest; import org.testng.annotations.Test;  populace shape addCookies {  WebDriver driver;    @BeforeTest  populace void setup() throws Exception {   driver = novel FirefoxDriver();   driver.manage().window().maximize();   driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);   driver.get("Your seek out site URL");  }    @Test  populace void addCookie(){   //Set cookie value together with and hence add together It for electrical current domain.   Cookie mention = novel Cookie("testCookie", "WSfed-ffsd-234DFGe-YUTYU");   driver.manage().addCookie(name);      //Get all cookies together with impress them.   Set<Cookie> totalCookies = driver.manage().getCookies();   System.out.println("Total Number Of cookies : " +totalCookies.size());      for (Cookie currentCookie : totalCookies) {       System.out.println(String.format("%s -> %s -> %s", "Domain Name : "+currentCookie.getDomain(), "Cookie Name : "+currentCookie.getName(), "Cookie Value : "+currentCookie.getValue()));   }    } }

It volition add together novel cookie for your seek out domain together with and hence all cookies volition hold out printed In console. Read side yesteryear side postal service to know how to delete cookie.
More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post