二、在example04文件夹里面新建example04.feature文件,代码如下:
# This Python file uses the following encoding: utf-8
#../feature/example04/steps/example04.py
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.common.by import By
@Then('There are more than 1 results displaying')
def step_impl(context):
context.sign_link = WebDriverWait(context.driver, 60).until(
expected_conditions.presence_of_element_located((By.CSS_SELECTOR, "div.nums")))
context.ele_results = context.driver.find_element_by_css_selector("div.nums")
context.expected_results = '相关结果'
if context.expected_results in context.ele_results.text:
assert True
else:
assert False
三、在example04文件夹里面新建steps文件夹,然后在steps文件夹里面新建example04.py, 代码如下:
Feature:Search behave results in baidu
@browser.firefox
Scenario: Search behave results in baidu with firefox browser
Given Access baidu website
When Input behave characters
Then There are more than 1 results displaying
@browser.chrome
Scenario: Search behave results in baidu with chrome browser
Given Access baidu website
When Input behave characters
Then There are more than 1 results displaying
@browser.ie
Scenario: Search behave results in baidu with ie browser
Given Access baidu website
When Input behave characters
Then There are more than 1 results displaying
四、打开cmd,cd到相应的路径,然后输入behave,你会发现代码分别会在firefox, chrome和IE中分别执行。 但是如果你想要只在firefox中执行,你可以使用命令 behave --tags=browser.firefox,然后你会发现仅仅打开了firefox。