Ask a Question

How to clear browser cache in firefox and chrome

nwalimbe
Occasional Contributor

How to clear browser cache in firefox and chrome

Before I login to the application, I want the ability to clear the cache. This is because the passwords and urls etc are stored in cache and before I execute any tests, I need to ensure that the cache is cleared. I am writing in python script. Is it possible? If yes, how?

8 REPLIES 8
shankar_r
Community Hero

best thread for this would be https://community.smartbear.com/t5/TestComplete-Functional-Web/How-to-ClearCache-and-Cookies-from-In...


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

AlexKaras
Champion Level 3

Hi,

 

Besides the fantastic reply from @shankar_r (Shankar, thank you a lot for bringing this ages-old thread to the light!), there are recommended settings for web browsers (https://support.smartbear.com/testcomplete/docs/app-testing/web/general/preparing-browsers/index.htm...) and it is a good idea to have them been done on the test machines.

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================

@AlexKaras I am admiring wherever i see old threads there i see you in comments which insist me how much you are contributing for the peoples in the community to get away from automation headaches. Smiley Happy


Thanks
Shankar R

LinkedIn | CG-VAK Software | Bitbucket | shankarr.75@gmail.com

“You must expect great things from you, before you can do them”

Extension Available

nwalimbe
Occasional Contributor

Thanks a lot shankar_r. However this seems to be in a language I do not understand. Could you pls. convert it into python or give me some pointers. I tried googling it, but looks like I am making mistakes here and there.

 

Thanks,

nwalimbe
Occasional Contributor

This is the code, I am trying to use:

 

def ClearAllChromeStoredInfo():

objFSO = CreateObject("Scripting.FileSystemObject")
strAppDataFolder = aqEnvironment.GetEnvironmentVariable(("LocalAppData") & "\Google\Chrome\User Data\Default")
FilesInFolder = objFSO.GetFolder(strAppDataFolder).Files
Log.Message(FilesInFolder)
FoldersInFolder = objFSO.GetFolder(strAppDataFolder).SubFolders
for Folder in FoldersInFolder:
if UCase(Folder.Name) = "EXTENSIONS" and LCase(Folder.Name) = "EXTENSION STATE"
Folder.Delete True
break
For File In range (FilesInFolder)
If Not UCase(File.Name) = "PREFERENCES"
File.Delete
break

 

The error I am getting is attached.

Hi,

 

I am not a Python expert. And not sure what line has #4, but maybe the slashes must be doubled? I.e.: "\\Google\\Chrome\\User Data\\Default" ?

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
mike_duck
Visitor

Hi!

 

There is a workaround using Keys() method of browser Page object. Sorry, I'm not familiar with Python but in JScript it looks like following:

 

var browser = Sys.Find('ProcessName', 'chrome');

var page = browser.Page('http://yourpage.url/');

page.Keys("!^[Del]"); // Ctrl + Shift + Delete keys combination opens clear browser data dialog
SleepSeconds(2);

browser.Page('chrome://settings/clearBrowserData').Keys("[Enter]"); // hit Enter in clear browser data dialog
SleepSeconds(2);
browser.Page('chrome://settings/').Close(); // close settings page after clearing browsing data

page.Keys('[F5]');

ucuber
Occasional Contributor

Aside the fact, that the question is a little bit older, here a solution in python for chrome I made for my tests. If you have some ideas to make it more elegant or effective or if you see errors, feel free to refactor it

 

Remark: on my system all the chrome stuff is in Roaming, so I use AppData instead of LocalAppData

 

def clear_stored_info_chrome():
    excluded_items = [item.upper() for item in [
                          r"Preferences",
                          r"Origin Bound",
                          r"Extension"
                        ] 
                      ]
    found_items = [item.upper() for item in glob.glob(
                      os.path.join(
                        aqEnvironment.GetEnvironmentVariable("AppData"),
                        r"Google\Chrome\User Data\Default\*"
                      )
                    )
                  ]
    for item in found_items:
      is_excluded = [True for tag in excluded_items if tag in item]
      if not is_excluded:
        try:
          if os.path.isdir(item):
             shutil.rmtree(item, ignore_errors=True)
          else:
              os.remove(item)
        except BaseException as e: # I ignore errors of any kind, just a remark to Log
          Log.Message("clear chrome stored info: {} - {}".format(item, e))
 

 

cancel
Showing results for 
Search instead for 
Did you mean: