Selenium - Stored Session Data

后端 未结 1 1300
执念已碎
执念已碎 2021-01-14 17:00

I have search throughout the internet but none of the answers I found have a clear solution.

I am using selenium webdriver with Java.

My test needs to verify

相关标签:
1条回答
  • 2021-01-14 17:44

    This solution works for chrome .

    When you launch chrome (I think same applies for other browsers ) selenium creates a temporary profile and when you close it it deletes it. So when you relaunch the browser the new profile will not have the session information. What you can do is use profiles.

     DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
     ChromeOptions options = new ChromeOptions();
    
     options.addArguments("user-data-dir=C:\\Users\\"+System.getenv("USERNAME")+"\\AppData\\Local\\Google\\Chrome\\User Data\\Profile 1");
                desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
     driver = new ChromeDriver(desiredCapabilities);
    

    This way if you create your driver, every time chrome will be launched with the same profile and if you dont do

    driver.manage().deleteAllCookies();
    

    Your session information will persist in the new session of chrome driver. If you dont want it to persist you can clean the cookies using the above command or just simple log out. This should solve your problem. Similar things should also be possible in other browsers.

    To know which profile is currently used type chrome://version/ in your address bar it has the information of the current profile being usesd. To know more about see this

    0 讨论(0)
提交回复
热议问题