summaryrefslogtreecommitdiff
path: root/crocoite/test_devtools.py
blob: 4ffbbf8ab90813bed5f3549ffbf34c322cfad42d (plain)
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
# Copyright (c) 2017 crocoite contributors
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import asyncio
import pytest

from aiohttp import web
import websockets

from .browser import ChromeService, NullService
from .devtools import Browser, Tab, MethodNotFound, Crashed, InvalidParameter

@pytest.fixture
async def browser ():
    with ChromeService () as url:
        yield Browser (url)

@pytest.fixture
async def tab (browser):
    async with browser as tab:
        yield tab
        # make sure there are no transactions left over (i.e. no unawaited requests)
        assert not tab.transactions

async def hello(request):
    return web.Response(text="Hello, world")

@pytest.fixture
async def server ():
    """ Simple HTTP server for testing notifications """
    app = web.Application()
    app.add_routes([web.get('/', hello)])
    runner = web.AppRunner(app)
    await runner.setup()
    site = web.TCPSite(runner, 'localhost', 8080)
    await site.start()
    yield app
    await runner.cleanup ()

@pytest.mark.asyncio
async def test_tab_create (tab):
    """ Creating tabs works """
    assert isinstance (tab, Tab)
    version = await tab.Browser.getVersion ()
    assert 'protocolVersion' in version
    assert tab.pending == 0

@pytest.mark.asyncio
async def test_tab_close (browser):
    """ Tabs are closed after using them """
    async with browser as tab:
        tid = tab.id
    # give the browser some time to close the tab
    await asyncio.sleep (0.5)
    tabs = [t['id'] async for t in browser]
    assert tid not in tabs

@pytest.mark.asyncio
async def test_tab_notify_enable_disable (tab):
    """ Make sure enabling/disabling notifications works for all known namespaces """
    for name in ('Debugger', 'DOM', 'Log', 'Network', 'Page', 'Performance', 'Profiler', 'Runtime', 'Security'):
        f = getattr (tab, name)
        await f.enable ()
        await f.disable ()

@pytest.mark.asyncio
async def test_tab_unknown_method (tab):
    with pytest.raises (MethodNotFound):
        await tab.Nonexistent.foobar ()

@pytest.mark.asyncio
async def test_tab_invalid_argument (tab):
    # should be string
    with pytest.raises (InvalidParameter):
        await tab.Page.captureScreenshot (format=123)

    with pytest.raises (InvalidParameter):
        await tab.Page.captureScreenshot (format=[123])

    with pytest.raises (InvalidParameter):
        await tab.Page.captureScreenshot (format={123: '456'})

@pytest.mark.asyncio
async def test_tab_crash (tab):
    with pytest.raises (Crashed):
        await tab.Page.crash ()

    # caling anything else now should fail as well
    with pytest.raises (Crashed):
        version = await tab.Browser.getVersion ()

@pytest.mark.asyncio
async def test_load (tab, server):
    await tab.Network.enable ()
    await tab.Page.navigate (url='http://localhost:8080')
    method, req = await tab.get ()
    assert method == tab.Network.requestWillBeSent
    method, resp = await tab.get ()
    assert method == tab.Network.responseReceived
    assert tab.pending == 0
    body = await tab.Network.getResponseBody (requestId=req['requestId'])
    assert body['body'] == "Hello, world"
    await tab.Network.disable ()

@pytest.mark.asyncio
async def test_recv_failure(browser):
    """ Inject failure into receiver process and crash it """
    async with browser as tab:
        await tab.ws.close ()
        with pytest.raises (Crashed):
            await tab.Browser.getVersion ()

    async with browser as tab:
        await tab.ws.close ()
        with pytest.raises (Crashed):
            await tab.get ()

    async with browser as tab:
        handle = asyncio.ensure_future (tab.get ())
        await tab.ws.close ()
        with pytest.raises (Crashed):
            await handle

def test_tab_function (tab):
    assert tab.Network.enable.name == 'Network.enable'
    assert tab.Network.disable == tab.Network.disable
    assert tab.Network.enable != tab.Network.disable
    assert tab.Network != tab.Network.enable
    assert callable (tab.Network.enable)
    assert not callable (tab.Network.enable.name)
    assert 'Network.enable' in repr (tab.Network.enable)

def test_tab_function_hash (tab):
    d = {tab.Network.enable: 1, tab.Network.disable: 2, tab.Page: 3, tab.Page.enable: 4}
    assert len (d) == 4

@pytest.mark.asyncio
async def test_ws_ping(tab):
    """
    Chrome does not like websocket pings and closes the connection if it
    receives one. Not sure why.
    """
    with pytest.raises (Crashed):
        await tab.ws.ping ()
        await tab.Browser.getVersion ()