2

I am automating web browsers to test my requirements (autofill/complete, persistence related especially). Clearing cookies is different than clearing browser history, right? Is automating to clear browser history in headless mode possible? My assumption after so much surfing online is that it's not. (I read an article that said clearing browsing history by automation in headless mode isn't allowed due to security reasons).

Also, the automated browser version which starts via selenium web drivers to test the cases doesn't store any cache as soon as it logs out of an account. Suppose I log in and log out of my account. I want selenium to keep the username I entered earlier as autofill when I refresh the page and type a few starting letters of it( which happens in normal browsers but not in the automated browser version). Can this be achieved? Thanks in advance

I've already managed to clear the browsing history of chrome in UI mode by using shadow DOM elements. This method works perfectly in user-interface mode(i.e. without headless mode) but my problem is to achieve it in a headless mode of testing( and would be great if suggested way works for other browsers too). Any suggestions or workaround for this problem?

public void clearBrowsingHistory() throws Exception {

    WebDriverWait wait = new WebDriverWait(driver, 15);
    driver.get("chrome://settings/clearBrowserData");
// Get shadow root elements
    WebElement shadowRoot1 = expandRootElement(driver.findElement(By.xpath("/html/body/settings-ui")));

    WebElement root2 = shadowRoot1.findElement(By.cssSelector("settings-main"));
    WebElement shadowRoot2 = expandRootElement(root2);

    WebElement root3 = shadowRoot2.findElement(By.cssSelector("settings-basic-page"));
    WebElement shadowRoot3 = expandRootElement(root3);

    WebElement root4 = shadowRoot3
        .findElement(By.cssSelector("#advancedPage > settings-section > settings-privacy-page"));
    WebElement shadowRoot4 = expandRootElement(root4);

    WebElement root5 = shadowRoot4.findElement(By.cssSelector("settings-clear-browsing-data-dialog"));
    WebElement shadowRoot5 = expandRootElement(root5);

    WebElement root6 = shadowRoot5
        .findElement(By.cssSelector("cr-dialog div[slot ='button-container'] #clearBrowsingDataConfirm"));

    root6.click();
    wait.until(ExpectedConditions.invisibilityOf(root6));
}
Pushkar
  • 100
  • 8
  • Maybe [this](https://sqa.stackexchange.com/questions/28037/how-to-clear-all-history-included-all-sessions-cache-etc-in-ie-with-selenium) answer provides some help – Cray Jun 24 '19 at 11:49
  • @Cray the answer is regarding a clean start as I understood. My query is to clear the browser history after the completing some operations – Pushkar Jun 24 '19 at 12:27
  • @DebanjanB my question is **different** from what you've marked it duplicate for. I've edited it to make it more clear, please see it. – Pushkar Jun 25 '19 at 07:26
  • @PushkarKumarKhatri `Is automating to clear browser history possible` you need to reach till the `Clear` button first. – undetected Selenium Jun 25 '19 at 07:29
  • @DebanjanB this clearing browser history method won't work if you start your new chrome driver session adding this line **options.addArguments("--headless");. Because in headless mode there is no user interface**. You can test that. Did you understand my problem? The code snippet I have shown here works correctly and clears the browser history when the driver session is opened without headless mode – Pushkar Jun 25 '19 at 09:20
  • here is the [link](https://stackoverflow.com/questions/2190808/how-to-clear-browsers-ie-firefox-opera-chrome-history-using-javascript-or-j) available on stack-overflow. – Pushkar Jun 25 '19 at 09:38

0 Answers0