How can I clear chrome browser state in between every test?

sgonzalez :

I'd like to get a fresh instance of selenium chrome browser before every test that I run. I can achieve this by doing driver.quit() after every test and then initializing a new instance, but this is time consuming and resource intensive. Seems like there needs to be a better way, but I haven't been able to find anything online.

This is what I'm doing now:

@AfterMethod
public void shutDown(){
   seleniumDriver.quit();
}

@BeforeMethod
public void start(){
    seleniumDriver = SeleniumExtensions.createDefaultWebDriver();
}

Ultimately, instead of quitting and getting new instancem I would just be able to do something like this, where getFreshInstance clears the state (cookies, cache, session, etc):

 @AfterMethod
    public void cleanDriver(){
       seleniumDriver.getFreshInstance();
    }
mdrichardson - MSFT :

For cookies:

seleniumDriver.manage().deleteAllCookies();

Selenium doesn't offer a method of deleting cache, natively. Per this blog post, you could try this for clearing the cache:

WebElement clearDataBtn = driver.findElement(By.cssSelector("* /deep/ #clearBrowsingDataConfirm"));
if(sync.isElementPresent(By.cssSelector("* /deep/ #clearBrowsingDataConfirm"))){
System.out.println("Clear button found");
clearDataBtn.click();
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=152391&siteId=1