19
19
20
20
import base64
21
21
import warnings
22
+ from contextlib import contextmanager
22
23
23
24
from .command import Command
24
25
from .webelement import WebElement
@@ -54,7 +55,8 @@ class WebDriver(object):
54
55
"""
55
56
56
57
def __init__ (self , command_executor = 'http://127.0.0.1:4444/wd/hub' ,
57
- desired_capabilities = None , browser_profile = None , proxy = None , keep_alive = False ):
58
+ desired_capabilities = None , browser_profile = None , proxy = None , keep_alive = False ,
59
+ file_detector = None ):
58
60
"""
59
61
Create a new driver that will issue commands using the wire protocol.
60
62
@@ -69,6 +71,8 @@ def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',
69
71
be started with given proxy settings, if possible. Optional.
70
72
- keep_alive - Whether to configure remote_connection.RemoteConnection to use
71
73
HTTP keep-alive. Defaults to False.
74
+ - file_detector - Pass custom file detector object during instantiation. If None,
75
+ then default LocalFileDetector() will be used.
72
76
"""
73
77
if desired_capabilities is None :
74
78
raise WebDriverException ("Desired Capabilities can't be None" )
@@ -87,12 +91,40 @@ def __init__(self, command_executor='http://127.0.0.1:4444/wd/hub',
87
91
self .start_session (desired_capabilities , browser_profile )
88
92
self ._switch_to = SwitchTo (self )
89
93
self ._mobile = Mobile (self )
90
- self .file_detector = LocalFileDetector ()
94
+ self .file_detector = file_detector or LocalFileDetector ()
91
95
92
96
def __repr__ (self ):
93
97
return '<{0.__module__}.{0.__name__} (session="{1}")>' .format (
94
98
type (self ), self .session_id )
95
99
100
+ @contextmanager
101
+ def file_detector_context (self , file_detector_class , * args , ** kwargs ):
102
+ """
103
+ Overrides the current file detector (if necessary) in limited context.
104
+ Ensures the original file detector is set afterwards.
105
+
106
+ Example:
107
+
108
+ with webdriver.file_detector_context(UselessFileDetector):
109
+ someinput.send_keys('/etc/hosts')
110
+
111
+ :Args:
112
+ - file_detector_class - Class of the desired file detector. If the class is different
113
+ from the current file_detector, then the class is instantiated with args and kwargs
114
+ and used as a file detector during the duration of the context manager.
115
+ - args - Optional arguments that get passed to the file detector class during
116
+ instantiation.
117
+ - kwargs - Keyword arguments, passed the same way as args.
118
+ """
119
+ last_detector = None
120
+ if not isinstance (self .file_detector , file_detector_class ):
121
+ last_detector = self .file_detector
122
+ self .file_detector = file_detector_class (* args , ** kwargs )
123
+ try :
124
+ yield
125
+ finally :
126
+ if last_detector is not None :
127
+ self .file_detector = last_detector
96
128
97
129
@property
98
130
def mobile (self ):
0 commit comments