|
| 1 | +<!DOCTYPE html> |
| 2 | +<title>interactions_test</title> |
| 3 | +<script src="../../test_bootstrap.js"></script> |
| 4 | +<script> |
| 5 | +goog.require('bot.Keyboard'); |
| 6 | +goog.require('bot.Mouse'); |
| 7 | +goog.require('bot.color'); |
| 8 | +goog.require('bot.dom'); |
| 9 | +goog.require('bot.inject.cache'); |
| 10 | +goog.require('bot.json'); |
| 11 | +goog.require('bot.response'); |
| 12 | +goog.require('goog.array'); |
| 13 | +goog.require('goog.testing.jsunit'); |
| 14 | +goog.require('webdriver.Key'); |
| 15 | +goog.require('webdriver.atoms.inject.action'); |
| 16 | +</script> |
| 17 | +<style> |
| 18 | +#click-box { |
| 19 | + position: absolute; |
| 20 | + width: 75px; |
| 21 | + height: 75px; |
| 22 | + top: 50px; |
| 23 | + left: 50px; |
| 24 | +} |
| 25 | +</style> |
| 26 | +<body> |
| 27 | +<input id="focus-sync"> |
| 28 | +<input id="text-input"> |
| 29 | +<div id="click-box"></div> |
| 30 | +<script> |
| 31 | +var clickBox = document.getElementById('click-box'); |
| 32 | +clickBox.style.background = 'red'; |
| 33 | +clickBox.onclick = function(e) { |
| 34 | + e = e || window.event; |
| 35 | + clickBox.style.background = 'blue'; |
| 36 | + if (e.clientX > 100) { |
| 37 | + clickBox.style.background = 'cyan'; |
| 38 | + } |
| 39 | + if (e.clientY > 100) { |
| 40 | + clickBox.style.background = 'yellow'; |
| 41 | + } |
| 42 | +}; |
| 43 | +clickBox.oncontextmenu = function() { |
| 44 | + clickBox.style.background = 'green'; |
| 45 | + return false; |
| 46 | +}; |
| 47 | + |
| 48 | + |
| 49 | +function resetFocus() { |
| 50 | + var sink = document.getElementById('focus-sync'); |
| 51 | + focusElement(sink); |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +function focusElement(el) { |
| 56 | + el.focus(); |
| 57 | + assertEquals(el, bot.dom.getActiveElement(document)); |
| 58 | +} |
| 59 | + |
| 60 | + |
| 61 | +function testSendKeysToActiveElement() { |
| 62 | + resetFocus(); |
| 63 | + |
| 64 | + var input = document.getElementById('text-input'); |
| 65 | + focusElement(input); |
| 66 | + |
| 67 | + var state = doType('foo'); |
| 68 | + assertEquals('foo', input.value); |
| 69 | + |
| 70 | + resetFocus(); |
| 71 | + |
| 72 | + state['pressed'].push(bot.Keyboard.Keys.SHIFT); |
| 73 | + state = doType('more', state); |
| 74 | + assertEquals('foo', input.value); |
| 75 | + |
| 76 | + focusElement(input); |
| 77 | + doType(['bar', webdriver.Key.SHIFT, 'baz'], state); |
| 78 | + assertEquals('fooBARbaz', input.value); |
| 79 | + |
| 80 | + function doType(keys, opt_state) { |
| 81 | + var res = webdriver.atoms.inject.action.sendKeysToActiveElement( |
| 82 | + keys, opt_state); |
| 83 | + res = bot.json.parse(res); |
| 84 | + bot.response.checkResponse(res); |
| 85 | + return res['value']; |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +function testMouseClick() { |
| 90 | + assertEquals('initial state is wrong', 'rgba(255, 0, 0, 1)', getColor()); |
| 91 | + |
| 92 | + var state = doClick(bot.Mouse.Button.LEFT, { |
| 93 | + clientXY: {x: 60, y: 60} // Click over the clickBox. |
| 94 | + }); |
| 95 | + assertEquals('rgba(0, 0, 255, 1)', getColor()); |
| 96 | + assertTrue(!!state['element']); |
| 97 | + assertEquals( |
| 98 | + clickBox, bot.inject.cache.getElement(state['element']['ELEMENT'])); |
| 99 | + |
| 100 | + state = doClick(bot.Mouse.Button.RIGHT, state); |
| 101 | + assertEquals('rgba(0, 128, 0, 1)', getColor()); |
| 102 | + |
| 103 | + state.clientXY.x += 41; |
| 104 | + assertEquals(101, state.clientXY.x); |
| 105 | + doClick(bot.Mouse.Button.LEFT, state); |
| 106 | + assertEquals('rgba(0, 255, 255, 1)', getColor()); |
| 107 | + |
| 108 | + state.clientXY.y += 41; |
| 109 | + assertEquals(101, state.clientXY.y); |
| 110 | + doClick(bot.Mouse.Button.LEFT, state); |
| 111 | + assertEquals('rgba(255, 255, 0, 1)', getColor()); |
| 112 | + |
| 113 | + state.clientXY = {x: 50, y: 50}; |
| 114 | + doClick(bot.Mouse.Button.LEFT, state); |
| 115 | + assertEquals('rgba(0, 0, 255, 1)', getColor()); |
| 116 | + |
| 117 | + function doClick(button, state) { |
| 118 | + var res = webdriver.atoms.inject.action.mouseClick(button, state); |
| 119 | + res = bot.json.parse(res); |
| 120 | + bot.response.checkResponse(res); |
| 121 | + return res['value']; |
| 122 | + } |
| 123 | + |
| 124 | + function getColor() { |
| 125 | + var value = bot.dom.getEffectiveStyle(clickBox, 'backgroundColor'); |
| 126 | + return bot.color.standardizeColor('backgroundColor', value); |
| 127 | + } |
| 128 | +} |
| 129 | +</script> |
0 commit comments