Arun
Arun

Reputation: 21

In Selenium, How can I open Firefox browser but with the existing cookie

I am trying to create a selenium test which is having below step:

  1. Login to google page.
  2. Successful login by entering valid credentials
  3. Page will shows inbox here
  4. Close the browser by directing clicking right corner "Close" button on browser.
  5. Repeat step 1

In this test case I am expecting that after 5th step google page do not ask for credentials again and moves to inbox page directly. How can do this using selenium webdriver?

Upvotes: 2

Views: 7650

Answers (3)

Stan
Stan

Reputation: 3461

Why are you closing Selenium session by clicking on a "Close" browser button? It's really not a way how it works. Every selenium framework has it's own implementation of session end, it's not necessary to invent something new.

For you purposes you have a lot of ways to do

  1. Save cookies in some global variable/object after login and add them on your next session start

  2. Implement constructor method (depending on your language and framework, it could be something like setUp() in php or beforeEach() in javascript) when you have a logic of logging in your application.

Why that is happening? When you are closing browser, Selenium is starting a fully new session without anything available from the previous one, this is done for "clear" testing results.

Upvotes: 1

aholt
aholt

Reputation: 2971

This should give you what you need. Copy the cookies from the first driver instance into the new driver instance using driver.manage().getCookies();

FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
//Passing valid credentials
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
driver.findElement(By.xpath("//*[@id='signIn']")).click();
Thread.sleep(20000);
Set<Cookie> cookies = driver.manage().getCookies();
driver.close();
//Starting new browser 
driver = new FirefoxDriver();
for(Cookie cookie : cookies)
{
    driver.manage().addCookie(cookie);
}
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
Thread.sleep(20000);
driver.quit();

Upvotes: 3

Arun
Arun

Reputation: 21

I am new in selenium. May be I might be incorrect in steps. Please correct me if I am wrong.

Objective behind this testcase is, if user closes the website where user logged in successfully, and then closes the browser. Now, when lunching a new browser for the same site, then login page should not display and redirect to inbox page.

Below is the sample code :

FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
//Passing valid credentials
driver.findElement(By.xpath("//*[@id='Email']")).sendKeys("[email protected]");
driver.findElement(By.xpath("//*[@id='Passwd']")).sendKeys("password");
driver.findElement(By.xpath("//*[@id='signIn']")).click();
Thread.sleep(20000);
driver.close();
//Starting new browser 
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://mail.google.com/");
Thread.sleep(20000);
driver.quit();

Upvotes: 0

Related Questions