I can see two reasons to use automation: 1) because you’re
lazy a good developer and don’t want to do a task by hand twice, and 2) because you prefer turning a boring task into a neat scripting challenge :) Well, next time you do anything related to a web interface, think about selenium and ruby!
Let’s not wait longer, you will need the
selenium-webdriver gem. I personally use RVM, and therefore some commands will be slightly different. If you don’t use it yet, well you should probably, it’s great!
And let’s start by opening an interactive ruby shell and play somewhat with the selenium API (some output lines are stripped for clarity).
1 2 3
By this time, firefox should be opening and soon ready. If you don’t have firefox installed on your machine, now is the time ;) So let’s open the google homepage.
Make a search, for “aspyct” for example. Of course you will first need to locate the input. You’re probably familiar with firebug or equivalent, so find the
id of that input. If not, simply right click the input and “inspect element”.
So, for me, the
gbqfq. Now grab this element from our script, and send some text to it. And by the way, click on the search button.
1 2 3 4
Now that you’ve found such a wonderful website (that is, aspyct.org), for sure you want to visit it! Click the link named “Aspyct.org”:
And you probably want to make sure that you landed on the correct website. You could check the page title, or browse the DOM with XPath.
1 2 3 4
Now you know what to do next time you have to fill the fields with the data from an CSV file ;) But most importantly, you can automate your functional tests and develop safer and faster. Have fun!
See also: Selenium WebDriver documentation.