def test_insert_child(self): self.set_element(self.tag) # test parent in constructor self.c1 = WdomElement('c1', parent=self.tag) self.c1.textContent = 'child1' self.assertIsTrue(self.set_element(self.c1)) with self.assertRaises(NoSuchElementException): self.wait(0.1) self.set_element(self.c2, self.wait_time * 10) self.set_element(self.tag) self.wait_until(lambda: self.element.text == 'child1') self.assertEqual(self.element.text, 'child1') self.tag.insertBefore(self.c2, self.c1) self.assertIsTrue(self.set_element(self.c2)) self.set_element(self.tag) self.wait_until(lambda: self.element.text == 'child2child1') self.assertEqual(self.element.text, 'child2child1') self.tag.empty() self.wait_until(lambda: self.element.text == '') self.assertEqual(self.element.text, '') with self.assertRaises(NoSuchElementException): self.set_element(self.c1, self.wait_time * 10) with self.assertRaises(NoSuchElementException): self.set_element(self.c2, self.wait_time * 10)
def setUp(self): super().setUp() self.tag = WdomElement('tag') self.c1 = WdomElement('tag') self.c2 = WdomElement('tag') self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock)
def test_parse_func(self): elm = WdomElement('tag') df = parse_html(body_sample) self.assertTrue(isinstance(df, DocumentFragment)) self.assertEqual(df.length, 3) # h1, empty text, p elm.appendChild(df) self.assertEqual(body_sample, remove_wdom_id(elm.innerHTML))
def test_parse_style(self): elm = WdomElement('tag') df = parse_html(style_html) self.assertTrue(isinstance(df, DocumentFragment)) self.assertEqual(df.length, 1) # h1, empty text, p elm.appendChild(df) self.assertEqual(style_html, remove_wdom_id(elm.innerHTML)) self.assertEqual(style_css, elm.firstChild.innerHTML)
def setUp(self): self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock) self.elm = WdomElement('tag') set_app(self.elm) self.elm.js_exec = MagicMock() self.mock = MagicMock(_is_coroutine=False) self.elm.addEventListener('click', self.mock) msg = {'type': 'click', 'currentTarget': {'id': self.elm.wdom_id}} self.event = create_event(msg)
def setUp(self): super().setUp() self.elm = WdomElement('tag') set_app(self.elm) self.elm.js_exec = MagicMock() self.msg = { 'type': 'response', 'id': self.elm.wdom_id, } self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock)
class TestEvent(PyppeteerTestCase): def get_elements(self): self.root = WdomElement('div') self.tag = WdomElement('span', parent=self.root) self.click_event_mock = MagicMock() self.click_event_mock._is_coroutine = False self.btn = WdomElement('button') self.btn.textContent = 'click' self.btn.addEventListener('click', self.click_event_mock) self.input_event_mock = MagicMock() self.input_event_mock._is_coroutine = False self.input = WdomElement('input', type='text') self.input.addEventListener('input', self.input_event_mock) self.root.appendChild(self.btn) self.root.appendChild(self.input) return self.root @sync async def test_click(self): btn = await self.get_element_handle(self.btn) await btn.click() await self.wait() self.assertEqual(self.click_event_mock.call_count, 1) @sync async def test_input(self): await self.page.focus('[wdom_id="{}"]'.format(self.input.wdom_id)) await self.page.type('abc') await self.wait() self.assertEqual(self.input_event_mock.call_count, 3)
async def test_scroll(self): rect = WdomElement('div', style='width:3000px;height:3000px;background:#eee;') # noqa: #501 self.tag.appendChild(rect) await asyncio.sleep(self.wait_time) X = await rect.scrollX() Y = await rect.scrollY() self.assertEqual(X['x'], 0) self.assertEqual(Y['y'], 0) rect.scrollTo(200, 200) await asyncio.sleep(self.wait_time) X = await rect.scrollX() Y = await rect.scrollY() self.assertEqual(X['x'], 200) self.assertEqual(Y['y'], 200)
def test_class_in_init(self) -> None: tag = WdomElement('tag', class_='a') self.assertIsTrue(tag.hasClass('a')) self.assertIsTrue(tag.hasClasses()) self.assertRegex(tag.html, '<tag wdom_id="\d+" class="a"></tag>') tag.removeClass('a') self.assertIsFalse(tag.hasClass('a')) self.assertIsFalse(tag.hasClasses()) self.assertRegex(tag.html, '<tag wdom_id="\d+"></tag>')
def test_get_element_by_wdom_id(self): elm = WdomElement(tag='a') wdom_id = elm.wdom_id self.assertIs(getElementByWdomId(wdom_id), elm) self.assertIsNone(self.doc.getElementByWdomId(wdom_id)) self.doc.appendChild(elm) self.assertIs(getElementByWdomId(wdom_id), elm) self.assertIs(self.doc.getElementByWdomId(wdom_id), elm)
async def test_get_rect(self): rect = WdomElement('div', style='width:200px;height:100px;') self.tag.appendChild(rect) await asyncio.sleep(self.wait_time) data = await rect.getBoundingClientRect() self.assertEqual(data['width'], 200) self.assertEqual(data['height'], 100)
def get_elements(self): self.root = WdomElement('div') self.tag = WdomElement('span', parent=self.root) self.df = DocumentFragment() self.c1 = WdomElement('c1') self.c2 = WdomElement('c2') self.c3 = WdomElement('c3') self.c4 = WdomElement('c4') self.c1.textContent = 'child1' self.c2.textContent = 'child2' self.c3.textContent = 'child3' self.c4.textContent = 'child4' return self.root
def test_get_element_by_id(self): elm = WdomElement(tag='a', id='a', wdom_id='b') self.assertIs(getElementById('a'), elm) self.assertIs(getElementByWdomId('b'), elm) self.assertIsNone(self.doc.getElementById('a')) self.assertIsNone(self.doc.getElementByWdomId('b'), elm) self.doc.appendChild(elm) self.assertIs(getElementById('a'), elm) self.assertIs(getElementByWdomId('b'), elm) self.assertIs(self.doc.getElementById('a'), elm) self.assertIs(self.doc.getElementByWdomId('b'), elm)
async def test_insert_child(self): # test parent in constructor self.c1 = WdomElement('c1', parent=self.tag) await self.wait_for_element(self.c1) self.c1.textContent = 'child1' await self.wait() self.assertTrue(await self.get_element_handle(self.c1)) self.assertIsNone(await self.get_element_handle(self.c2)) self.assertEqual(await self.get_text(), 'child1') self.tag.insertBefore(self.c2, self.c1) await self.wait_for_element(self.c2) self.assertTrue(await self.get_element_handle(self.c2)) self.assertEqual(await self.get_text(), 'child2child1') self.tag.empty() await self.wait() self.assertEqual(await self.get_text(), '') self.assertIsNone(await self.get_element_handle(self.c1)) self.assertIsNone(await self.get_element_handle(self.c2))
class TestQuery(TestCase): def setUp(self): super().setUp() self.elm = WdomElement('tag') set_app(self.elm) self.elm.js_exec = MagicMock() self.msg = { 'type': 'response', 'id': self.elm.wdom_id, } self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock) def tearDown(self): _tornado.connections.remove(self.conn_mock) def test_query(self): fut = self.elm.js_query('test') self.elm.js_exec.assert_called_once_with('test', 0) self.msg['reqid'] = 0 self.msg['data'] = 1 self.elm.on_response(self.msg) self.assertEqual(fut.result(), 1) @sync async def test_scroll(self): fut = self.elm.scrollX() self.assertFalse(fut.done()) self.msg['reqid'] = 0 self.msg['data'] = {'x': 1} self.elm.on_response(self.msg) x = await fut self.assertEqual(x, {'x': 1})
def test_create_event(self): tag = WdomElement('tag') mock = MagicMock(_is_coroutine=False) tag.addEventListener('a', mock) e = self.doc.createEvent('a') self.assertEqual(type(e), Event) tag.dispatchEvent(e) mock.assert_called_once_with(e)
def test_style_init(self): _js_exec = WdomElement.js_exec WdomElement.js_exec = self.js_mock WdomElement('elm', style='color: red;') _call = call('setAttribute', 'style', 'color: red;') _call_remove = call('removeAttribute', 'style') self.js_mock.assert_has_calls([_call]) count = 0 for c in self.js_mock.call_args_list: if c == _call: count += 1 elif c == _call_remove: raise AssertionError('Unexpeted remove style') self.assertEqual(count, 1) WdomElement.js_exec = _js_exec
def setUp(self): super().setUp() self.elm = WdomElement('tag') set_app(self.elm) self.c1 = WdomElement() self.c2 = WdomElement() self.js_mock = MagicMock() self.js_mock1 = MagicMock() self.js_mock2 = MagicMock() self.elm.js_exec = self.js_mock self.c1.js_exec = self.js_mock1 self.c2.js_exec = self.js_mock2 self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock)
class TestEvent(ElementTestCase, TestCase): def get_elements(self): self.root = WdomElement('div') self.tag = WdomElement('span', parent=self.root) self.click_event_mock = MagicMock() self.click_event_mock._is_coroutine = False self.btn = WdomElement('button') self.btn.textContent = 'click' self.btn.addEventListener('click', self.click_event_mock) self.input_event_mock = MagicMock() self.input_event_mock._is_coroutine = False self.input = WdomElement('input', type='text') self.input.addEventListener('input', self.input_event_mock) self.root.appendChild(self.btn) self.root.appendChild(self.input) return self.root def test_click(self): # need to wait some times self.wait(times=10) self.set_element(self.btn) self.wait(times=10) self.element.click() self.wait(times=10) self.wait_until(lambda: self.click_event_mock.call_count == 1) self.assertEqual(self.click_event_mock.call_count, 1) def test_input(self): self.set_element(self.input) self.element.send_keys('abc') self.wait_until(lambda: self.input_event_mock.call_count == 3) self.assertEqual(self.input_event_mock.call_count, 3)
def get_elements(self): self.root = WdomElement('div') self.tag = WdomElement('span', parent=self.root) self.click_event_mock = MagicMock() self.click_event_mock._is_coroutine = False self.btn = WdomElement('button') self.btn.textContent = 'click' self.btn.addEventListener('click', self.click_event_mock) self.input_event_mock = MagicMock() self.input_event_mock._is_coroutine = False self.input = WdomElement('input', type='text') self.input.addEventListener('input', self.input_event_mock) self.root.appendChild(self.btn) self.root.appendChild(self.input) return self.root
class TestWdomElement(ElementTestCase, TestCase): def get_elements(self): self.root = WdomElement('div') self.tag = WdomElement('span', parent=self.root) self.df = DocumentFragment() self.c1 = WdomElement('c1') self.c2 = WdomElement('c2') self.c3 = WdomElement('c3') self.c4 = WdomElement('c4') self.c1.textContent = 'child1' self.c2.textContent = 'child2' self.c3.textContent = 'child3' self.c4.textContent = 'child4' return self.root def test_connection(self): self.wait_until(lambda: self.root.connected) self.assertTrue(self.root.connected) self.browser.get('http://example.com/') self.assertIsFalse(self.root.connected) def test_text_content(self): self.set_element(self.tag) self.assertEqual(self.element.text, '') self.tag.textContent = 'text' self.wait_until(lambda: self.element.text == 'text') self.assertEqual(self.element.text, 'text') self.c1.textContent = 'child' self.tag.appendChild(self.c1) self.wait_until(lambda: self.element.text == 'textchild') self.assertEqual(self.element.text, 'textchild') self.tag.textContent = 'NewText' self.wait_until(lambda: self.element.text == 'NewText') self.assertEqual(self.element.text, 'NewText') with self.assertRaises(NoSuchElementException): self.set_element(self.c1, self.wait_time * 10) self.set_element(self.tag) t_node = Text('TextNode') self.tag.replaceChild(t_node, self.tag.childNodes[0]) self.wait_until(lambda: self.element.text == 'TextNode') self.assertEqual(self.element.text, 'TextNode') self.tag.removeChild(self.tag.childNodes[0]) self.set_element(self.tag) self.wait_until(lambda: self.element.text == '') self.assertEqual(self.element.text, '') def test_attr(self): self.set_element(self.tag) self.assertIsNone(self.element.get_attribute('src')) self.tag.setAttribute('src', 'a') self.wait_until(lambda: self.element.get_attribute('src') == 'a') self.assertEqual(self.element.get_attribute('src'), 'a') self.tag.removeAttribute('src') self.wait_until(lambda: self.element.get_attribute('src') is None) self.assertIsNone(self.element.get_attribute('src')) def test_addremove_child(self): self.tag.appendChild(self.c1) self.set_element(self.c1) self.assertEqual(self.element.text, 'child1') self.c1.textContent = 'Child' self.wait_until(lambda: self.element.text == 'Child') self.assertEqual(self.element.text, 'Child') self.set_element(self.tag) self.wait_until(lambda: self.element.text == 'Child') self.assertEqual(self.element.text, 'Child') self.tag.removeChild(self.c1) with self.assertRaises(NoSuchElementException): self.wait(0.1) self.set_element(self.c1, self.wait_time * 10) self.set_element(self.tag) self.wait_until(lambda: self.element.text == '') self.assertEqual(self.element.text, '') def test_insert_child(self): self.set_element(self.tag) # test parent in constructor self.c1 = WdomElement('c1', parent=self.tag) self.c1.textContent = 'child1' self.assertIsTrue(self.set_element(self.c1)) with self.assertRaises(NoSuchElementException): self.wait(0.1) self.set_element(self.c2, self.wait_time * 10) self.set_element(self.tag) self.wait_until(lambda: self.element.text == 'child1') self.assertEqual(self.element.text, 'child1') self.tag.insertBefore(self.c2, self.c1) self.assertIsTrue(self.set_element(self.c2)) self.set_element(self.tag) self.wait_until(lambda: self.element.text == 'child2child1') self.assertEqual(self.element.text, 'child2child1') self.tag.empty() self.wait_until(lambda: self.element.text == '') self.assertEqual(self.element.text, '') with self.assertRaises(NoSuchElementException): self.set_element(self.c1, self.wait_time * 10) with self.assertRaises(NoSuchElementException): self.set_element(self.c2, self.wait_time * 10) def test_add_df(self): self.set_element(self.tag) self.df.append(self.c1, self.c2, 'text') self.tag.appendChild(self.df) self.wait_until(lambda: self.element.text == 'child1child2text') self.assertEqual(self.element.text, 'child1child2text') df = DocumentFragment() df.append(self.c3, 'text2') self.tag.appendChild(df) self.wait_until( lambda: self.element.text == 'child1child2textchild3text2') self.assertEqual(self.element.text, 'child1child2textchild3text2') def test_insert_df(self): self.set_element(self.tag) self.tag.appendChild(self.c1) self.df.append(self.c2, self.c3, 'text') self.tag.insertBefore(self.df, self.c1) self.wait_until(lambda: self.element.text == 'child2child3textchild1') self.assertEqual(self.element.text, 'child2child3textchild1') df = DocumentFragment() df.append(self.c4, 'text2') self.tag.insertBefore(df, self.c3) self.wait_until( lambda: self.element.text == 'child2child4text2child3textchild1') self.assertEqual(self.element.text, 'child2child4text2child3textchild1') def test_replace_child(self): self.set_element(self.tag) self.tag.appendChild(self.c1) with self.assertRaises(NoSuchElementException): self.set_element(self.c2, self.wait_time * 10) self.assertIsTrue(self.set_element(self.c1)) self.wait_until(lambda: self.element.text == 'child1') self.assertEqual(self.element.text, 'child1') self.tag.replaceChild(self.c2, self.c1) with self.assertRaises(NoSuchElementException): self.wait(0.1) self.set_element(self.c1, self.wait_time * 10) self.assertIsTrue(self.set_element(self.c2)) self.wait_until(lambda: self.element.text == 'child2') self.assertEqual(self.element.text, 'child2') self.set_element(self.tag) self.wait_until(lambda: self.element.text == 'child2') self.assertEqual(self.element.text, 'child2') def test_append(self): self.set_element(self.tag) self.tag.append(self.c1) self.wait_until(lambda: self.element.text == 'child1') self.assertEqual(self.element.text, 'child1') self.tag.append(self.c2, self.c3) self.wait_until(lambda: self.element.text == 'child1child2child3') self.assertEqual(self.element.text, 'child1child2child3') self.tag.append(self.c4, self.c1) self.wait_until( lambda: self.element.text == 'child2child3child4child1') self.assertEqual(self.element.text, 'child2child3child4child1') self.tag.append('t1', 't2') self.wait_until( lambda: self.element.text == 'child2child3child4child1t1t2') self.assertEqual(self.element.text, 'child2child3child4child1t1t2') def test_prepend(self): self.set_element(self.tag) self.tag.prepend(self.c1) self.wait_until(lambda: self.element.text == 'child1') self.assertEqual(self.element.text, 'child1') self.tag.prepend(self.c2, self.c3) self.wait_until(lambda: self.element.text == 'child2child3child1') self.assertEqual(self.element.text, 'child2child3child1') self.tag.prepend(self.c4, self.c1) self.wait_until( lambda: self.element.text == 'child4child1child2child3') self.assertEqual(self.element.text, 'child4child1child2child3') self.tag.prepend('t1', 't2') self.wait_until( lambda: self.element.text == 't1t2child4child1child2child3') self.assertEqual(self.element.text, 't1t2child4child1child2child3') def test_prepend_append_text(self): self.set_element(self.tag) self.tag.append('t1') self.wait_until(lambda: self.element.text == 't1') self.assertEqual(self.element.text, 't1') self.tag.firstChild.remove() self.wait_until(lambda: self.element.text == '') self.assertEqual(self.element.text, '') self.tag.prepend('t2') self.wait_until(lambda: self.element.text == 't2') self.assertEqual(self.element.text, 't2') self.tag.append('t3', 't4') self.wait_until(lambda: self.element.text == 't2t3t4') self.assertEqual(self.element.text, 't2t3t4') self.tag.prepend('t5', 't6') self.wait_until(lambda: self.element.text == 't5t6t2t3t4') self.assertEqual(self.element.text, 't5t6t2t3t4') def test_after(self): self.set_element(self.tag) self.tag.append(self.c1) self.c1.after(self.c2) self.wait_until(lambda: self.element.text == 'child1child2') self.assertEqual(self.element.text, 'child1child2') self.c1.after(self.c3, self.c4) self.wait_until( lambda: self.element.text == 'child1child3child4child2') self.assertEqual(self.element.text, 'child1child3child4child2') self.c1.after(self.c2, 'text') self.wait_until( lambda: self.element.text == 'child1child2textchild3child4') self.assertEqual(self.element.text, 'child1child2textchild3child4') def test_before(self): self.set_element(self.tag) self.tag.append(self.c1) self.c1.before(self.c2) self.wait_until(lambda: self.element.text == 'child2child1') self.assertEqual(self.element.text, 'child2child1') self.c1.before(self.c3, self.c4) self.wait_until( lambda: self.element.text == 'child2child3child4child1') self.assertEqual(self.element.text, 'child2child3child4child1') self.c1.before(self.c2, 'text') self.wait_until( lambda: self.element.text == 'child3child4child2textchild1') self.assertEqual(self.element.text, 'child3child4child2textchild1') def test_after_before_text(self): self.set_element(self.tag) self.tag.append('a') t = self.tag.firstChild t.after('b') self.wait_until(lambda: self.element.text == 'ab') self.assertEqual(self.element.text, 'ab') t.after('c', 'd') self.wait_until(lambda: self.element.text == 'acdb') self.assertEqual(self.element.text, 'acdb') t.before('e') self.wait_until(lambda: self.element.text == 'eacdb') self.assertEqual(self.element.text, 'eacdb') t.before('f', 'g') self.wait_until(lambda: self.element.text == 'efgacdb') self.assertEqual(self.element.text, 'efgacdb') def test_inner_html(self): self.set_element(self.tag) self.tag.innerHTML = '<div>a</div>' self.wait_until(lambda: self.element.text == 'a') self.set_element(self.tag.firstChild) self.wait_until(lambda: self.element.text == 'a') self.assertEqual(self.element.text, 'a') def test_shortcut_attr(self): self.tag.textContent = 'TAG' self.set_element(self.tag) self.wait_until(lambda: self.element.is_displayed()) self.assertIsTrue(self.element.is_displayed()) self.tag.hidden = True self.wait_until(lambda: not self.element.is_displayed()) self.assertIsFalse(self.element.is_displayed()) self.tag.hidden = False self.wait_until(lambda: self.element.is_displayed()) self.assertIsTrue(self.element.is_displayed()) def test_style(self): self.tag.textContent = 'Style' self.set_element(self.tag) self.wait_until(lambda: self.element.get_attribute('style') == '') self.assertEqual(self.element.get_attribute('style'), '') style = 'color: red;' self.tag.style = style self.wait_until(lambda: self.element.get_attribute('style') == style) self.assertEqual(self.element.get_attribute('style'), style) self.tag.style.color = 'black' self.wait_until( lambda: self.element.get_attribute('style') == 'color: black;') self.assertEqual(self.element.get_attribute('style'), 'color: black;') def test_classlist(self): self.set_element(self.tag) self.assertEqual(self.element.get_attribute('class'), '') self.tag.classList.add('a') self.wait_until(lambda: self.element.get_attribute('class') == 'a') self.assertEqual(self.element.get_attribute('class'), 'a') self.tag.classList.add('b', 'c', 'd') self.wait_until( lambda: self.element.get_attribute('class') == 'a b c d') self.assertEqual(self.element.get_attribute('class'), 'a b c d') self.tag.classList.remove('c') self.wait_until(lambda: self.element.get_attribute('class') == 'a b d') self.assertEqual(self.element.get_attribute('class'), 'a b d') self.tag.classList.remove('a', 'd') self.wait_until(lambda: self.element.get_attribute('class') == 'b') self.assertEqual(self.element.get_attribute('class'), 'b') self.tag.classList.toggle('b') self.wait_until(lambda: self.element.get_attribute('class') == '') self.assertEqual(self.element.get_attribute('class'), '') self.tag.classList.toggle('b') self.wait_until(lambda: self.element.get_attribute('class') == 'b') self.assertEqual(self.element.get_attribute('class'), 'b') def test_click(self): mock = MagicMock(_is_coroutine=False) self.tag.addEventListener('click', mock) self.tag.click() self.wait_until(lambda: mock.call_count == 1) self.assertEqual(mock.call_count, 1) @sync async def test_get_rect(self): rect = WdomElement('div', style='width:200px;height:100px;') self.tag.appendChild(rect) await asyncio.sleep(self.wait_time) data = await rect.getBoundingClientRect() self.assertEqual(data['width'], 200) self.assertEqual(data['height'], 100) @sync async def test_scroll(self): rect = WdomElement('div', style='width:3000px;height:3000px;background:#eee;') # noqa: #501 self.tag.appendChild(rect) await asyncio.sleep(self.wait_time) X = await rect.scrollX() Y = await rect.scrollY() self.assertEqual(X['x'], 0) self.assertEqual(Y['y'], 0) rect.scrollTo(200, 200) await asyncio.sleep(self.wait_time) X = await rect.scrollX() Y = await rect.scrollY() self.assertEqual(X['x'], 200) self.assertEqual(Y['y'], 200) def test_exec(self): self.tag.exec('this.style = "color: red;"') self.set_element(self.tag) self.wait_until( lambda: re.search(r'255,\s*0,\s* 0,\s*1\s*', self.element.value_of_css_property('color'))) self.assertRegex(self.element.value_of_css_property('color'), r'255,\s*0,\s* 0,\s*1\s*') self.tag.exec('node.style = "color: blue;"') self.wait_until( lambda: re.search(r'0,\s*0,\s* 255,\s*1\s*', self.element.value_of_css_property('color'))) self.assertRegex(self.element.value_of_css_property('color'), r'0,\s*0,\s*255,\s*1\s*') def test_exec_error(self): with self.assertLogs('wdom.server', 'ERROR') as log: self.tag.exec('a.b') self.wait_until(lambda: len(log.output) > 0) self.assertRegex(log.output[0], r'JS: ReferenceError')
class TestWdomElement(TestCase): def setUp(self): super().setUp() self.elm = WdomElement('tag') set_app(self.elm) self.c1 = WdomElement() self.c2 = WdomElement() self.js_mock = MagicMock() self.js_mock1 = MagicMock() self.js_mock2 = MagicMock() self.elm.js_exec = self.js_mock self.c1.js_exec = self.js_mock1 self.c2.js_exec = self.js_mock2 self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock) def tearDown(self): _tornado.connections.remove(self.conn_mock) def test_id(self): self.assertRegex(self.elm.html, r'<tag wdom_id="\d+"></tag>') self.assertRegex(self.elm.wdom_id, r'\d+') def test_noid(self): self.assertEqual('<tag></tag>', self.elm.html_noid) self.c1.tag = 'c1' appended_child1 = self.elm.appendChild(self.c1) self.assertIs(appended_child1, self.c1) self.assertEqual('<tag><c1></c1></tag>', self.elm.html_noid) self.c2.tag = 'c2' appended_child2 = self.c1.appendChild(self.c2) self.assertIs(appended_child2, self.c2) self.assertEqual('<tag><c1><c2></c2></c1></tag>', self.elm.html_noid) def test_id_init(self): elm = WdomElement('tag', wdom_id='myid') self.assertEqual('<tag wdom_id="myid"></tag>', elm.html) def test_connected(self): self.assertTrue(self.elm.connected) def test_parent(self): self.assertTrue(self.elm.parentNode) self.assertIsNone(self.c1.parentNode) self.elm.appendChild(self.c1) self.assertIs(self.elm, self.c1.parentNode) self.js_mock1.assert_not_called() self.assertEqual(self.js_mock.call_count, 1) self.js_mock.assert_called_once_with('insertAdjacentHTML', 'beforeend', self.c1.html) removed_child1 = self.elm.removeChild(self.c1) self.assertIs(removed_child1, self.c1) self.assertIsNone(self.c1.parentNode) self.assertEqual(self.js_mock.call_count, 2) self.js_mock1.assert_not_called() self.js_mock.assert_called_with('removeChildById', self.c1.wdom_id) def test_addremove_child(self): self.assertFalse(self.elm.hasChildNodes()) self.elm.appendChild(self.c1) self.assertTrue(self.elm.hasChildNodes()) self.assertIn(self.c1, self.elm) self.assertNotIn(self.c2, self.elm) self.assertEqual(self.elm.length, 1) self.elm.insertBefore(self.c2, self.c1) self.assertIn(self.c1, self.elm) self.assertIn(self.c2, self.elm) self.assertEqual(self.elm.length, 2) self.c2.remove() self.assertEqual(self.elm.length, 1) self.assertIn(self.c1, self.elm) self.assertNotIn(self.c2, self.elm) self.assertIsNone(self.c2.parentNode) self.js_mock2.assert_called_once_with('remove') self.elm.removeChild(self.c1) self.assertFalse(self.elm.hasChildNodes()) self.assertEqual(self.elm.length, 0) self.assertNotIn(self.c1, self.elm) self.assertNotIn(self.c2, self.elm) self.js_mock1.assert_called_once_with('insertAdjacentHTML', 'beforebegin', self.c2.html) self.assertEqual(self.js_mock.call_count, 3) with self.assertRaises(ValueError): self.elm.removeChild(self.c1) def test_append_text(self): from html import escape t = '<a>' t_node = Text(t) self.assertEqual(t_node.textContent, t) self.assertEqual(t_node.html, t) self.elm.appendChild(t_node) self.js_mock.assert_called_once_with('insertAdjacentHTML', 'beforeend', escape('<a>')) self.assertEqual(t_node.html, escape(t)) def test_addremove_attr(self): self.elm.setAttribute('src', 'a') self.js_mock.assert_called_with('setAttribute', 'src', 'a') self.elm.removeAttribute('src') self.js_mock.assert_called_with('removeAttribute', 'src') def test_attr_subscription(self): # test add tag-attr self.elm['a'] = 'b' self.assertEqual(self.elm['a'], 'b') self.assertIn('a="b"', self.elm.html) self.assertRegex(self.elm.start_tag, '<tag wdom_id="\d+" a="b">') self.assertRegex(self.elm.html, '<tag wdom_id="\d+" a="b"></tag>') del self.elm['a'] self.assertRegex(self.elm.html, '<tag wdom_id="\d+"></tag>') def test_style(self): self.elm.style = 'color: red;' self.js_mock.assert_called_once_with('setAttribute', 'style', 'color: red;') self.elm.removeAttribute('style') self.js_mock.assert_called_with('removeAttribute', 'style') self.elm.style.color = 'black' self.js_mock.assert_called_with('setAttribute', 'style', 'color: black;') def test_style_init(self): _js_exec = WdomElement.js_exec WdomElement.js_exec = self.js_mock WdomElement('elm', style='color: red;') _call = call('setAttribute', 'style', 'color: red;') _call_remove = call('removeAttribute', 'style') self.js_mock.assert_has_calls([_call]) count = 0 for c in self.js_mock.call_args_list: if c == _call: count += 1 elif c == _call_remove: raise AssertionError('Unexpeted remove style') self.assertEqual(count, 1) WdomElement.js_exec = _js_exec def test_set_text_content(self): self.elm.textContent = 'text' self.js_mock.assert_called_once_with('textContent', 'text') def test_set_inner_html(self): self.elm.innerHTML = 'html' self.js_mock.assert_called_once_with('innerHTML', 'html') def test_shallow_copy(self): from copy import copy clone = copy(self.elm) self.assertNotEqual(clone.wdom_id, self.elm.wdom_id) clone = self.elm.cloneNode() self.assertNotEqual(clone.wdom_id, self.elm.wdom_id) def test_deep_copy(self): from copy import deepcopy clone = deepcopy(self.elm) self.assertNotEqual(clone.wdom_id, self.elm.wdom_id) clone = self.elm.cloneNode(deep=True) self.assertNotEqual(clone.wdom_id, self.elm.wdom_id) def test_click(self): mock = MagicMock(_is_coroutine=False) self.elm.addEventListener('click', mock) self.js_mock.assert_called_once_with('addEventListener', 'click') self.elm.click() self.js_mock.assert_called_with('click') # called only from browser's click event self.assertEqual(mock.call_count, 0) def test_hidden(self): self.elm.show() self.assertRegex(self.elm.html, '<tag wdom_id="\d+"></tag>') self.elm.hide() self.assertRegex(self.elm.html, '<tag wdom_id="\d+" hidden></tag>') self.elm.show() self.assertRegex(self.elm.html, '<tag wdom_id="\d+"></tag>') def test_clone_node_sharrow_hidden(self): self.elm.hide() clone = self.elm.cloneNode() self.assertRegex(clone.html, '<tag wdom_id="\d+" hidden></tag>') clone.show() self.assertRegex(self.elm.html, '<tag wdom_id="\d+" hidden></tag>') self.assertRegex(clone.html, '<tag wdom_id="\d+"></tag>') def test_clone_node_deep_hidden(self): self.elm.appendChild(self.c1) self.c1.tag = 'tag' self.c1.hide() clone = self.elm.cloneNode(deep=True) self.assertRegex( self.elm.html, '<tag wdom_id="\d+"><tag wdom_id="\d+" hidden></tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+"><tag wdom_id="\d+" hidden></tag></tag>', ) self.c1.show() self.assertRegex( self.elm.html, '<tag wdom_id="\d+"><tag wdom_id="\d+"></tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+"><tag wdom_id="\d+" hidden></tag></tag>', )
class TestWdomElementClass(TestCase): def setUp(self): super().setUp() self.tag = WdomElement('tag') self.c1 = WdomElement('tag') self.c2 = WdomElement('tag') self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock) def tearDown(self): _tornado.connections.remove(self.conn_mock) def test_class_addremove(self): self.assertIsFalse(self.tag.hasClasses()) self.assertIsFalse(self.tag.hasClass('a')) self.assertRegex(self.tag.html, '<tag wdom_id="\d+"></tag>') self.tag.addClass('a') self.assertIsTrue(self.tag.hasClasses()) self.assertIsTrue(self.tag.hasClass('a')) self.assertIsFalse(self.tag.hasClass('b')) self.assertRegex(self.tag.html, '<tag wdom_id="\d+" class="a"></tag>') self.tag.removeClass('a') self.assertIsFalse(self.tag.hasClasses()) self.assertIsFalse(self.tag.hasClass('a')) self.assertRegex(self.tag.html, '<tag wdom_id="\d+"></tag>') def test_class_in_init(self) -> None: tag = WdomElement('tag', class_='a') self.assertIsTrue(tag.hasClass('a')) self.assertIsTrue(tag.hasClasses()) self.assertRegex(tag.html, '<tag wdom_id="\d+" class="a"></tag>') tag.removeClass('a') self.assertIsFalse(tag.hasClass('a')) self.assertIsFalse(tag.hasClasses()) self.assertRegex(tag.html, '<tag wdom_id="\d+"></tag>') def test_class_addremove_multi(self): self.tag.addClass('a', 'b', 'c') self.assertIsTrue(self.tag.hasClasses()) self.assertIsTrue(self.tag.hasClass('a')) self.assertIsTrue(self.tag.hasClass('b')) self.assertIsTrue(self.tag.hasClass('c')) self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a b c"></tag>', ) self.tag.removeClass('a', 'c') self.assertIsTrue(self.tag.hasClasses()) self.assertIsFalse(self.tag.hasClass('a')) self.assertIsTrue(self.tag.hasClass('b')) self.assertIsFalse(self.tag.hasClass('c')) self.assertRegex(self.tag.html, '<tag wdom_id="\d+" class="b"></tag>') def test_class_addremove_multi_string(self): with self.assertRaises(ValueError): self.tag.addClass('a b') def test_class_getset(self) -> None: self.assertEqual(self.tag['class'], None) self.tag.addClass('a') self.assertEqual(self.tag['class'], 'a') self.tag['class'] = 'b' self.assertEqual(self.tag['class'], 'b') self.assertIsFalse(self.tag.hasClass('a')) self.assertIsTrue(self.tag.hasClass('b')) def test_class_remove_error(self) -> None: with self.assertLogs('wdom', 'WARNING'): self.tag.removeClass('a') def test_clone_node_sharrow_class(self): self.tag.appendChild(self.c1) self.tag.addClass('a') clone = self.tag.cloneNode() self.assertRegex(clone.html, '<tag wdom_id="\d+" class="a"></tag>') clone.removeClass('a') self.assertRegex(clone.html, '<tag wdom_id="\d+"></tag>') self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) clone.addClass('b') self.assertRegex(clone.html, '<tag wdom_id="\d+" class="b"></tag>') self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) def test_clone_node_deep_class(self): self.tag.appendChild(self.c1) self.tag.addClass('a') self.c1.addClass('b') clone = self.tag.cloneNode(deep=True) self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+" class="b">' '</tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+" class="b">' '</tag></tag>', ) clone.childNodes[0].removeClass('b') self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+" class="b">' '</tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) self.c1.removeClass('b') self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) clone.addClass('c') self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+" class="a c"><tag wdom_id="\d+"></tag></tag>', ) clone.removeClass('a') self.assertRegex( self.tag.html, '<tag wdom_id="\d+" class="a"><tag wdom_id="\d+"></tag></tag>', ) self.assertRegex( clone.html, '<tag wdom_id="\d+" class="c"><tag wdom_id="\d+"></tag></tag>', ) def test_class_of_class(self): class A(WdomElement): class_ = 'a1' self.assertEqual(A.get_class_list().toString(), 'a1') a = A('a') self.assertRegex(a.html, '<a wdom_id="\d+" class="a1"></a>') a.addClass('a2') self.assertRegex(a.html, '<a wdom_id="\d+" class="a1 a2"></a>') with self.assertLogs('wdom', 'WARNING'): a.removeClass('a1') self.assertRegex(a.html, '<a wdom_id="\d+" class="a1 a2"></a>') def test_classes_multiclass(self): class A(WdomElement): class_ = 'a1 a2' self.assertEqual(A.get_class_list().toString(), 'a1 a2') a = A('a') a.addClass('a3', 'a4') self.assertRegex(a.html, '<a wdom_id="\d+" class="a1 a2 a3 a4"></a>') def test_classes_inherit_class(self): class A(WdomElement): class_ = 'a1 a2' class B(A): class_ = 'b1 b2' self.assertEqual(B.get_class_list().toString(), 'a1 a2 b1 b2') b = B('b') b.addClass('b3') self.assertRegex( b.html, '<b wdom_id="\d+" class="a1 a2 b1 b2 b3"></b>', ) def test_classes_notinherit_class(self): class A(WdomElement): class_ = 'a1 a2' class B(A): class_ = 'b1 b2' inherit_class = False self.assertEqual(B.get_class_list().toString(), 'b1 b2') b = B('b') b.addClass('b3') self.assertRegex(b.html, '<b wdom_id="\d+" class="b1 b2 b3"></b>') class C(B): class_ = 'c1 c2' self.assertEqual(C.get_class_list().toString(), 'b1 b2 c1 c2') def test_classes_inherit_diamond(self): class A(WdomElement): class_ = 'a' class B(A): class_ = 'b' class C(A): class_ = 'c' class D(B, C): class_ = 'd' self.assertEqual(D.get_class_list().toString(), 'a c b d')
class TestWdomElement(PyppeteerTestCase): def setUp(self): super().setUp() def get_elements(self): root = WdomElement('div') self.tag = WdomElement('span', parent=root) self.df = DocumentFragment() self.c1 = WdomElement('c1') self.c2 = WdomElement('c2') self.c3 = WdomElement('c3') self.c4 = WdomElement('c4') self.c1.textContent = 'child1' self.c2.textContent = 'child2' self.c3.textContent = 'child3' self.c4.textContent = 'child4' return root @sync async def test_connection(self): self.assertTrue(self.root.connected) await self.page.goto('https://example.com') await self.wait() self.assertFalse(self.root.connected) @sync async def test_text_content(self): self.element = await self.get_element_handle(self.tag) self.assertEqual(await self.get_text(), '') self.tag.textContent = 'text' await self.wait() self.assertEqual(await self.get_text(), 'text') self.c1.textContent = 'child' self.tag.appendChild(self.c1) await self.wait_for_element(self.c1) self.assertEqual(await self.get_text(), 'textchild') self.tag.textContent = 'NewText' await self.wait() self.assertEqual(await self.get_text(), 'NewText') self.assertIsNone(await self.get_element_handle(self.c1)) t_node = Text('TextNode') self.tag.replaceChild(t_node, self.tag.childNodes[0]) await self.wait() self.assertEqual(await self.get_text(), 'TextNode') self.tag.removeChild(t_node) await self.wait() self.assertEqual(await self.get_text(), '') @sync async def test_attr(self): self.element = await self.get_element_handle(self.tag) self.assertIsNone(await self.get_attribute('src')) self.tag.setAttribute('src', 'a') await self.wait() self.assertEqual(await self.get_attribute('src'), 'a') self.tag.removeAttribute('src') await self.wait() self.assertIsNone(await self.get_attribute('src')) @sync async def test_addremove_child(self): self.tag.appendChild(self.c1) await self.wait_for_element(self.c1) self.assertEqual(await self.get_text(), 'child1') self.c1.textContent = 'Child' await self.wait() self.assertEqual(await self.get_text(), 'Child') self.tag.removeChild(self.c1) await self.wait() self.assertIsNone(await self.get_element_handle(self.c1)) self.assertEqual(await self.get_text(), '') @sync async def test_insert_child(self): # test parent in constructor self.c1 = WdomElement('c1', parent=self.tag) await self.wait_for_element(self.c1) self.c1.textContent = 'child1' await self.wait() self.assertTrue(await self.get_element_handle(self.c1)) self.assertIsNone(await self.get_element_handle(self.c2)) self.assertEqual(await self.get_text(), 'child1') self.tag.insertBefore(self.c2, self.c1) await self.wait_for_element(self.c2) self.assertTrue(await self.get_element_handle(self.c2)) self.assertEqual(await self.get_text(), 'child2child1') self.tag.empty() await self.wait() self.assertEqual(await self.get_text(), '') self.assertIsNone(await self.get_element_handle(self.c1)) self.assertIsNone(await self.get_element_handle(self.c2)) @sync async def test_add_df(self): self.df.append(self.c1, self.c2, 'text') self.tag.appendChild(self.df) await self.wait_for_element(self.c1) await self.wait_for_element(self.c2) self.assertEqual(await self.get_text(), 'child1child2text') df = DocumentFragment() df.append(self.c3, 'text2') self.tag.appendChild(df) await self.wait_for_element(self.c3) self.assertEqual(await self.get_text(), 'child1child2textchild3text2') @sync async def test_insert_df(self): self.tag.appendChild(self.c1) await self.wait_for_element(self.c1) self.df.append(self.c2, self.c3, 'text') self.tag.insertBefore(self.df, self.c1) await self.wait_for_element(self.c2) await self.wait_for_element(self.c3) self.assertEqual(await self.get_text(), 'child2child3textchild1') df = DocumentFragment() df.append(self.c4, 'text2') self.tag.insertBefore(df, self.c3) await self.wait_for_element(self.c4) self.assertEqual(await self.get_text(), 'child2child4text2child3textchild1') # noqa @sync async def test_replace_child(self): self.tag.appendChild(self.c1) await self.wait_for_element(self.c1) self.assertIsNotNone(await self.get_element_handle(self.c1)) self.assertIsNone(await self.get_element_handle(self.c2)) self.assertEqual(await self.get_text(), 'child1') self.tag.replaceChild(self.c2, self.c1) await self.wait_for_element(self.c2) self.assertIsNone(await self.get_element_handle(self.c1)) self.assertIsNotNone(await self.get_element_handle(self.c2)) self.assertEqual(await self.get_text(), 'child2') @sync async def test_append(self): self.tag.append(self.c1) await self.wait_for_element(self.c1) self.assertEqual(await self.get_text(), 'child1') self.tag.append(self.c2, self.c3) await self.wait_for_element(self.c2) await self.wait_for_element(self.c3) self.assertEqual(await self.get_text(), 'child1child2child3') self.tag.append(self.c4, self.c1) await self.wait_for_element(self.c4) self.assertEqual(await self.get_text(), 'child2child3child4child1') self.tag.append('t1', 't2') await self.wait() self.assertEqual(await self.get_text(), 'child2child3child4child1t1t2') @sync async def test_prepend(self): self.tag.prepend(self.c1) await self.wait_for_element(self.c1) self.assertEqual(await self.get_text(), 'child1') self.tag.prepend(self.c2, self.c3) await self.wait_for_element(self.c2) await self.wait_for_element(self.c3) self.assertEqual(await self.get_text(), 'child2child3child1') self.tag.prepend(self.c4, self.c1) await self.wait_for_element(self.c4) self.assertEqual(await self.get_text(), 'child4child1child2child3') self.tag.prepend('t1', 't2') await self.wait() self.assertEqual(await self.get_text(), 't1t2child4child1child2child3') @sync async def test_prepend_append_text(self): self.tag.append('t1') await self.wait() self.assertEqual(await self.get_text(), 't1') self.tag.firstChild.remove() await self.wait() self.assertEqual(await self.get_text(), '') self.tag.prepend('t2') await self.wait() self.assertEqual(await self.get_text(), 't2') self.tag.append('t3', 't4') await self.wait() self.assertEqual(await self.get_text(), 't2t3t4') self.tag.prepend('t5', 't6') await self.wait() self.assertEqual(await self.get_text(), 't5t6t2t3t4') @sync async def test_after(self): self.tag.append(self.c1) await self.wait_for_element(self.c1) self.c1.after(self.c2) await self.wait_for_element(self.c2) self.assertEqual(await self.get_text(), 'child1child2') self.c1.after(self.c3, self.c4) await self.wait_for_element(self.c3) await self.wait_for_element(self.c4) self.assertEqual(await self.get_text(), 'child1child3child4child2') self.c1.after(self.c2, 'text') await self.wait() self.assertEqual(await self.get_text(), 'child1child2textchild3child4') @sync async def test_before(self): self.tag.append(self.c1) await self.wait_for_element(self.c1) self.c1.before(self.c2) await self.wait_for_element(self.c2) self.assertEqual(await self.get_text(), 'child2child1') self.c1.before(self.c3, self.c4) await self.wait_for_element(self.c3) await self.wait_for_element(self.c4) self.assertEqual(await self.get_text(), 'child2child3child4child1') self.c1.before(self.c2, 'text') await self.wait() self.assertEqual(await self.get_text(), 'child3child4child2textchild1') @sync async def test_after_before_text(self): self.tag.append('a') t = self.tag.firstChild t.after('b') await self.wait() self.assertEqual(await self.get_text(), 'ab') t.after('c', 'd') await self.wait() self.assertEqual(await self.get_text(), 'acdb') t.before('e') await self.wait() self.assertEqual(await self.get_text(), 'eacdb') t.before('f', 'g') await self.wait() self.assertEqual(await self.get_text(), 'efgacdb') @sync async def test_inner_html(self): self.tag.innerHTML = '<div>a</div>' await self.wait() self.assertEqual(await self.get_text(), 'a') @sync async def test_shortcut_attr(self): tag = await self.get_element_handle(self.tag) self.tag.textContent = 'TAG' await self.wait() self.assertFalse(await tag.evaluate('(elm) => elm.hasAttribute("hidden")')) self.tag.hidden = True await self.wait() self.assertTrue(await tag.evaluate('(elm) => elm.hasAttribute("hidden")')) self.tag.hidden = False await self.wait() self.assertFalse(await tag.evaluate('(elm) => elm.hasAttribute("hidden")')) @sync async def test_style(self): self.element = await self.get_element_handle(self.tag) self.tag.textContent = 'Style' await self.wait() self.assertIsNone(await self.get_attribute('style')) style = 'color: red;' self.tag.style = style await self.wait() self.assertEqual(await self.get_attribute('style'), style) self.tag.style.color = 'black' await self.wait() self.assertEqual(await self.get_attribute('style'), 'color: black;') @sync async def test_classlist(self): self.element = await self.get_element_handle(self.tag) self.assertEqual(await self.get_attribute('class'), None) self.assertNotIn('class', await self.element.evaluate('(elm) => elm.outerHTML')) self.tag.classList.add('a') await self.wait() self.assertEqual(await self.get_attribute('class'), 'a') self.tag.classList.add('b', 'c', 'd') await self.wait() self.assertEqual(await self.get_attribute('class'), 'a b c d') self.tag.classList.remove('c') await self.wait() self.assertEqual(await self.get_attribute('class'), 'a b d') self.tag.classList.remove('a', 'd') await self.wait() self.assertEqual(await self.get_attribute('class'), 'b') self.tag.classList.toggle('b') await self.wait() self.assertEqual(await self.get_attribute('class'), None) self.assertNotIn('class', await self.element.evaluate('(elm) => elm.outerHTML')) self.tag.classList.toggle('b') await self.wait() self.assertEqual(await self.get_attribute('class'), 'b') @sync async def test_click(self): mock = MagicMock(_is_coroutine=False) self.tag.addEventListener('click', mock) self.tag.click() await self.wait() self.assertEqual(mock.call_count, 1) @sync async def test_get_rect(self): rect = WdomElement('div', style='width:200px;height:100px;') self.tag.appendChild(rect) await self.wait() data = await rect.getBoundingClientRect() self.assertEqual(data['width'], 200) self.assertEqual(data['height'], 100) @sync async def test_scroll(self): rect = WdomElement( 'div', style='width:3000px;height:3000px;background:#eee;') # noqa: #501 self.tag.appendChild(rect) await self.wait() X = await rect.scrollX() Y = await rect.scrollY() self.assertEqual(X['x'], 0) self.assertEqual(Y['y'], 0) rect.scrollTo(200, 200) await asyncio.sleep(self.wait_time) X = await rect.scrollX() Y = await rect.scrollY() self.assertEqual(X['x'], 200) self.assertEqual(Y['y'], 200) @sync async def test_exec(self): self.element = await self.get_element_handle(self.tag) self.tag.exec('this.style = "color: red;"') await self.wait() self.assertEqual(await self.get_attribute('style'), 'color: red;') self.tag.exec('node.style = "color: blue;"') await self.wait() self.assertEqual(await self.get_attribute('style'), 'color: blue;') @sync async def test_exec_error(self): with self.assertLogs('wdom.server', 'ERROR') as log: self.tag.exec('a.b') await self.wait() self.assertRegex(log.output[0], r'JS: ReferenceError')
class TestEventMessage(TestCase): def setUp(self): self.conn_mock = MagicMock() _tornado.connections.append(self.conn_mock) self.elm = WdomElement('tag') set_app(self.elm) self.elm.js_exec = MagicMock() self.mock = MagicMock(_is_coroutine=False) self.elm.addEventListener('click', self.mock) msg = {'type': 'click', 'currentTarget': {'id': self.elm.wdom_id}} self.event = create_event(msg) def tearDown(self): _tornado.connections.remove(self.conn_mock) def test_handle_event(self): self.elm.js_exec.assert_called_once_with('addEventListener', 'click') self.elm.dispatchEvent(self.event) self.assertTrue(self.mock.called) def test_remove_event(self): self.elm.removeEventListener('click', self.mock) self.elm.js_exec.assert_called_with('removeEventListener', 'click') self.elm.dispatchEvent(self.event) self.mock.assert_not_called() def test_remove_multi_event(self): self.elm.addEventListener('click', self.mock) self.elm.removeEventListener('click', self.mock) with self.assertRaises(AssertionError): self.elm.js_exec.assert_has_calls( call('removeEventListener', 'click')) self.elm.removeEventListener('click', self.mock) self.elm.js_exec.assert_called_with('removeEventListener', 'click') self.elm.dispatchEvent(self.event) self.mock.assert_not_called()
def test_id_init(self): elm = WdomElement('tag', wdom_id='myid') self.assertEqual('<tag wdom_id="myid"></tag>', elm.html)