本文整理汇总了Python中selenium.webdriver.firefox.webdriver.WebDriver.chain方法的典型用法代码示例。如果您正苦于以下问题:Python WebDriver.chain方法的具体用法?Python WebDriver.chain怎么用?Python WebDriver.chain使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类selenium.webdriver.firefox.webdriver.WebDriver
的用法示例。
在下文中一共展示了WebDriver.chain方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_tests
# 需要导入模块: from selenium.webdriver.firefox.webdriver import WebDriver [as 别名]
# 或者: from selenium.webdriver.firefox.webdriver.WebDriver import chain [as 别名]
def run_tests(firefox_path=None):
basedir = os.path.dirname(__file__)
driver = None
profile = FirefoxProfile()
if firefox_path:
if sys.platform == "darwin" and os.path.isdir(firefox_path):
firefox_path = os.path.join(firefox_path, "Contents", "MacOS", "firefox")
binary = FirefoxBinary(firefox_path)
else:
binary = None
try:
build1 = tempfile.NamedTemporaryFile(mode="wb", suffix=".xpi", delete=False)
build2 = tempfile.NamedTemporaryFile(mode="wb", suffix=".xpi", delete=False)
try:
createBuild(basedir, type="gecko", outFile=build1)
createBuild(os.path.join(basedir, "testhelper"), type="gecko", outFile=build2)
profile.add_extension(build1.name)
profile.add_extension(build2.name)
finally:
os.unlink(build1.name)
os.unlink(build2.name)
driver = WebDriver(profile, firefox_binary=binary)
driver.wait_until = lambda method: WebDriverWait(driver, default_timeout).until(lambda d: method())
driver.accept_alert = Alert(driver).accept
driver.keys = Keys
def chain(*actions):
for action in actions:
c = ActionChains(driver)
action(c)
c.perform()
driver.chain = chain
max_timestamp = {"value": 0}
def get_urls():
result = []
prefix = "[testhelper] Loading: "
new_timestamp = max_timestamp["value"]
for item in driver.get_log("browser"):
timestamp = item["timestamp"]
if timestamp <= max_timestamp["value"] or not item["message"].startswith(prefix):
continue
if timestamp > new_timestamp:
new_timestamp = timestamp
result.append(item["message"][len(prefix):])
max_timestamp["value"] = new_timestamp
return result
driver.get_urls = get_urls
def close_background_tabs():
driver.execute_script('''
var event = document.createEvent("Events");
event.initEvent("testhelper_closeBackgroundTabs", true, false);
document.dispatchEvent(event);
''')
driver.close_background_tabs = close_background_tabs
def middle_click(self):
driver.execute_script('''
var event = document.createEvent("Events");
event.initEvent("testhelper_middleclick", true, false);
arguments[0].dispatchEvent(event);
''', self)
WebElement.middle_click = middle_click
environment = {
"__builtins__": __builtins__,
"driver": driver,
}
testdir = os.path.join(basedir, "tests")
for filename in os.listdir(testdir):
if filename.startswith(".") or not filename.endswith(".py"):
continue
filepath = os.path.join(testdir, filename)
environment["__file__"] = filepath
with open(filepath, "rb") as handle:
exec handle in environment
finally:
if driver:
driver.quit()
shutil.rmtree(profile.path, ignore_errors=True)