When working with pages in several automating projects I noticed that the fields like login ID, search fields, etc., are getting saved on the page. Thus, when my Selenium test runs, I need to put additional code to clear those fields if those are populated. I needed a way to clear the browsing data through my test before I went to the page.
I attempted a few solutions from here and here. However, these worked on older versions of the Chrome browser. Next, I tried out the solution from here. It worked in part but I needed to also set the time range as AllTime.
A longer version of the final completed code is here. It basically traverses through the DOM which is tricky because of the nested shadow roots. This version is easy to understand but it is long and it’s a bit flaky as well.
I optimized the code and came up with a shorter version as below:
In the above code, we bring up the
ClearBrowserData modal and then start getting the shadow root elements. Our Clear Data button is nested in the shadow root structure so a normal XPath will not be able to find it. I have also added a call to set the
TimeRange as well. The extension methods written for
IWebDriver used above are as below:
The following function sets the Time range to AllTime. We can change the
SelectByValue call to select other values in the list.
I put the
ClearBrowserCache method in a
BrowserHelper.cs method in my framework. These methods work with Chrome 90.0.4430.85 (Official Build) (64-bit).
Call the above
ClearBrowserCache method in your setup method as below: