Sami Derin
Sami Derin

Reputation: 1

How can i same session id in my test when i close single open tab in Selenium?

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

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

Upvotes: -1

Views: 1122

Answers (1)

AbiSaran
AbiSaran

Reputation: 2678

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.

Upvotes: 0

Related Questions