-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathend2end_test.py
executable file
·846 lines (635 loc) · 27.8 KB
/
end2end_test.py
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
#!/usr/bin/env python3
from __future__ import annotations
from contextlib import ExitStack
from dataclasses import dataclass
from datetime import datetime
from pathlib import Path
from time import sleep
from typing import Iterator, TypeVar, Callable
import os
import pytest
from selenium.webdriver import Remote as Driver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.support import expected_conditions as EC
from promnesia.tests.common import get_testdata
from promnesia.tests.utils import index_urls
from promnesia.tests.server_helper import run_server as wserver
from promnesia.logging import LazyLogger
from common import under_ci, has_x, local_http_server, notnone
from webdriver_utils import is_visible, wait_for_alert, get_webdriver
from addon import get_addon_source, Addon, LOCALHOST, addon
logger = LazyLogger('promnesia-tests', level='debug')
@dataclass
class Browser:
dist: str
headless: bool
@property
def name(self) -> str:
return self.dist.split('-')[0] # TODO meh
def skip_ci_x(self) -> None:
if under_ci() and not self.headless:
pytest.skip("Only can't use headless browser on CI")
FF = Browser('firefox', headless=False)
CH = Browser('chrome' , headless=False)
FFH = Browser('firefox', headless=True)
CHH = Browser('chrome' , headless=True)
# TODO ugh, I guess it's not that easy to make it work because of isAndroid checks...
# I guess easy way to test if you really want is to temporary force isAndroid to return true in extension...
FM = Browser('firefox-mobile', headless=False)
def browser_(driver: Driver) -> Browser:
name = driver.name
# TODO figure out headless??
if name == 'firefox':
return FF
elif name == 'chrome':
return CH
else:
raise AssertionError(driver)
def confirm(what: str) -> None:
is_headless = 'headless' in os.environ.get('PYTEST_CURRENT_TEST', '')
if is_headless:
# ugh.hacky
Headless().confirm(what)
return
import click
click.confirm(click.style(what, blink=True, fg='yellow'), abort=True)
# TODO focus window if not headless
class Manual:
def confirm(self, what: str) -> None:
raise NotImplementedError
class Interactive(Manual):
def confirm(self, what: str) -> None:
confirm(what)
class Headless(Manual):
def confirm(self, what: str) -> None:
logger.warning('"%s": headless mode, responding "yes"', what)
'''
Helper for tests that are not yet fully automated and require a human to check...
- if running with the GUI, will be interactive
- if running in headless mode, will automatically assume 'yes'.
of course it's not very robust, but at least we're testing some codepaths then
'''
manual = Interactive() if has_x() else Headless()
WITH_BROWSER_TESTS = 'WITH_BROWSER_TESTS'
with_browser_tests = pytest.mark.skipif(
WITH_BROWSER_TESTS not in os.environ,
reason=f'set env var {WITH_BROWSER_TESTS}=true if you want to run this test',
)
X = TypeVar('X')
IdType = Callable[[X], X]
def browsers(*br: Browser) -> IdType:
if len(br) == 0:
br = (FF, FFH, CH, CHH)
if not has_x():
br = tuple(b for b in br if b.headless)
from functools import wraps
def dec(f):
if len(br) == 0:
dec_ = pytest.mark.skip('Filtered out all browsers (because of no GUI/non-interactive mode)')
else:
dec_ = pytest.mark.parametrize('browser', br, ids=lambda b: b.dist.replace('-', '_') + ('_headless' if b.headless else ''))
@with_browser_tests
@dec_
@wraps(f)
def ff(*args, **kwargs):
return f(*args, **kwargs)
return ff
return dec
@pytest.fixture
def driver(tmp_path: Path, browser: Browser) -> Iterator[Driver]:
profile_dir = tmp_path / 'browser_profile'
res = get_webdriver(
profile_dir=profile_dir,
addon_source=get_addon_source(kind=browser.dist),
browser=browser.name,
headless=browser.headless,
logger=logger,
)
try:
yield res
finally:
res.quit()
@dataclass
class Backend:
backend_dir: Path # directory with database and configs
port: str
@pytest.fixture
def backend(tmp_path: Path, addon: Addon) -> Iterator[Backend]:
backend_dir = tmp_path
# TODO ideally should index in a separate thread? and perhaps start server too
with wserver(db=backend_dir / 'promnesia.sqlite') as srv:
# this bit (up to yield) takes about 1.5s -- I guess it's the 1s sleep in configure_extension
addon.configure(host=LOCALHOST, port=srv.port)
addon.helper.driver.get('about:blank') # not sure if necessary
yield Backend(backend_dir=backend_dir, port=srv.port)
@browsers()
def test_installs(addon: Addon) -> None:
"""
Even loading the extension into webdriver is pretty elaborate, so the test just checks it works
"""
assert addon.helper.addon_id is not None
@browsers()
def test_settings(addon: Addon, driver: Driver) -> None:
"""
Just a basic test for opening options page and making sure it loads options
"""
addon.options_page.open()
hh = driver.find_element(By.ID, 'host_id')
assert hh.get_attribute('value') == 'http://localhost:13131' # default
addon.configure(host=LOCALHOST, port='12345', show_dots=False)
driver.get('about:blank')
addon.options_page.open()
hh = driver.find_element(By.ID, 'host_id')
assert hh.get_attribute('value') == 'http://localhost:12345'
@browsers()
def test_backend_status(addon: Addon, driver: Driver) -> None:
"""
We should get an alert if backend is unavailable on the status check
"""
addon.options_page.open()
addon.options_page._set_endpoint(host='https://nosuchhost.com', port='1234')
driver.find_element(By.ID, 'backend_status_id').click()
alert = wait_for_alert(driver)
assert 'ERROR' in alert.text
alert.accept()
# TODO implement positive check, e.g. when backend is present
@browsers()
def test_sidebar_position(addon: Addon, driver: Driver) -> None:
"""
Checks that default sidebar position is on the right, and that changing it to --bottom: 1 works
"""
options_page = addon.options_page
options_page.open()
# TODO WTF; if we don't open extension page once, we can't read out hotkeys from the chrome extension settings file
# (so e.g. trigger_command isn't working???)
options_page._set_endpoint(host=None, port=None) # we don't need backend here
driver.get('https://example.com')
addon.sidebar.open()
confirm("sidebar: should be displayed on the right (default)")
addon.sidebar.close()
options_page.open()
settings = """
#promnesia-frame {
--bottom: 1;
--size: 20%;
}""".strip()
options_page._set_position(settings)
options_page._save()
driver.get('https://example.com')
addon.sidebar.open()
confirm("sidebar: should be displayed below")
@browsers()
def test_bad_port(addon: Addon, driver: Driver) -> None:
"""
Check that we get error notification instead of silently failing if the endpoint is wrong
"""
addon.configure(host=LOCALHOST, port='12346')
driver.get('https://example.com')
sidebar = addon.sidebar
sidebar.open()
# TODO would be nice to check that regular visits also work..
with sidebar.ctx():
visits = sidebar.visits
[err] = visits
assert err.get_attribute('class') == 'error'
assert 'http://localhost:12346/visits: unavailable' in err.text
# TODO check 'mark visited' too?
# although at the moment due to error handling in sources.visited it's not really showing error
# if there is a single visit in the results
@browsers()
def test_blacklist_custom(addon: Addon, driver: Driver) -> None:
addon.configure(port='12345', blacklist=('stackoverflow.com',))
driver.get('https://stackoverflow.com/questions/27215462')
addon.activate()
manual.confirm('page should be blacklisted (black icon), you should see an error notification')
# make sure there is not even the frame for blacklisted page
assert not addon.sidebar.available
# reset blacklist
# also running without backend here, so need to set host to none as well
addon.configure(host=None, blacklist=())
driver.back()
driver.refresh()
addon.sidebar.open()
manual.confirm('sidebar: should be visible')
@browsers()
def test_blacklist_builtin(addon: Addon, driver: Driver) -> None:
addon.configure(port='12345')
driver.get('https://www.hsbc.co.uk/mortgages/')
addon.activate()
manual.confirm('page should be blacklisted (black icon), your should see an error notification')
# make sure there is not even the frame for blacklisted page
assert not addon.sidebar.available
# reset blacklist
# also running without backend here, so need to set host to none as well
addon.configure(host=None, excludelists=())
driver.back()
driver.refresh()
addon.sidebar.open()
manual.confirm('sidebar: should be visible')
@browsers(FF, CH)
def test_add_to_blacklist_context_menu(addon: Addon, driver: Driver) -> None:
# doesn't work on headless because not sure how to interact with context menu.
addon.configure(port='12345')
driver.get('https://example.com')
addon.open_context_menu()
addon.helper.gui_write(['enter']) # select first item
confirm('shows prompt with alert to enter pattern to block?')
wait_for_alert(driver).accept()
# ugh, seems necessary to guard with sleep; otherwise racey
sleep(0.5)
driver.get(driver.current_url)
confirm('page should be blacklisted (black icon)')
# todo might be nice to run soft asserts for this test?
@browsers()
def test_visits(addon: Addon, driver: Driver, backend: Backend) -> None:
# TODO separate this out into a parameterized test?
# also check that custom hostname/ip works too
# backend listens on 'localhost', and it's also default in extension prefs
# this triggers permissions check in Firefox v3 manifest, so good enough
ip = '127.0.0.1'
addon.configure(host=f'http://{ip}', port=backend.port)
from promnesia.tests.sources.test_hypothesis import index_hypothesis
test_url = "http://www.e-flux.com/journal/53/59883/the-black-stack/"
# test_url = "file:///usr/share/doc/python3/html/library/contextlib.html" # todo ??
index_hypothesis(backend.backend_dir)
driver.get(test_url)
confirm("sidebar: SHOULD NOT be visible")
with addon.sidebar.ctx():
# hmm not sure how come it returns anything at all.. but whatever
srcs = driver.find_elements(By.CLASS_NAME, 'src')
for s in srcs:
# elements should be bound to the sidebar, but aren't displayed yet
assert not is_visible(driver, s), s
assert len(srcs) >= 8, srcs
# todo ugh, need to filter out filters, how to only query the ones in the sidebar?
addon.sidebar.open()
confirm('sidebar: you SHOULD see hypothesis contexts')
with addon.sidebar.ctx():
# sleep(1)
# locate the linkified link in sidebar
link = driver.find_element(By.PARTIAL_LINK_TEXT, 'how_algorithms_shape_our_world')
assert is_visible(driver, link), link
contexts = driver.find_elements(By.CLASS_NAME, 'context')
for c in contexts:
assert is_visible(driver, c), c
assert len(contexts) == 8
addon.sidebar.close()
confirm("sidebar: SHOULD NOT be visible")
@browsers()
def test_search_around(addon: Addon, driver: Driver, backend: Backend) -> None:
from promnesia.tests.sources.test_hypothesis import index_hypothesis
# TODO hmm. dunno if we want to highlight only result with the same timestamp, or the results that are 'near'??
ts = int(datetime.strptime("2017-05-22T10:59:12.082375+00:00", '%Y-%m-%dT%H:%M:%S.%f%z').timestamp())
index_hypothesis(backend.backend_dir)
addon.open_search_page(f'?utc_timestamp_s={ts}')
visits = driver.find_element(By.ID, 'visits')
sleep(1) # wait till server responds and renders results
results = visits.find_elements(By.CSS_SELECTOR, 'li')
assert len(results) == 9
hl = visits.find_element(By.CLASS_NAME, 'highlight')
assert 'anthrocidal' in hl.text
manual.confirm('you should see search results, "anthrocidal" should be highlighted red')
# FIXME test clicking search around in actual search page.. it didn't work, seemingly because of initBackground() handling??
@browsers()
def test_show_visited_marks(addon: Addon, driver: Driver, backend: Backend) -> None:
# fmt: off
visited = {
'https://en.wikipedia.org/wiki/Special_linear_group': 'some note about linear groups',
'http://en.wikipedia.org/wiki/Unitary_group' : None,
'en.wikipedia.org/wiki/Transpose' : None,
}
# fmt: on
test_url = "https://en.wikipedia.org/wiki/Symplectic_group"
index_urls(visited)(backend.backend_dir)
addon.configure(show_dots=False)
driver.get(test_url)
sleep(2) # hmm not sure why it's necessary, but often fails headless firefox otherwise
addon.mark_visited()
sleep(1) # marks are async, wait till it marks
slg = driver.find_elements(By.XPATH, '//a[contains(@href, "/wiki/Special_linear_group")]')
assert len(slg) > 0
for s in slg:
assert 'promnesia-visited' in notnone(s.get_attribute('class'))
confirm(
"You should see visited marks near 'Special linear group', 'Unitary group', 'Transpose'. 'Special linear group' should be green."
)
@browsers()
@pytest.mark.parametrize(
'url',
[
"https://en.wikipedia.org/wiki/Symplectic_group",
# regression test for https://github.com/karlicoss/promnesia/issues/295
# note: seemed to reproduce on chrome more consistently for some reason
"https://www.udemy.com/course/javascript-bible/",
],
ids=['wiki', 'udemy'],
)
def test_sidebar_basic(url: str, addon: Addon, driver: Driver, backend: Backend) -> None:
if 'udemy' in url:
pytest.skip('TODO udemy tests are very timing out. Perhaps because of cloudflare protection?')
visited = {
# this also tests org-mode style link highlighting (custom anchorme version)
url: 'whatever\nalso [[https://wiki.openhumans.org/wiki/Personal_Science_Wiki][Personal Science Wiki]]\nmore text',
}
src = "ælso test unicode 💩"
addon.configure(show_dots=True)
index_urls(visited, source_name=src)(backend.backend_dir)
driver.get(url)
addon.sidebar.open()
# a bit crap, but also annoying to indent just to put it in context considering it impacts all of the test...
# ugh also it doesn't work for some reason..
# helper._sidebar.ctx().__enter__()
with addon.sidebar.ctx():
filters = addon.sidebar.filters
assert len(filters) == 2, filters
_all = filters[0]
tag = filters[1]
# this should happen in JS
sanitized = src.replace(' ', '')
assert 'all' in _all.text
assert sanitized in tag.text
assert 'all' in notnone(_all.get_attribute('class')).split()
assert sanitized in notnone(tag .get_attribute('class')).split()
visits = addon.sidebar.visits
assert len(visits) == 1, visits
[v] = visits
assert v.find_element(By.CLASS_NAME, 'src').text == sanitized
ctx_el = v.find_element(By.CLASS_NAME, 'context')
assert ctx_el.text == visited[url]
# make sure linkifying works
assert ctx_el.find_element(By.TAG_NAME, 'a').get_attribute('href') == 'https://wiki.openhumans.org/wiki/Personal_Science_Wiki'
confirm("You should see green icon, also one visit in sidebar. Make sure the unicode is displayed correctly.")
@browsers()
def test_search_command(addon: Addon, driver: Driver, backend: Backend) -> None:
"""
Basic test that search command handler works and it opens search inteface
"""
from promnesia.tests.sources.test_hypothesis import index_hypothesis
index_hypothesis(backend.backend_dir)
test_url = "https://en.wikipedia.org/wiki/Symplectic_vector_space"
driver.get(test_url)
addon.search()
# TODO actually search something?
# TODO use current domain as default? or 'parent' url?
confirm("You shoud see search prompt now, with focus on search field")
@browsers()
def test_new_background_tab(addon: Addon, driver: Driver, backend: Backend) -> None:
from promnesia.tests.sources.test_hypothesis import index_hypothesis
index_hypothesis(backend.backend_dir)
addon.configure(notify_contexts=True)
start_url = "http://www.e-flux.com/journal/53/59883/the-black-stack/"
# bg_url_text = "El Proceso (The Process)"
# TODO generate some fake data instead?
driver.get(start_url)
manual.confirm('you should see notification about contexts')
page_logo = driver.find_element(By.XPATH, '//a[@class="page-logo"]')
page_logo.send_keys(Keys.CONTROL + Keys.ENTER) # ctrl+click -- opens the link in new background tab
manual.confirm('you should not see any new notifications')
# TODO switch to new tab?
# TODO https://www.e-flux.com/journal/53/
PYTHON_DOC_PATH = Path('/usr/share/doc/python3/html')
@pytest.fixture
def exit_stack() -> Iterator[ExitStack]:
with ExitStack() as stack:
yield stack
@browsers()
@pytest.mark.parametrize(
'base_url',
[
f'file://{PYTHON_DOC_PATH}',
'LOCAL',
],
ids=[
'file',
'local',
],
)
def test_sidebar_navigation(base_url: str, addon: Addon, driver: Driver, backend: Backend, exit_stack: ExitStack) -> None:
if 'file:' in base_url and driver.name == 'chrome':
pytest.skip("TODO used to work, but must have broken after some Chrome update?")
# seems broken on any local page -- only transparent sidebar frame is shown
# the issue is that contentDocument.body is null -- no idea why
if driver.name == 'chrome':
pytest.skip(
"TODO need to split the test into version which isn's using back/forward. see https://bugs.chromium.org/p/chromedriver/issues/detail?id=4329"
)
# also need to extract a scenario for manual testing I guess
if base_url == 'LOCAL':
local_addr = exit_stack.enter_context(local_http_server(PYTHON_DOC_PATH))
base_url = local_addr
tutorial = f'{base_url}/tutorial/index.html'
reference = f'{base_url}/reference/index.html'
# reference has a link to tutorial (so will display a context)
urls = {
tutorial: 'TODO read this https://please-highligh-this-link.com',
reference: None,
}
index_urls(urls)(backend.backend_dir)
url = reference
# TODO hmm so this bit is actually super fast, takes like 1.5 secs
# need to speed up the preparation
driver.get(url)
assert not addon.sidebar.visible
confirm("grey icon. sidebar should NOT be visible")
driver.get(tutorial)
assert not addon.sidebar.visible
confirm("green icon. sidebar should NOT be visible")
# TODO ideally we'll get rid of it
# at the moment without this sleep chrome pretty much always fails
def sleep_if_chrome() -> None:
if driver.name == 'chrome':
sleep(0.01)
# switch between these in quick succession deliberately
# previously it was triggering a bug when multiple sidebars would be injected due to race condition
for i in range(100):
driver.get(url)
sleep_if_chrome()
driver.get(tutorial)
sleep_if_chrome()
if i % 10 == 0:
# huh, it's quite slow... to run it on single iteration
# what it's really testing here is that there is only one promnesia frame/sidebar
assert not addon.sidebar.visible
# hmm, headless chrome web test failed here on CI once...
# yep, still happening...
# and firefox is failing as well at times (which is sort of good news)
addon.sidebar.open()
confirm("green icon. sidebar should open and show one visit")
driver.back()
assert not addon.sidebar.visible
confirm("grey/purple icon, sidebar should NOT be visible")
# again, stress test it to try to trigger weird bugs
for i in range(100):
sleep_if_chrome()
driver.forward()
# TODO ugh. still failing here sometimes under headless firefox??
# if i % 10 == 0:
# assert helper.sidebar.visible
sleep_if_chrome()
driver.back()
if i % 10 == 0:
# huh, it's quite slow... to run it on single iteration
# what it's really testing here is that there is only one promnesia frame/sidebar
assert not addon.sidebar.visible
# checks it's still possible to interact with the sidebar
assert not addon.sidebar.visible
driver.forward()
# sidebar should be preserved between page transitions
assert addon.sidebar.visible
confirm('green icon, sidebar visible')
# check that still can interact with the sidebar
addon.sidebar.close()
confirm('green icon, sidebar is closed')
@browsers()
def test_unreachable(addon: Addon, driver: Driver, backend: Backend) -> None:
pytest.skip("NOTE: broken at the moment because webNavigation.onCompleted isn't working for unreachable pages")
url = 'https://somenonexist1ngurl.com'
urls = {
url: 'some context',
}
index_urls(urls)(backend.backend_dir)
addon.configure(notify_contexts=True, verbose_errors=True)
try:
driver.get(url)
except:
# results in exception because it's unreachable
pass
manual.confirm('green icon, no errors, desktop notification with contexts')
@browsers()
def test_stress(addon: Addon, driver: Driver, backend: Backend) -> None:
url = 'https://www.reddit.com/'
urls = [(f'{url}/subpath/{i}.html', f'context {i}' if i > 10000 else None) for i in range(50000)]
index_urls(urls)(backend.backend_dir)
driver.get(url)
addon.activate()
# todo I guess it's kinda tricky to test in headless webdriver
manual.confirm(
'''
Is performance reasonable?
The sidebar should show up, and update gradually.
You should be able to scroll the page, trigger tooltips, etc., without any lags.
'''.strip()
)
@browsers()
def test_fuzz(addon: Addon, driver: Driver, backend: Backend) -> None:
# TODO ugh. this still results in 'tab permissions' pages, but perhaps because of closed tabs?
# dunno if it's worth fixing..
urls = {
'https://www.iana.org/help/example-domains': 'IANA',
'iana.org/help/example-domains': 'IANA2',
}
index_urls(urls)(backend.backend_dir)
addon.configure(notify_contexts=True)
driver.get('https://example.com')
tabs = 30
for _ in range(tabs):
# find and click "More information" link, open them in new background tabs
driver.find_element(By.TAG_NAME, 'a').send_keys(Keys.CONTROL + Keys.RETURN)
sleep(5)
for _ in range(tabs - 2):
driver.close()
sleep(0.1)
driver.switch_to.window(driver.window_handles[0])
if addon.helper.headless:
pytest.skip("Rest of this test uses send_key to restore tab and it's not working under headless webdriver :(")
for _ in range(10):
addon.helper.gui_hotkey('Ctrl+Shift+t') # restore tabs
sleep(0.1)
confirm("shouldn't result in 'unexpected error occured'; show only show single notification per page")
@browsers()
def test_duplicate_background_pages(addon: Addon, driver: Driver, backend: Backend) -> None:
url = 'https://example.com'
index_urls({'whatever.coom': '123'})(backend.backend_dir)
driver.get(url)
addon.sidebar.open()
confirm('sidebar opened?')
original = driver.current_window_handle
# NOTE: Sidebar.trigger_search asserts that only one search window is opened
# so this test is actually fairly automated
addon.sidebar.trigger_search()
driver.switch_to.window(original)
addon.sidebar.trigger_search()
driver.switch_to.window(original)
confirm('only two search pages should be opened (in background tabs)')
addon.sidebar.close()
confirm('sidebar should be closed now')
# TODO wtf? browser with search pages stays open after test...
# TODO getting this in chrome inspector while running this...
# VM2048 common.js:116 [background] [object Object]
# log @ VM2048 common.js:116
# notifyError @ VM2056 notifications.js:40
# Promise.catch (async)
# (anonymous) @ VM2056 notifications.js:49
# VM2056 notifications.js:17 Uncaught (in promise) TypeError: Cannot read property 'create' of undefined
# at notify (VM2056 notifications.js:17)
# at notifyError (VM2056 notifications.js:41)
@browsers()
def test_showvisits_popup(addon: Addon, driver: Driver, backend: Backend) -> None:
url = 'https://www.iana.org/'
indexer = index_urls([('https://www.iana.org/abuse', 'some comment')])
indexer(backend.backend_dir)
addon.configure(notify_contexts=True, show_dots=True)
driver.get(url)
# todo might need to wait until marks are shown?
link_with_popup = driver.find_elements(By.XPATH, '//a[@href = "/abuse"]')[0]
# wait till visited marks appear
Wait(driver, timeout=5).until(
EC.presence_of_element_located((By.CLASS_NAME, 'promnesia-visited')),
)
addon.move_to(link_with_popup) # hover over visited mark
# meh, but might need some time to render..
popup_context = Wait(driver, timeout=5).until(
EC.presence_of_element_located((By.CLASS_NAME, 'context')),
)
sleep(3) # text might take some time to render too..
assert is_visible(driver, popup_context)
assert popup_context.text == 'some comment'
popup_datetime = Wait(driver, timeout=5).until(
EC.presence_of_element_located((By.CLASS_NAME, 'datetime'))
)
assert is_visible(driver, popup_datetime)
assert popup_datetime.text in {
'10/09/2014, 00:00:00',
'9/10/2014, 12:00:00 AM', # TODO ugh. github actions has a different locale..
}
@browsers()
def test_multiple_page_updates(tmp_path: Path, addon: Addon, driver: Driver, backend: Backend, exit_stack: ExitStack) -> None:
# on some pages, onUpdated is triggered multiple times (because of iframes or perhaps something else??)
# which previously resulted in flickering sidebar/performance degradation etc, so it's a regression test against this
# TODO would be nice to hook to the backend and check how many requests it had...
http_server_root = get_testdata('test_multiple_page_updates')
base_url = exit_stack.enter_context(local_http_server(http_server_root))
indexer = index_urls(
[
(f'{base_url}', 'this url is for the toast'),
('https://github.com/karlicoss/promnesia', 'this url is to be marked as visited on the page'),
(f'{base_url}/projects/1', 'just a note for the sidebar'),
]
)
indexer(backend.backend_dir)
addon.configure(notify_contexts=True, show_dots=True)
driver.get(base_url)
had_toast = False
# TODO need a better way to check this...
seen_titles = set()
for _ in range(50):
toasts = driver.find_elements(By.CLASS_NAME, 'toastify')
if len(toasts) == 1:
had_toast = True
assert len(toasts) <= 1
sleep(0.1)
seen_titles.add(driver.title)
assert had_toast
assert len(seen_titles) > 10, seen_titles # precondition to make sure page update events would be triggered
addon.sidebar.open()
addon.sidebar.close()
xpath = '//a[@href = "https://github.com/karlicoss/promnesia"]'
links_to_mark = driver.find_elements(By.XPATH, xpath)
assert len(links_to_mark) >= 2 # sanity check
for l in links_to_mark:
assert 'promnesia-visited' in notnone(l.get_attribute('class'))
# TODO would be nice to test clicking on them...
# TODO FIXME need to test racey conditions _while_ page is loading, results in this 'unexpected error occured'?
# TODO shit, sometimes I have 'bindSidebarData is not defined'? with vebose errors on demo_how_did_i_get_here