34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from behave import given, when, then
|
||
from selenium import webdriver
|
||
from time import sleep
|
||
|
||
@given('关键词 {keyword}') # 对应步骤 Given 关键词 behave, 参数放在{}中
|
||
def step_impl(context, keyword): # context是上下文对象,有参数的话,加上对应参数
|
||
context.keyword = keyword # 将参数绑定上下文对象,以便其他步骤使用
|
||
|
||
@when('打开百度页面')
|
||
def step_imp2(context):
|
||
context.driver = driver = webdriver.Firefox() # 同样绑定上下文对象
|
||
driver.implicitly_wait(10)
|
||
driver.get('https://www.baidu.com')
|
||
|
||
|
||
@then('输入关键词')
|
||
def step_imp3(context):
|
||
context.driver.find_element('id', 'kw').send_keys(context.keyword)
|
||
|
||
|
||
@then('点击百度一下按钮')
|
||
def step_imp4(context):
|
||
context.driver.find_element('id', 'su').click()
|
||
sleep(2)
|
||
|
||
|
||
@then('页面标题中应包含关键词')
|
||
def step_imp5(context):
|
||
assert context.keyword in context.driver.title
|
||
|
||
@then('关闭浏览器')
|
||
def step_imp6(context):
|
||
context.driver.quit()
|