pagebot-action-tests.html 7.82 KB
Newer Older
Rafael Monnerat's avatar
Rafael Monnerat committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">

<!--
Copyright 2004 ThoughtWorks, Inc

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JsUnit Utility Tests</title>
<link rel="stylesheet" type="text/css" href="../../jsunit/css/jsUnitStyle.css">
<script language="JavaScript" type="text/javascript" src="../../jsunit/app/jsUnitCore.js"></script>
<script language="JavaScript" type="text/javascript" src="../jsmock/mock.js"></script>
<script language="JavaScript" type="text/javascript" src="../dummy-logging.js"></script>

<script language="JavaScript" type="text/javascript" src="../../core/scripts/htmlutils.js"></script>
<script language="JavaScript" type="text/javascript" src="../../core/scripts/selenium-browserdetect.js"></script>
<script language="JavaScript" type="text/javascript" src="../../core/scripts/selenium-browserbot.js"></script>
<script language="JavaScript" type="text/javascript">

function setUp() {
    initialiseSelect()
    initialiseTextbox()
    initialiseCheckbox()
    initialiseRadios()
    initialiseButtons()
    pageBot = BrowserBot.createForWindow(window);
    pageBot._windowClosed = function() {
    };
}

function attachEventListeners(element, eventSink) {
    element.onfocus = function() {
        eventSink.push('focus')
    }
    element.onselect = function() {
        eventSink.push('select')
    }
    element.onclick = function() {
        eventSink.push('click')
    }
    element.onchange = function() {
        eventSink.push('change')
    }
    element.onblur = function() {
        eventSink.push('blur')
    }
}

function initialiseSelect() {
    theSelect = document.getElementById("theSelect")
    theSelect.options[1].selected = true

    theSelectEvents = new Array()
    attachEventListeners(theSelect, theSelectEvents)

    assertEquals("option2", theSelect.value)
}

function initialiseTextbox() {
    theTextbox = document.getElementById("theTextbox")
    theTextbox.value = "theTextbox"

    textboxEvents = new Array()
    attachEventListeners(theTextbox, textboxEvents)
}

function initialiseCheckbox() {
    theCheckbox = document.getElementById("theCheckbox")
    theCheckbox.checked = false
    assertFalse(theCheckbox.checked)

    checkboxEvents = new Array()
    attachEventListeners(theCheckbox, checkboxEvents)
}

function initialiseRadios() {
    theRadio1 = document.getElementById("theRadio1")
    theRadio1.checked = true
    assertTrue(theRadio1.checked)
    radio1Events = new Array()
    attachEventListeners(theRadio1, radio1Events)

    theRadio2 = document.getElementById("theRadio2")
    theRadio2.checked = false
    assertFalse(theRadio2.checked)
    radio2Events = new Array()
    attachEventListeners(theRadio2, radio2Events)
}

function initialiseButtons() {
    theButton = document.getElementById("theButton")
    buttonEvents = new Array()
    attachEventListeners(theButton, buttonEvents)

    theSubmit = document.getElementById("theSubmit")
    submitEvents = new Array()
    attachEventListeners(theSubmit, submitEvents)
}

function testSelectOptionShouldSelectAndTriggerFocusChange() {
    pageBot.selectOption(theSelect, theSelect.options[2])

    assertEquals("option3", theSelect.value)
    assertEquals("focus,change", theSelectEvents.join());
}

function testSelectAlreadySelectedOptionWithLabelShouldNotTriggerChange() {
    pageBot.selectOption(theSelect, theSelect.options[1])

    assertEquals("option2", theSelect.value)
    assertEquals("focus", theSelectEvents.join())
}

function testReplaceTextInTextBoxShouldTriggerFocusSelectChange() {
    pageBot.replaceText(theTextbox, "new text")
    assertEquals("new text", theTextbox.value)

    assertEquals("focus,select,change", textboxEvents.join())
}

function TODO_testReplaceTextShouldNotTriggerSelectIfOriginallyEmpty() {
    theTextbox.value = ""

    pageBot.replaceText(theTextbox, "new text")

    assertEquals("new text", theTextbox.value)
    assertEquals("focus,change", textboxEvents.join());
}

function testClickCheckboxShouldTriggerFocusClickChange() {
    pageBot.clickElement(theCheckbox)

    assertTrue("Checkbox should be checked", theCheckbox.checked)

    // Emulate actual event order (varies per browser for manual click)
    if (browserVersion.isSafari)
    {
        assertEquals("focus,change,click", checkboxEvents.join())
    } else if (browserVersion.isOpera) {
        assertEquals("focus,click", checkboxEvents.join())
    } else
    {
        assertEquals("focus,click,change", checkboxEvents.join())
    }
}

function testClickCheckboxShouldUncheckIfAlreadyChecked() {
    theCheckbox.checked = true
    pageBot.clickElement(theCheckbox)
    assertFalse("Checkbox shouldn't be checked", theCheckbox.checked)
}

function testClickRadioShouldTriggerFocusClickChange() {
    pageBot.clickElement(theRadio2)
    assertFalse("Radio1 shouldn't be checked", theRadio1.checked)
    assertTrue("Radio should be checked", theRadio2.checked)

    assertEquals("", radio1Events.join());

    // Safari-real doesn't support change events on radio
    if (browserVersion.isSafari)
    {
        assertEquals("focus,click", radio2Events.join());
    } else if (browserVersion.isOpera) {
        assertEquals("focus,click", radio2Events.join())
    } else
    {
        assertEquals("focus,click,change", radio2Events.join())
    }
}

function testClickRadioShouldNotTriggerChangeIfAlreadySelected() {
    assertTrue(theRadio1.checked)
    pageBot.clickElement(theRadio1)
    assertTrue("Radio1 should be checked", theRadio1.checked)

    assertEquals("focus,click", radio1Events.join())
    assertEquals("", radio2Events.join())
}

function testClickButtonShouldTriggerFocusClick() {
    pageBot.clickElement(theButton)

    assertEquals("focus,click", buttonEvents.join())
}

function testClickSubmitShouldTriggerFocusClick() {
    pageBot.clickElement(theSubmit)
    assertEquals("focus,click", submitEvents.join())
}

function testClickShouldNotTriggerEventsPassedClickIfWindowIsClosed() {
    theButton.onclick = function() {
        buttonEvents.push('clickNclose');
        pageBot._windowClosed = function() {
            return true
        };
    }

    pageBot.clickElement(theButton);
    assertEquals("focus,clickNclose", buttonEvents.join());
}
</script>
</head>

<body>
<h1>Selenium Browserbot Tests</h1>

<form id="theForm" name="theForm" onsubmit="return false;">
    <input name="theTextbox" id="theTextbox" type="text" value="theTextbox"/>
    <input name="theCheckbox" id="theCheckbox" type="checkbox"/>
    <input name="theRadio" id="theRadio1" type="radio" value="radio1"/><input name="theRadio" id="theRadio2"
                                                                              type="radio" value="radio2"/>
    <select name="theSelect" id="theSelect">
        <option value="option1">First Option</option>
        <option value="option2" selected="true">Second Option</option>
        <option value="option3">Third Option</option>
    </select>
    <input name="theButton" id="theButton" type="button" value="The Button"/>
    <input name="theSubmit" id="theSubmit" type="submit" value="The Submit"/>
</form>
</body>
</html>