Suppose we have to test for the following scenario:
1. Go to login page and login to the application 2. Close the browser 3. Open the browser and go to login page � the user should not see login form and should be already logged in.
On first login, cookies are stored in the browser. In WebDriver, when the browser window is closed, all session data and cookies are deleted, so the testing of the above scenario becomes impossible.
Luckily, WebDriver has functionality to read the cookies from the browser before closing it and then restore the cookies in the new browser window.
Restore Cookies in New Browser Java package com.testingexcellence.webdriver import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert;
import java.util.Set;
public class CookieTest { WebDriver driver;
@Test public void login_state_should_be_restored() { driver = new FirefoxDriver();
driver.get("http://www.example.com/login"); driver.findElement(By.id("username")).sendKeys("admin"); driver.findElement(By.id("password")).sendKeys("12345"); driver.findElement(By.id("login")).click();
Assert.assertTrue( driver.findElement(By.id("welcome")).isDisplayed());
//Before closing the browser, read the cookies Set allCookies = driver.manage().getCookies();
driver.close();
//open a new browser window driver = new FirefoxDriver();
//restore all cookies from previous session for(Cookie cookie : allCookies) { driver.manage().addCookie(cookie); }
driver.get("http://www.example.com/login"); //Login page should not be disaplyed Assert.assertTrue( driver.findElement(By.id("welcome")).isDisplayed()); driver.close(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.testingexcellence.webdriver import org.openqa.selenium.By; import org.openqa.selenium.Cookie; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import java.util.Set; public class CookieTest { WebDriver driver; @Test public void login_state_should_be_restored() { driver = new FirefoxDriver(); driver.get("http://www.example.com/login"); driver.findElement(By.id("username")).sendKeys("admin"); driver.findElement(By.id("password")).sendKeys("12345"); driver.findElement(By.id("login")).click(); Assert.assertTrue( driver.findElement(By.id("welcome")).isDisplayed()); //Before closing the browser, read the cookies Set allCookies = driver.manage().getCookies(); driver.close(); //open a new browser window driver = new FirefoxDriver(); //restore all cookies from previous session for(Cookie cookie : allCookies) { driver.manage().addCookie(cookie); } driver.get("http://www.example.com/login"); //Login page should not be disaplyed Assert.assertTrue( driver.findElement(By.id("welcome")).isDisplayed()); driver.close();
|