20 from selenium
import webdriver
21 from selenium.webdriver.support.event_firing_webdriver
import EventFiringWebDriver
28 from .webdrivertools
import WebDriverCreator
34 LibraryComponent.__init__(self, ctx)
47 self.
debugdebug(
'Closing all browsers.')
54 self.
debugdebug(
'Closing browser with session id {}.'
152 remote_url=False, desired_capabilities=None,
153 ff_profile_dir=None, service_log_path=None):
156 self.
infoinfo(
'Using existing browser from index %s.' % index)
161 desired_capabilities, ff_profile_dir,
165 remote_url=False, desired_capabilities=None,
166 ff_profile_dir=None, service_log_path=None):
168 self.
infoinfo(
"Opening browser '%s' to base url '%s' through "
169 "remote server at '%s'." % (browser, url, remote_url))
171 self.
infoinfo(
"Opening browser '%s' to base url '%s'." % (browser, url))
172 driver = self.
_make_driver_make_driver(browser, desired_capabilities,
173 ff_profile_dir, remote_url,
179 self.
ctxctx.register_driver(driver, alias)
180 self.
debugdebug(
"Opened browser with session id %s but failed "
181 "to open url '%s'." % (driver.session_id, url))
183 self.
debugdebug(
'Opened browser with session id %s.' % driver.session_id)
184 return self.
ctxctx.register_driver(driver, alias)
218 if not isinstance(kwargs, dict):
219 raise RuntimeError(
"kwargs must be a dictionary.")
220 for arg_name
in kwargs:
221 if arg_name
in init_kwargs:
222 raise RuntimeError(
"Got multiple values for argument '%s'." % arg_name)
223 init_kwargs[arg_name] = kwargs[arg_name]
224 driver_name = driver_name.strip()
226 creation_func = getattr(webdriver, driver_name)
227 except AttributeError:
228 raise RuntimeError(
"'%s' is not a valid WebDriver name." % driver_name)
229 self.
infoinfo(
"Creating an instance of the %s WebDriver." % driver_name)
230 driver = creation_func(**init_kwargs)
231 self.
debugdebug(
"Created %s WebDriver instance with session id %s."
232 % (driver_name, driver.session_id))
234 return self.
ctxctx.register_driver(driver, alias)
237 if not self.
ctxctx.event_firing_webdriver:
239 self.
debugdebug(
'Wrapping driver to event_firing_webdriver.')
240 return EventFiringWebDriver(driver, self.
ctxctx.event_firing_webdriver())
272 raise RuntimeError(
"No browser with index or alias '%s' found."
274 self.
debugdebug(
'Switched to browser with Selenium session id %s.'
314 message = (
"Location should have been '%s' but "
315 "was '%s'." % (url, actual))
316 raise AssertionError(message)
317 self.
infoinfo(
"Current location is '%s'." % url)
331 if expected
not in actual:
333 message = (
"Location should have contained '%s' but "
334 "it was '%s'." % (expected, actual))
335 raise AssertionError(message)
336 self.
infoinfo(
"Current location contains '%s'." % expected)
354 self.
loglog(source, loglevel)
376 message =
"Title should have been '%s' but was '%s'." % (title, actual)
377 raise AssertionError(message)
378 self.
infoinfo(
"Page title is '%s'." % title)
388 self.
infoinfo(
"Opening url '%s'" % url)
404 return secs_to_timestr(self.
ctxctx.speed)
414 return secs_to_timestr(self.
ctxctx.timeout)
424 return secs_to_timestr(self.
ctxctx.implicit_wait)
441 self.
ctxctx.speed = timestr_to_secs(value)
463 self.
ctxctx.timeout = timestr_to_secs(value)
465 driver.set_script_timeout(self.
ctxctx.timeout)
489 self.
ctxctx.implicit_wait = timestr_to_secs(value)
491 driver.implicitly_wait(self.
ctxctx.implicit_wait)
501 self.
driverdriverdriver.implicitly_wait(timestr_to_secs(value))
504 profile_dir=None, remote=None, service_log_path=None):
506 browser=browser, desired_capabilities=desired_capabilities,
507 remote_url=remote, profile_dir=profile_dir, service_log_path=service_log_path)
508 driver.set_script_timeout(self.
ctxctx.timeout)
509 driver.implicitly_wait(self.
ctxctx.implicit_wait)
510 if self.
ctxctx.speed:
515 def execute(self, driver_command, params=None):
516 result = self._base_execute(driver_command, params)
517 speed = self._speed
if hasattr(self,
'_speed')
else 0.0
521 if not hasattr(driver,
'_base_execute'):
522 driver._base_execute = driver.execute
523 driver.execute = types.MethodType(execute, driver)
524 driver._speed = self.
ctxctx.speed
def debug(self, msg, html=False)
def info(self, msg, html=False)
def log(self, msg, level='INFO', html=False)
def reload_page(self)
Simulates user reloading page.
def get_selenium_timeout(self)
Gets the timeout that is used by various keywords.
def set_browser_implicit_wait(self, value)
Sets the implicit wait value used by Selenium.
def get_selenium_implicit_wait(self)
Gets the implicit wait value used by Selenium.
def location_should_contain(self, expected, message=None)
Verifies that current URL contains expected.
def open_browser(self, url, browser='firefox', alias=None, remote_url=False, desired_capabilities=None, ff_profile_dir=None, service_log_path=None)
Opens a new browser instance to the given url.
def __init__(self, ctx)
Base class exposing attributes from the common context.
def log_location(self)
Logs and returns the current URL.
def set_selenium_implicit_wait(self, value)
Sets the implicit wait value used by Selenium.
def log_source(self, loglevel='INFO')
Logs and returns the HTML source of the current page or frame.
def _make_driver(self, browser, desired_capabilities=None, profile_dir=None, remote=None, service_log_path=None)
def set_selenium_timeout(self, value)
Sets the timeout that is used by various keywords.
def _wrap_event_firing_webdriver(self, driver)
def _monkey_patch_speed(self, driver)
def _make_new_browser(self, url, browser='firefox', alias=None, remote_url=False, desired_capabilities=None, ff_profile_dir=None, service_log_path=None)
def location_should_be(self, url, message=None)
Verifies that current URL is exactly url.
def go_back(self)
Simulates the user clicking the back button on their browser.
def close_browser(self)
Closes the current browser.
def set_selenium_speed(self, value)
Sets the delay that is waited after each Selenium command.
def get_session_id(self)
Returns the currently active browser session id.
def close_all_browsers(self)
Closes all open browsers and resets the browser cache.
def go_to(self, url)
Navigates the active browser instance to the provided url.
def get_source(self)
Returns the entire HTML source of the current page or frame.
def get_selenium_speed(self)
Gets the delay that is waited after each Selenium command.
def log_title(self)
Logs and returns the title of current page.
def get_location(self)
Returns the current browser URL.
def get_title(self)
Returns the title of current page.
def title_should_be(self, title, message=None)
Verifies that current page title equals title.
def switch_browser(self, index_or_alias)
Switches between active browsers using index_or_alias.
def create_webdriver(self, driver_name, alias=None, kwargs={}, **init_kwargs)
Creates an instance of Selenium WebDriver.