2

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?

Skandix
  • 1,916
  • 6
  • 27
  • 36
Arun
  • 21
  • 1
  • 3
  • What is the code that you have tried to do this? – mjuarez Apr 05 '15 at 08:19
  • Possible duplicate of [How can I test a "Remember Me" checkbox feature in Selenium](https://stackoverflow.com/questions/29338944/how-can-i-test-a-remember-me-checkbox-feature-in-selenium) – stiemannkj1 Oct 26 '17 at 15:11
  • @stiemannkj1 He ask about Firefox, not Chrome. – dns Jun 12 '20 at 07:10
  • @dns good point. My answer in that question is cross-browser compatible, but I forgot that both these questions are targeting different browsers. – stiemannkj1 Jun 12 '20 at 15:02

3 Answers3

3

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("testuser@gmail.com");
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();
aholt
  • 2,829
  • 2
  • 10
  • 13
  • If using Java 8, you can use a lamba instead of the for loop. – aholt Apr 06 '15 at 18:55
  • This while concept of having to get https://mail.google.com, add the cookies and then get https://mail.google.com again seems quite odd. It's certainly not how a browser works normally. It would be better for selenium to allow the user to specify a domain without loading it, add cookies and then load the page. – conteh Oct 12 '17 at 20:54
1

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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
Stan E
  • 3,396
  • 20
  • 31
0

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("testuser@gmail.com");
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();

Arun
  • 21
  • 1
  • 3
  • Possible solutions: 1) not to close browser 2) keep cookies or session variables in IDE variables and put in them for next test. No any other ways. – Stan E Apr 05 '15 at 14:16