= Browser-based Testing of Massive Ajax-using Rails Applications with Selenium by Till Vollmer www.mindmeister.com == What can go wrong? * Normal tests do not cover what happens in browser * Web 2.0 means JavaScript, CSS * JavaScript, DOM, CSS opertionas differ on various browsers == Selenium Framework (in general) === Selenium Core * Heart of Selenium * All JavaScript (browser framework) * Extensible * Executes commands in browser * A frame triggers JavaScript events in the browser * If used must be on the same server as the application (XSS, same origin policy), or use Selenium RC === Selenium IDE * Firefox plugin to record scenarious * File format customizable * Standard RC-based * Drawbacks: Some actions not recorded * Ajax based actions === Selenium RC (Remote Control) * A server (written in Java) that remote controls the server * Good for continous integration tests * Works perfectly with Selenium IDE * Proxy (same origin policy) ==== Example class GoogleTest < TestCase def setup @sel = Selenium::SeleniumDriver.new("localhost", 4444, "*firefox", "http://google.com", 15000) @sel.start end def test_google @sel.open("http://...") @sel.type("q", "helleo world") ... # Assertions end end === Selenium Ruby on Rails plugin * rake task to test * Direct URL (but will destroy DB) * Drawback: rsel tests are not 100% compatible with Selenium IDE (but could be easily adopted) * The naming is some times a bit weird * You can define your own export format in the IDE to fix that == Selenium Commands Syntax * Syntax: Command, Target, Value - ... AndWait (e.g. clickAndWait) * Command issies a serber response ... === Locators You often need to identify and address an element: * ID: click("test") * Name: click("name=test") * Javascript: ... * CSS: ... === Commands Examples * click * keyPress * doubleClick * waitForCondition * waitForVisible * verifyElementNotPresent * verifyElementPresent * assertElementPresent === Simple example: Mindmaster homepage # setup setup :keep_session test.setup fixtures => :all # open root page and login open "/" type "username", "xxx" ... click_and_wait "//button[@type='submit']" click_and_wait "link=Start" # set map view to list click_and_waut "link=List" assert_element_not_present ... # set map view to thumbnails ... == Testing Ajax things * Problem: Wait for something (response) click(loc1) # Ajax? waitForVisible(loc2) waitForElementPresent(loc3) === Helper Good idea to define some helpers for advanced Ajax testing def wait_for_ajax wait_for_condition(selenium.browserbot.getCurrentWindow().ServerConnection.isPending() == false) end == Q&A ?: How are tests recorded? !: By hand. The Selenium IDE is not very good with Ajax, because it just "fakes" Javascript events ?: How to test multiple connections? !: Not clever to allow that, since you have to serialize