新建一个features文件夹,文件夹中包含三个子文件,baidu.feature,baidu.py,terrain.py
baidu.feature
# Created by 郑晓伟 at 2019/11/19
Feature: Search in baidu website
In order to Search in baidu website
As a visitor
w'll search the NBA player
Scenario: Search NBA player
Given I have the english name "<search_name>"
When I search it in baidu website
Then I see the entire name "<search_result>"
Examples:
|search_name|search_result|
|Jordan |Michael |
|Curry |Stephen |
|Kobe |Bryant |
baidu.py
# -*- coding:UTF-8 -*-
from lettuce import *
from selenium import webdriver
import time
@step('I have the english name "(.*)"')
def have_the_searchname(step,searchname):
world.searchname=searchname
@step('I search it in baidu website')
def search_in_baidu_website(step):
world.driver=webdriver.Firefox(executable_path="C:\\webdriver\geckodriver.exe")
world.driver.get("http://www.baidu.com")
world.driver.find_element_by_id("kw").send_keys(world.searchname)
world.driver.find_element_by_id("su").click()
time.sleep(3)
@step('I see the entire name "(.*)"')
def check_result_in_baidu(step,searchresult):
assert searchresult in world.driver.page_source,"got word %s" %searchresult
world.driver.quit()
terrain.py 输出日志的文件
# -*- coding:UTF-8 -*-
from lettuce import *
import os
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
datafmt='%a, %Y-%m-%d %H:%M:%S',
filename='D:/test.log',
filemode='w'
)
@before.all
def say_hello():
logging.info(u'开始打印日记')
print u'开始打印日记'
@before.each_scenario
def setup_scenario(scenario):
print 'Begin to execute scenario name:' + scenario.name
logging.info('Begin to execute scenario name:' + scenario.name)
@before.each_step
def setup_step(step):
run="running step %r , defined at %s" %(step.sentence,step.defined_at.file)
print run
logging.info(run)
@after.each_step
def teardown_step(step):
end="End of the '%s'"%step.sentence
print end
logging.info(end)
@after.each_scenario
def teardown_scenario(scenario):
print 'finished:' + scenario.name
logging.info('finished:' + scenario.name)
@after.all
def say_goodbye(total):
result="Congratulations,%d of %d scenarios passed!"%(total.scenarios_ran,total.scenarios_passed)
print result
logging.info(result)
logging.info(u'脚本执行完毕')
print u'脚本执行完毕'