|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import time |
| 16 | +from typing import TYPE_CHECKING, Generator |
| 17 | + |
| 18 | +import pytest |
16 | 19 |
|
17 | 20 | from appium import webdriver |
18 | | -from appium.options.common import AppiumOptions |
19 | 21 | from appium.webdriver.client_config import AppiumClientConfig |
20 | 22 | from test.helpers.constants import SERVER_URL_BASE |
21 | 23 |
|
22 | | -from .helper.desired_capabilities import get_desired_capabilities |
23 | | - |
24 | | - |
25 | | -class TestSafari: |
26 | | - def setup_method(self) -> None: |
27 | | - caps = get_desired_capabilities() |
28 | | - caps.update( |
29 | | - { |
30 | | - 'bundleId': 'com.apple.mobilesafari', |
31 | | - 'nativeWebTap': True, |
32 | | - 'safariIgnoreFraudWarning': True, |
33 | | - 'webviewConnectTimeout': 100000, |
34 | | - } |
35 | | - ) |
36 | | - client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE) |
37 | | - client_config.timeout = 600 |
38 | | - self.driver = webdriver.Remote(options=AppiumOptions().load_capabilities(caps), client_config=client_config) |
39 | | - |
40 | | - # Fresh iOS 17.4 simulator may not show up the webview context with "safari" |
41 | | - # after a fresh simlator instance creation. |
42 | | - # Re-launch the process could be a workaround in my debugging. |
43 | | - self.driver.terminate_app('com.apple.mobilesafari') |
44 | | - self.driver.activate_app('com.apple.mobilesafari') |
45 | | - |
46 | | - def teardown_method(self) -> None: |
47 | | - self.driver.quit() |
48 | | - |
49 | | - def test_context(self) -> None: |
50 | | - contexts = self.driver.contexts |
51 | | - assert 'NATIVE_APP' == contexts[0] |
52 | | - assert contexts[1].startswith('WEBVIEW_') |
53 | | - self.driver.switch_to.context(contexts[1]) |
54 | | - assert 'WEBVIEW_' in self.driver.current_context |
55 | | - |
56 | | - def test_get(self) -> None: |
57 | | - ok = False |
58 | | - contexts = self.driver.contexts |
59 | | - for context in contexts: |
60 | | - if context.startswith('WEBVIEW_'): |
61 | | - self.driver.switch_to.context(context) |
62 | | - ok = True |
63 | | - break |
64 | | - |
65 | | - if ok is False: |
66 | | - assert False, 'Could not set WEBVIEW context' |
67 | | - |
68 | | - self.driver.get('http://google.com') |
69 | | - for _ in range(5): |
70 | | - time.sleep(0.5) |
71 | | - if 'Google' == self.driver.title: |
72 | | - return |
73 | | - |
74 | | - assert False, 'The title was wrong' |
| 24 | +from .helper.options import make_options |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from appium.webdriver.webdriver import WebDriver |
| 28 | + |
| 29 | + |
| 30 | +@pytest.fixture |
| 31 | +def driver() -> Generator['WebDriver', None, None]: |
| 32 | + """Create and configure Safari driver for testing.""" |
| 33 | + options = make_options() |
| 34 | + options.bundle_id = 'com.apple.mobilesafari' |
| 35 | + options.native_web_tap = True |
| 36 | + options.safari_ignore_fraud_warning = True |
| 37 | + options.webview_connect_timeout = 100000 |
| 38 | + |
| 39 | + client_config = AppiumClientConfig(remote_server_addr=SERVER_URL_BASE) |
| 40 | + client_config.timeout = 600 |
| 41 | + driver = webdriver.Remote(options=options, client_config=client_config) |
| 42 | + |
| 43 | + # Fresh iOS 17.4 simulator may not show up the webview context with "safari" |
| 44 | + # after a fresh simlator instance creation. |
| 45 | + # Re-launch the process could be a workaround in my debugging. |
| 46 | + driver.terminate_app('com.apple.mobilesafari') |
| 47 | + driver.activate_app('com.apple.mobilesafari') |
| 48 | + |
| 49 | + yield driver |
| 50 | + |
| 51 | + driver.quit() |
| 52 | + |
| 53 | + |
| 54 | +def test_context(driver: 'WebDriver') -> None: |
| 55 | + """Test Safari context switching.""" |
| 56 | + contexts = driver.contexts |
| 57 | + assert 'NATIVE_APP' == contexts[0] |
| 58 | + assert contexts[1].startswith('WEBVIEW_') |
| 59 | + driver.switch_to.context(contexts[1]) |
| 60 | + assert 'WEBVIEW_' in driver.current_context |
| 61 | + |
| 62 | + |
| 63 | +def test_navigation(driver: 'WebDriver') -> None: |
| 64 | + """Test Safari navigation to Google.""" |
| 65 | + contexts = driver.contexts |
| 66 | + for context in contexts: |
| 67 | + if context.startswith('WEBVIEW_'): |
| 68 | + driver.switch_to.context(context) |
| 69 | + break |
| 70 | + else: |
| 71 | + pytest.fail('Could not set WEBVIEW context') |
| 72 | + |
| 73 | + driver.get('http://google.com') |
| 74 | + for _ in range(5): |
| 75 | + time.sleep(0.5) |
| 76 | + if 'Google' == driver.title: |
| 77 | + return |
| 78 | + else: |
| 79 | + pytest.fail('The title was wrong') |
0 commit comments