-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbrowser_utils.py
More file actions
executable file
·53 lines (42 loc) · 1.43 KB
/
browser_utils.py
File metadata and controls
executable file
·53 lines (42 loc) · 1.43 KB
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
import re
from playwright.async_api import Error, Page, expect
from constants import FACEBOOK_URL, SUPPORTED_FB_LANGUAGES
async def is_logged_in(page: Page, expected_title_pattern: str) -> bool:
"""
Check if the user is logged in.
:param page: The browser page instance.
:param expected_title_pattern: The expected title of the Facebook groups page.
:return: A boolean indicating if the user is logged in.
:raise AssertionError: If the user is not logged in.
:raise playwright.sync_api.Error: If an error occurs during the process.
"""
try:
await expect(page).to_have_title(re.compile(expected_title_pattern))
except AssertionError:
return False
except Error:
return False
else:
return True
async def get_fb_lang(page: Page) -> str | None:
"""
Get the language of the Facebook user interface.
:param page: The browser page instance.
"""
try:
await navigate(page, FACEBOOK_URL + "/groups/feed/")
lang = await page.locator("html").get_attribute("lang")
except Error:
return None
else:
if lang in SUPPORTED_FB_LANGUAGES:
return lang
return None
async def navigate(page: Page, url: str) -> None:
"""
Navigates to the given URL.
Errors are handled by the caller function.
:param page: The browser page instance.
:param url: The URL to navigate to.
"""
await page.goto(url)