|
31 | 31 | import types |
32 | 32 |
|
33 | 33 | from .extension_connection import ExtensionConnection |
| 34 | +from contextlib import contextmanager |
| 35 | + |
34 | 36 | from .firefox_binary import FirefoxBinary |
35 | 37 | from .firefox_profile import FirefoxProfile |
36 | 38 | from .options import Options |
@@ -130,6 +132,10 @@ def __init__(self, firefox_profile=None, firefox_binary=None, |
130 | 132 |
|
131 | 133 | # W3C remote |
132 | 134 | # TODO(ato): Perform conformance negotiation |
| 135 | + |
| 136 | + self.CONTEXT_CHROME = 'chrome' |
| 137 | + self.CONTEXT_CONTENT = 'content' |
| 138 | + |
133 | 139 | if capabilities.get("marionette"): |
134 | 140 | self.service = Service(executable_path, log_path=log_path) |
135 | 141 | self.service.start() |
@@ -195,3 +201,25 @@ def firefox_profile(self): |
195 | 201 |
|
196 | 202 | def set_context(self, context): |
197 | 203 | self.execute("SET_CONTEXT", {"context": context}) |
| 204 | + |
| 205 | + @contextmanager |
| 206 | + def context(self, context): |
| 207 | + """Sets the context that Selenium commands are running in using |
| 208 | + a `with` statement. The state of the context on the server is |
| 209 | + saved before entering the block, and restored upon exiting it. |
| 210 | +
|
| 211 | + :param context: Context, may be one of the class properties |
| 212 | + `CONTEXT_CHROME` or `CONTEXT_CONTENT`. |
| 213 | +
|
| 214 | + Usage example:: |
| 215 | +
|
| 216 | + with selenium.context(selenium.CONTEXT_CHROME): |
| 217 | + # chrome scope |
| 218 | + ... do stuff ... |
| 219 | + """ |
| 220 | + initial_context = self.execute('GET_CONTEXT').pop('value') |
| 221 | + self.set_context(context) |
| 222 | + try: |
| 223 | + yield |
| 224 | + finally: |
| 225 | + self.set_context(initial_context) |
0 commit comments