问题
Summary of tools: watir-webdriver 1.8.17 Mac OS X 10.7.3 Chrome 18.0.1025.151
I'm currently using Watir WebDriver to automate Chrome sessions across a number of websites. I need to backup the state of the web browser (cookies, cache, etc.) at certain points throughout the session. Originally, I figured I could do this with Ruby's file IO library by copying ~/Library/Application Support/Google/Chrome/Default
at the necessary points. However, it does not appear that Chrome sessions created with Watir WebDriver store the needed information in this default location. How can I locate this data to back it up? Is this information stored elsewhere? Is there something other than Watir that would make this easier?
回答1:
I finally have a solution!
It appears that watir-webdriver stores the browser state/user data in random path. By default this can be found here (where XXXXXX is the random identifier):
/private/var/folders/2v/vkd2v3vs5njf69m59nqzc16m0000gn/T/.com.google.Chrome.XXXXXX/Default/
Instead of relying on this default and randomized path, you can specify a precise location for the user data using the following flag:
Watir::Browser.new :chrome, :switches => %w[--user-data-dir=/path/to/user/data]
Then the cache, cookies, etc. can be backed up, deleted, etc. using Ruby's standard library. Hopefully this helps someone else.
Edit: If you are unable to find where watir-webdriver is storing your user data by default, find Chrome's process id by running watir-webdriver and top
. Once you have the pid, type lsof -p <pid>
into terminal to find the path to the user data.
回答2:
Another thing I like to do is serialize(save) the Watir::Browser object into a file using YAML, like so:
require "yaml"
File.open("browserObj.yaml", 'w').write YAML::dump(@browser)
This browserObj.yaml file will then contain all sorts of internal details in easily readable/parseable text, including PID of whichever browser, path to temp profile, etc. Eg.
profile_dir: /tmp/webdriver-rb-profilecopy20121201-1981-9o9t9a
来源:https://stackoverflow.com/questions/10060493/how-to-backup-browser-state-after-watir-automation