0

I want run one test script. The scenario is like that;

  • Go to login page
  • Login with valid credentials
  • Close current single tab
  • Go to the login page again
  • Verify whether browser will go homepage instead of login page or not.

But i don't know how can i do that. Because if i use close or quit method, session id will be killed. even if i open new browser for another step, it will not be same with real scenario. Because as manually, if i try to go to login page after closing single open homepage without logging out, i can go to home page directly. I am not be able to automate it.

I used some Actions method to close and also open new tab. And i used these;

driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL+”t”);

driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + 'w');

I am using Selenium+Java+Cucumber+Chrome+Mac

  • From Selenium 4+, there is a `newWindow` method that let you open said window as a tab or a separate window. Refer to this answer: https://stackoverflow.com/a/66640852/10621296 – Jetto Martínez Oct 28 '22 at 15:15
  • I know but i am not using selenium 4+ in my framework and i am not able to change it. That's why i should find different solution. – Sami Derin Oct 28 '22 at 22:12
  • Then please add what version of Selenium you are using. Since Selenium 4 and newer releases already have solutions for this, knowing which version of Selenium you have can help others better understand your problem. – Jetto Martínez Oct 28 '22 at 22:19
  • My Selenium version is 3.141.59. And also i am using Singleton and POM design patterns. – Sami Derin Oct 29 '22 at 04:23

1 Answers1

0

You can use profile to achieve this scenario. I 've just given some sample code:

    WebDriverManager.chromedriver().setup();
    
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--user-data-dir=C:\\Temp\\");
    options.addArguments("--profile-directory=ChromeData");
    
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
    driver.get("https://www.flipkart.com/");
    driver.findElement(By.xpath("//input[@class='_2IX_2- VJZDxU']")).sendKeys("<username>");
    driver.findElement(By.xpath("//input[@class='_2IX_2- _3mctLh VJZDxU']")).sendKeys("<password>");
    Thread.sleep(1000);
    driver.findElement(By.xpath("//button[@class='_2KpZ6l _2HKlqd _3AWRsL']")).click();
    Thread.sleep(3000);
    driver.close();
    Thread.sleep(1000);
    
    driver = new ChromeDriver(options);
    driver.manage().window().maximize();
    driver.get("https://www.flipkart.com/");

The above code will create a new chrome profile in the directory - "C:\Temp\ChromeData", then open the browser, launch the URL, login, close the window, again open a chrome browser, launch the same URL, but this time, the user already logged in.

AbiSaran
  • 2,538
  • 3
  • 9
  • 15