Your position of Chief Electricity Officer is going to be redudant sooner or later. So be the one who makes it so.
I tend to program in Ruby regardless of its appropriateness to the situation. I do this because it reduces complexity, decreases opacity and, primarily, because its the language I know best.
So, here’s my crack at deleting files older than a certain date in a given directory, recursively, with Ruby:
older_than_time = Time.now - 604800 # = one week, in seconds
# Iterate through some folder's file paths
Dir["/path/to/logs/*.txt"].each do |file_path|
# Only delete the file if it exists
# Can delete the file on .ctime, .mtime or .atime
File.delete(file_path) if File.stat(file_path).atime < older_than_time
rescue => e
Hopefully that’s helpful for others.
I’m putting this out there for all of you, who like me, might embarrassingly take better part of a day figure out that when you want to access the text within a <textarea> box on a web page you need to look at its value like so:
browser.textarea(:id => "some_id").value
.text is reserved for the DOM selector innerText, which is for <div> and <span>, etc. And even though it doesn’t appear in the HTML, <textarea> does have a value property in the DOM.
I was able to confirm this on StackOverflow, but I haven’t seen anything specific to Ruby or Watir-Webdriver — hence my wasted hours.
For many reasons, trying to get a WatirWebdriver-driven Chrome browser working on a CentOS 6.x machine is difficult:
I’ll walk through how I came close to getting this to run. Maybe someone else can actually get this up and running sufficiently for their purposes.
chmod u+x install_chrome.sh
You can verify this works in your headless setup like so:
xvfb-run --server-args='-screen 0, 1024x768x16' google-chrome -start-maximized http://google.com > /some/path/null &
Install any of the current versions Google hosts for ChromeDriver and I get an error message something about how libstdc++.so.6 doesn’t have GLIBCXX_3.4.15. I surmise this means Google is blocking install because my version is too old, but I am not sure. But I know you can install ChromeDriver, as long as you install version 14 — something like 10 versions older than is current — as described here.
sudo chmod +x chromedriver
However, you’ll get an error here:
./chromedriver: error while loading shared libraries: libbz2.so.1.0: cannot open shared object file: No such file or directory
Many on StackOverflow and other threads have correctly ID’d the problem but their solutions don’t work for 32-bit CentOS. This worked for me:
sudo ln -s /lib/libbz2.so.1 /usr/lib/libbz2.so.1.0
Add where you installed ChromeDriver to your $PATH
Chrome works. ChromeDriver works. But now Selenium-Webdriver 2.35.1 fails. if I run:
headless = Headless.new
client = Selenium::WebDriver::Remote::Http::Default.new
client.timeout = 180 # seconds – default is 60. For some reason Chrome takes much longer to start than Firefox.
browser = Watir::Browser.new :chrome, :http_client => client
browser.close if browser
headless.destroy if headless
[...] .rvm/gems/ruby-2.0.0-p247/gems/selenium-webdriver-2.35.1/lib/selenium/webdriver/remote/response.rb:51:in `assert_ok': Unable to either launch or connect to Chrome. Please check that ChromeDriver is up-to-date. Using Chrome binary at: /opt/google/chrome/chrome (Selenium::WebDriver::Error::UnknownError)
Backtrace: [A giant stack of error codes ...]
Generally the internet recommended replacing Chrome with Chromium, which is described here. This was less fruitful, as I got stuck on a Net::ReadTimeout error that lead me nowhere fast:
.rvm/rubies/ruby-2.0.0-p247/lib/ruby/2.0.0/net/protocol.rb:158:in `rescue in rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
I assume I can downgrade my Chrome, Firefox, Ruby and Selenium Webdriver (and probably Watir and RSpec and a few dozen dependencies…) versions to make this work. But even if this is possible, it means potentially compromising a perfectly functional environment for the Firefox automation I’m already doing while not being able to test the most ideal versions of either browser.
So instead I’ll probably start my Chrome automation on my Windows machine instead (which I have to have to run IE automation). (Since Google’s decision to stop supporting CentOS seems arbitrary and capricious, it tickles me that in this tiny hyper-specific use-case, Google is contributing to Microsoft’s bottom line.)
Eventually I might dive in with another distro (Linux Mint, maybe?) to run a dedicated Chrome automation machine
Edit: Rather than just assume I am dead in the water, I have posted an edited version of this to Stack Overflow. Hopefully the wizards there can help.
I am surprised at the Times. This hurts consumers.
I read about the most amazing Ruby function: Array#Product:
… It turns out the Array::product produces the cartesian product
(1..13).to_a.product([:spades, :hearts, :diamonds, :clubs])
# produces a full card deck:
# => [[1, :spades], [1, :hearts], …, [2, :spades],…]