Set Cookies in Selenium Webdriver / Run JavaScript


There have been instances where I would run JavaScript snippets from Java's WebDriver

WebDriver driver = new AnyDriverYouWant();
if (driver instanceof JavascriptExecutor) {
    ((JavascriptExecutor)driver).executeScript("yourScript();");
}
Example: ((JavascriptExecutor) driver).executeScript("window.focus()");
You can find more documentation on this in the JavaDocs of JavascriptExecutor.

Now the above example is good with JS being run directly buy what if you want to set a cookie?
If you are new to setting cookies then read this: http://www.quirksmode.org/js/cookies.html

Cookie ck = new Cookie("name", "value");
driver.manage().addCookie(ck);
Cookie cookie = new Cookie(name, value, domain, path, expiry);
Read this for Selenium!

Comments

Popular posts from this blog

XPATH for IE / internet explorer

All time good software testing books!

Website's underlying technology for automation