Handling cookies

Let's say you are automating the demo application. There could be many scenarios you want to automate, such as searching for products, adding products to the shopping cart, checkout, returns, and so on. For all these actions, one common thing is to have to log into the demo application in each of the test cases. So, logging into the application in every test case of yours will increase the overall test execution time significantly. To reduce the execution time of your test cases, you can actually skip signing in for every test case. This can be done by signing in once and writing all the cookies of that domain into a file. From the next login onward, you can actually load the cookies from the file and add them to the driver.

To fetch all the cookies that are loaded for a web page, WebDriver provides the following method:

driver.manage().getCookies()

This will return all the cookies that the web page stores in the current session. Each cookie is associated with a name, value, domain, path, expiry, and the status of whether it is secure or not. The server to validate a client cookie parses all of these values. Now, we will store all of this information for each cookie in a file so that our individual test cases read from this file and load that information into the driver. Hence, you can skip the login, because once your driver session has this information in it, the application server treats your browser session as authenticated and directly takes you to your requested URL. The following is a quick code to store the cookie information:

public class StoreCookieInfo {
WebDriver driver;

@BeforeMethod
public void setup() throws IOException {
System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
driver = new ChromeDriver();
driver.get("http://demo-store.seleniumacademy.com/customer/account/login/");
}

@Test
public void storeCookies() {
driver.findElement(By.id("email")).sendKeys("[email protected]");
driver.findElement(By.id("pass")).sendKeys("tester");
driver.findElement(By.id("send2")).submit();

File dataFile = new File("./target/browser.data");
try {
dataFile.delete();
dataFile.createNewFile();
FileWriter fos = new FileWriter(dataFile);
BufferedWriter bos = new BufferedWriter(fos);
for (Cookie ck : driver.manage().getCookies()) {
bos.write((ck.getName() + ";" + ck.getValue() + ";" + ck.
getDomain()
+ ";" + ck.getPath() + ";" + ck.getExpiry() + ";" + ck.
isSecure()));
bos.newLine();
}

bos.flush();
bos.close();
fos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}

@AfterMethod
public void tearDown() {
driver.quit();
}
}

From now on, for every test case or a set of test cases, load the cookie information from the browser.data file, and add it to the driver using the following method:

driver.manage().addCookie(ck);

After you add this information to your browser session and go to the dashboard page, it will automatically redirect you to the home page, without asking for a login, thus avoiding a login every time, for every test case. The code that adds all of the previous cookies to the driver is as follows:

public class LoadCookieInfo {
WebDriver driver;

@BeforeMethod
public void setup() throws IOException {
System.setProperty("webdriver.chrome.driver",
"./src/test/resources/drivers/chromedriver");
driver = new ChromeDriver();
driver.get("http://demo-store.seleniumacademy.com");
}

@Test
public void loadCookies() {
try {
File dataFile = new File("./target/browser.data");
FileReader fr = new FileReader(dataFile);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
StringTokenizer str = new StringTokenizer(line, ";");
while (str.hasMoreTokens()) {
String name = str.nextToken();
String value = str.nextToken();
String domain = str.nextToken();
String path = str.nextToken();
Date expiry = null;
String dt;
if (!(dt = str.nextToken()).equals("null")) {
SimpleDateFormat formatter =
new SimpleDateFormat("E MMM d HH:mm:ss z yyyy");
expiry = formatter.parse(dt);
}

boolean isSecure = new Boolean(str.nextToken()).
booleanValue();
Cookie ck = new Cookie(name, value, domain, path, expiry, isSecure);
driver.manage().addCookie(ck)
;
}
}

driver.get("http://demo-store.seleniumacademy.com/customer/account/index/");
assertThat(driver.findElement(By.cssSelector("div.page-title")).getText())
.isEqualTo("MY DASHBOARD");

} catch (Exception ex) {
ex.printStackTrace();
}
}

@AfterMethod
public void tearDown() {
driver.quit();
}
}

Hence, we can be directly taken to the home page without logging in again and again. As you can see, after creating the driver instance, we have the following line:

driver.get("http://demo-store.seleniumacademy.com");

Ideally, this line should be visible after we have set the cookies to the driver. But the reason it is at the top is that the WebDriver doesn't allow you to set the cookies directly to this session, because it treats those cookies as if they were from a different domain. Try removing the previous line of code and execute it, and you will see the error. So, initially, you will try to visit the home page to set the domain value of the driver to the application server domain and load all the cookies. When you execute this code, initially, you will see the home page of the application.

Hence, you can avoid entering the username and the password on the server, validating them again and again for each test, and thereby save a lot of time, by using the WebDriver's cookies feature.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.104.238