Skip to content

Instantly share code, notes, and snippets.

@Archenoth
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Archenoth/9858e98cacee396d0d97 to your computer and use it in GitHub Desktop.
Save Archenoth/9858e98cacee396d0d97 to your computer and use it in GitHub Desktop.
How to use Watir in Emacs Org mode

Watir Org-mode

This is how you can use Watir inside of org-mode..! First off, you need to have Ruby blocks working.

Second, you need to have both the watir and watir-webdriver RubyGems installed. You can get that by evaluating the following block (By default you can do that with C-c C-c) after installing RubyGems:

gem install watir watir-webdriver

After that, evaluate the following code block to begin:

require "watir-webdriver"
browser = Watir::Browser.new

Testing

Now that you have a session in Org, you can create as many Ruby blocks with the variable browser as you like, and the result will evaluate in the running browser. This is because they are all in the same Org Babel session. (You can see that defined for all Ruby Blocks here.)

For example, the following will navigate your browser to Google and search “Mawile”:

browser.goto "http://google.ca"
browser.text_field(:name => 'q').set "Mawile"

Another cool thing about having a session like this is that you can play around with it manually by visiting the <a href=”elisp:(switch-to-buffer “@@html:@@watir@@html:@@”)”>”watir” buffer.

Dependencies

If you want to make sure you always run a specific block before another, you can make a “named block” and include it with :var.

For example… If I wanted to make sure that I have already logged in for some action, I could create two blocks, one for logging into something, and one for logging out as follows:

browser.goto "http://www.example.com"

browser.text_field(:type => "username").set "Something"
browser.text_field(:type => "password").set "Something else"
browser.button(:type => "submit").click
browser.goto "http://www.example.com/logout"

With blocks like this, I can now depend on one or the other for actions like so:

# Things while logged in

Or:

# Things while logged out

You can make chains however you like.

Ending the session

After you are finished experimenting… You can end the Watir session by evaluating the following code block.

browser.close if browser

Metadata

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment