Friday 22 July 2016

Session mechanism in selenium webdriver

Session mechanism in selenium web driver:
=================================

Why do we need Session Handling?
During test execution, the Selenium Web Driver has to interact with the browser all the time to execute given commands. At the time of execution, it is also possible that, before current execution completes, someone else starts execution of another script ,in the same machine and in the same type of browser.

In such situation, we need a mechanism by which our two different executions should not overlap with each other. This can be achieved using Session Handling in Selenium.

Example:
=========
package com.rs.blog;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;

public class SessionMechanism {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
SessionId session1 = ((RemoteWebDriver) driver).getSessionId();
System.out.println("FIRST SESSION ID: " + session1);
// System.out.println(s.toString());
WebDriver driver1 = new FirefoxDriver();
SessionId session2 = ((RemoteWebDriver) driver1).getSessionId();
System.out.println("SECOND SESSION ID: " + session2);
WebDriver driver2 = new FirefoxDriver();
SessionId session3 = ((RemoteWebDriver) driver2).getSessionId();
System.out.println("THIRD SESSION ID: " + session3);
}
}


No comments:

Post a Comment