Remove Non Printable Characters From String Using Ruby Regex

non printable characters
non printable characters

I Recently was working on building some Selenium tests and needed to compare a string after being saved to a database and returned to the user interface. Because of the interface plugin that does some syntax highlighting, the string was being displayed with some non printable characters. Because the non printable characters were not breaking the app, I decided that it would be OK for me to remove them to compare the strings in my selenium test.

To remove the non printable characters or only return the printable characters I used the following ruby:

printableString = stringFromInterface.scan(/[[:print:]]/).join

There is a great list of other character classes that can be used in your regular expressions found at http://doc.infosnel.nl/ruby_regular_expressions.html

Character classes

Class Switch Match description
[0-9] \d Decimal digit character
[^0-9] \D Not a decimal digit character
[\s\t\r\n\f] \s Whitespace character
[^\s\t\r\n\f] \S Not a whitespace character
[A-Za-z0-9_] \w Word character (alpha, numeric, and underscore)
[^A-Za-z0-9_] \W Not a word character
[:alnum:] Alpha numeric ([A-Za-z0-9])
[:alpha:] Uppercase and lowercase letters ([A-Za-z])
[:blank:] Blank or tab character
[:space:] Whitespace characters
[:digit:] Decimal digit characters
[:lower:] Lowercase letters ([a-z])
[:upper:] Uppercase characters
[:print:] Any printable character, including space
[:graph:] Printable characters excluding space
[:punct:] Punctuation characters: any printable character excluding aplhanumeric or space
[:cntrl] Chontrol characters (0x00 to 0x1F and 0x7F)
[:xdigit:] Hexadecimal digits ([0-9a-fA-F])

Taking Screenshots In Selenium Using Ruby

I LOVE being able to take screenshots of my Selenium tests as they are running!

Here’s how I take screenshots of my selenium tests running, written in Ruby:

  • Find the variable referencing the selenium driver. Might look something like this:
    @selenium_driver = Selenium::Client::Driver.new(.....
  • Wait for condition to take screenshot. Example:
    page.wait_for_element("//h1", {:timeout_in_seconds => 10})
  • Take screenshot. Example:
    @selenium_driver.capture_entire_page_screenshot(File.expand_path(File.dirname(__FILE__)) + 'screenshot1.png', '')

    or something simpler like:

    @selenium_driver.capture_entire_page_screenshot('~/Desktop/screenshot1.png', '')

Selenium documentation for using the capture screenshot function.

Escaping Ruby Strings For Eval In Selenium Tests

If you are setting up a string in Ruby to then be used in an eval statement in a Selenium test you’ll need some escaping fu to be able to pull it off.

Quotes in strings that are then going to be put in other Ruby strings need to be tripple escaped. If the string is going directly to the eval in Selenium then only a single escape is needed.

first = "first quote: \\\"......"
second = "#{first} second quote: \\\".........."
jsfunction = "window.alert(\"#{second}\")"
puts jsfunction

it "should alert string" do
  page.js_eval(jsfunction)
end

selenuim ruby string escaping for eval

Install 64 bit Ruby On Rails and MySQL on OS X

because it's fast!

For a project that I am starting I wanted to use Rails and MySQL so I decided to install for source that I compile myself for both. It was amazingly easy and satisfying to have both compiled and installed on my machine for development. Hivelogic has some really simple easy to follow blog posts on how to do both. If you are wanting to compile and install either Rails or MySQL on your Mac from source code then I recommend you check them out.

Installing MySQL on Snow Leopard

Installing Ruby, RubyGems, and Rails on Snow Leopard

Ruby On Rails

Bike photo by piston9 / CC BY 2.0