behave-web-demo/features/steps/web_test.py

29 lines
950 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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_impl(context):
context.driver = driver = webdriver.Chrome() # 同样绑定上下文对象
driver.implicitly_wait(10)
driver.get('https://www.baidu.com')
@when('输入关键词')
def step_impl(context):
context.driver.find_element('id', 'kw').send_keys(context.keyword)
@when('点击百度一下按钮')
def step_impl(context):
context.driver.find_element('id', 'su').click()
sleep(0.5)
@then('页面标题中应包含关键词')
def step_impl(context):
assert context.keyword in context.driver.title