Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
SeleniumHQ
GitHub Repository: SeleniumHQ/Selenium
Path: blob/trunk/py/selenium/webdriver/remote/mobile.py
1864 views
1
# Licensed to the Software Freedom Conservancy (SFC) under one
2
# or more contributor license agreements. See the NOTICE file
3
# distributed with this work for additional information
4
# regarding copyright ownership. The SFC licenses this file
5
# to you under the Apache License, Version 2.0 (the
6
# "License"); you may not use this file except in compliance
7
# with the License. You may obtain a copy of the License at
8
#
9
# http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing,
12
# software distributed under the License is distributed on an
13
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
# KIND, either express or implied. See the License for the
15
# specific language governing permissions and limitations
16
# under the License.
17
18
from .command import Command
19
20
21
class _ConnectionType:
22
def __init__(self, mask):
23
self.mask = mask
24
25
@property
26
def airplane_mode(self):
27
return self.mask % 2 == 1
28
29
@property
30
def wifi(self):
31
return (self.mask / 2) % 2 == 1
32
33
@property
34
def data(self):
35
return (self.mask / 4) > 0
36
37
38
class Mobile:
39
ConnectionType = _ConnectionType
40
ALL_NETWORK = ConnectionType(6)
41
WIFI_NETWORK = ConnectionType(2)
42
DATA_NETWORK = ConnectionType(4)
43
AIRPLANE_MODE = ConnectionType(1)
44
45
def __init__(self, driver):
46
import weakref
47
48
self._driver = weakref.proxy(driver)
49
50
@property
51
def network_connection(self):
52
return self.ConnectionType(self._driver.execute(Command.GET_NETWORK_CONNECTION)["value"])
53
54
def set_network_connection(self, network):
55
"""Set the network connection for the remote device.
56
57
Example of setting airplane mode::
58
59
driver.mobile.set_network_connection(driver.mobile.AIRPLANE_MODE)
60
"""
61
mode = network.mask if isinstance(network, self.ConnectionType) else network
62
return self.ConnectionType(
63
self._driver.execute(
64
Command.SET_NETWORK_CONNECTION, {"name": "network_connection", "parameters": {"type": mode}}
65
)["value"]
66
)
67
68
@property
69
def context(self):
70
"""Returns the current context (Native or WebView)."""
71
return self._driver.execute(Command.CURRENT_CONTEXT_HANDLE)
72
73
@context.setter
74
def context(self, new_context) -> None:
75
"""Sets the current context."""
76
self._driver.execute(Command.SWITCH_TO_CONTEXT, {"name": new_context})
77
78
@property
79
def contexts(self):
80
"""Returns a list of available contexts."""
81
return self._driver.execute(Command.CONTEXT_HANDLES)
82
83