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
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.
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.
After you are finished experimenting… You can end the Watir session by evaluating the following code block.
browser.close if browser