码迷,mamicode.com
首页 > 其他好文 > 详细

lettuce行为驱动框架实例

时间:2018-07-18 21:48:06      阅读:691      评论:0      收藏:0      [点我收藏+]

标签:批量   find   print   which   编程   start   app   并且   visit   

练习:
一:e1_MyFirstBDD 使用方法的方式实现阶乘的计算
zero.feature:
Feature: Compute factorial
  In order to play with Lettuce
  As beginners
  Well implement factorial

  Scenario: Factorial of 0
    Given I have the number 0
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 1
    Given I have the number 1
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 2
    Given I have the number 2
    When I compute its factorial
    Then I see the number 2

  Scenario: Factorial of 3
    Given I have the number 3
    When I compute its factorial
    Then I see the number 6

steps.py:
#encoding=utf-8
from lettuce import *

# 用于计算整数的阶乘函数
def factorial(number):
    number = int(number)
    if (number == 0) or (number == 1):
        return 1
    else:
        return reduce(lambda x, y: x * y, range(1, number + 1))

#取出数据
@step(I have the number (\d+))
def have_the_number(step, number):
    # 将通过正则表达式匹配的数字存于全局变量world中
    #world是公用的,不能变
    #step对应的是I have the number,number参数名字是自定义的
    world.number = int(number)


#用测试数据调用测试方法
@step(I compute its factorial)
def compute_its_factorial(step):
    # 从全局变量world中取出匹配的数字,
    # 计算其阶乘,并将结果再存回world中
    world.number = factorial(world.number)

#断言实际结果和期望结果是否一致
@step(I see the number (\d+))
def check_number(step, expected):
    # 通过正则匹配到预期数字
    expected = int(expected)
    # 断言计算阶乘结果是否等于预期
    assert world.number == expected, "Got %d" %world.number
二:e2_MyFirstBDD 使用类的方式实现阶乘的计算
steps.py:
#encoding=utf-8
#这个例子是通过类的方式实现的
from lettuce import world, steps

def factorial(number):
  number = int(number)
  if (number == 0) or (number == 1):
    return 1
  else:
      return reduce(lambda x, y: x * y, range(1, number + 1))

@steps
class FactorialSteps(object):
    """Methods in exclude or starting with _ will not be considered as step"""
    #定义哪些方法不需要执行,排除
    exclude = [set_number, get_number]

    def __init__(self, environs):
        # 初始全局变量
        self.environs = environs

    def set_number(self, value):
        # 设置全局变量中的number变量的值
        # 实际上self.environs=world
        self.environs.number = int(value)

    def get_number(self):
        # 从全局变量中取出number的值
        return self.environs.number

    #私有方法默认也不是测试步骤
    def _assert_number_is(self, expected, msg="Got %d"):
        number = self.get_number()
        # 断言
        assert number == expected, msg % number

    def have_the_number(self, step, number):
        ‘‘‘I have the number (\d+)‘‘‘
        # 上面的三引号引起的代码必须写,并且必须是三引号引起
        # 表示从场景步骤中获取需要的数据
        # 并将获得数据存到环境变量number中
        self.set_number(number)

    def i_compute_its_factorial(self, step):
        """When I compute its factorial"""
        number = self.get_number()
        # 调用factorial方法进行阶乘结算,
        # 并将结算结果存于全局变量中的number中
        self.set_number(factorial(number))

    def check_number(self, step, expected):
        ‘‘‘I see the number (\d+)‘‘‘
        # 上面的三引号引起的代码必须写,并且必须是三引号引起
        # 表示从场景步骤中获取需要的数据以便断言测试结果
        self._assert_number_is(int(expected))

#world可以理解为一个类,存全局共享变量的对象
FactorialSteps(world)
三:e3_StepDataTables 基于表格的方式实现查找出名字以"G"开头的学生
student.feature
Feature: bill students alphabetically
    In order to bill students properly
    As a financial specialist
    I want to bill those which name starts with some letter

    Scenario: Bill students which name starts with "G"
    Given I have the following students in my database:
        | name     | monthly_due | billed |
        | Anton    | $ 500       | no     |
        | Jack     | $ 400       | no     |
        | Gabriel  | $ 300       | no     |
        | Gloria   | $ 442.65    | no     |
        | Ken      | $ 907.86    | no     |
        | Leonard  | $ 742.84    | no     |
    When I bill names starting with "G"
    Then I see those billed students:
        | name     | monthly_due | billed |
        | Gabriel  | $ 300       | no     |
        | Gloria   | $ 442.65    | no     |
    And those that werent:
        | name     | monthly_due | billed |
        | Anton    | $ 500       | no     |
        | Jack     | $ 400       | no     |
        | Ken      | $ 907.86    | no     |
        | Leonard  | $ 742.84    | no     |

step.py:
#encoding=utf-8
from lettuce import *

#基于表格数据来做测试
@step(I have the following students in my database:)
def students_in_database(step):
    #获取自然语言写的表格数据,用哈希的方法,返回的是个列表
    if step.hashes:
        # 如果存在步骤表格数据,则继续后续步骤
        print type(step.hashes)
        assert step.hashes == [
            {
                name: Anton,
                monthly_due: $ 500,
                billed: no
            },
            {
                name: Jack,
                monthly_due: $ 400,
                billed: no
            },
            {
                name: Gabriel,
                monthly_due: $ 300,
                billed: no
            },
            {
                name: Gloria,
                monthly_due: $ 442.65,
                billed: no
            },
            {
                name: Ken,
                monthly_due: $ 907.86,
                billed: no
            },
            {
                name: Leonard,
                monthly_due: $ 742.84,
                billed: no
            },
        ]

#通过正则获取到的(.*)这个分组,给参数startAlpha
@step(I bill names starting with "(.*)")
def match_starting(step, startAlpha):
    # 将通过正则表达式匹配步骤中最后一个字母,
    # 并存于全局变量startAlpha中
    world.startAlpha = startAlpha
    print "no data exist:",step.hashes
    print "1"

@step(I see those billed students:)
def get_starting_with_G_student(step):
    #遍历步骤数据表中的数据
    for i in step.hashes:
        # 断言学生的名字是否以world.startAlpha变量存取的字母开头?
        assert i["name"].startswith(world.startAlpha)

@step("those that weren‘t:")
def result(step):
    for j in step.hashes:
        # 断言学生名字不以world.startAlpha变量存取的的字母开头?
        assert world.startAlpha not in j["name"][0]
四:e4_English_example 英文模板
sogou.feature
Feature: Search in Sogou website
  In order to Search in Sogou  website
  As a visitor
  Well search the NBA best player

  Scenario: Search NBA player
    Given I have the english name "<search_name>"
    When  I search it in Sogou website
    Then  I see the entire name "<search_result>"

  Examples:
    | search_name | search_result |
    | Jordan      | Michael       |
    | Curry       | Stephen       |
    | Kobe        | Bryant        |


sogou.py:
#encoding=utf-8
from lettuce import *
from selenium import webdriver
import time

@step(I have the english name "(.*)")
def have_the_searchWord(step, searchWord):
    world.searchWord = str(searchWord)
    print world.searchWord

@step(I search it in Sogou website)
def search_in_sogou_website(step):
    world.driver = webdriver.Ie(executable_path = "c:\\IEDriverServer")
    world.driver.get("http://www.sogou.com")
    world.driver.find_element_by_id("query").send_keys(world.searchWord)
    world.driver.find_element_by_id("stb").click()
    time.sleep(3)

@step(I see the entire name "(.*)")
def check_result_in_sogou(step, searchResult):
    assert searchResult in world.driver.page_source, "got word:%s" %searchResult
    world.driver.quit()

五:e5_Chinese_example 中文模板
baidu.feature:
#encoding=utf-8
# language: zh-CN

特性: 在百度网址搜索IT相关书籍
    能够搜索到书的作者,比如吴晓华

    场景: 在百度网站搜索IT相关书籍
        如果将搜索词设定为书的名字"<书名>"
        当打开百度网站
        和在搜索输入框中输入搜索的关键词,并点击搜索按钮后
        那么在搜索结果中可以看到书的作者"<作者>"

    例如:
        | 书名                         | 作者   |
        | Selenium WebDriver实战宝典   | 吴晓华 |
        | HTTP权威指南                 | 协议 |
        | Python核心编程               | Python |

baidu.py:
#encoding=utf-8
# language: zh-CN
from lettuce import *
from selenium import webdriver
import time

@step(u将搜索词设定为书的名字"(.*)")
def have_the_searchWord(step, searchWord):
    world.searchWord = searchWord
    print world.searchWord

@step(u打开百度网站)
def visit_baidu_website(step):
    world.driver = webdriver.Ie(executable_path = "c:\\IEDriverServer")
    world.driver.get("http://www.baidu.com")

@step(u在搜索输入框中输入搜索的关键词,并点击搜索按钮后)
def search_in_sogou_website(step):
    world.driver.find_element_by_id("kw").send_keys(world.searchWord)
    world.driver.find_element_by_id("su").click()
    time.sleep(3)

@step(u在搜索结果中可以看到书的作者"(.*)")
def check_result_in_sogou(step, searchResult):
    assert searchResult in world.driver.page_source, "got word:%s" %searchResult
    world.driver.quit()

六:e6_executed_by_batch 批量执行
Login_Chinese.feature:
#encoding=utf-8
# language: zh-CN

特性: 登录126邮箱和退出126邮箱登录

    场景: 成功登录126邮箱
        假如启动一个浏览器
        当用户访问http://mail.126.com网址
        当用户输入输入用户名“testman1980”和密码“wulaoshi1978”
        那么页面会出现“未读邮件”关键字

    场景: 成功退出126邮箱
        当用户从页面单击退出链接
        那么页面显示“您已成功退出网易邮箱”关键内容

Login_Chinese.py:
#encoding=utf-8
# language: zh-CN
from lettuce import *
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

@step(u启动一个浏览器)
def open_browser(step):
    try:
        # 创建Chrome浏览器的driver实例,并存于全局对象world中,
        # 供后续场景或步骤函数使用
        world.driver = webdriver.Ie(executable_path = "c:\\IEDriverServer")
    except Exception, e:
        raise e

@step(u用户访问(.*)网址)
def visit_url(step, url):
    print url
    world.driver.get(url)

@step(u用户输入输入用户名“(.*)”和密码“(.*)”)
def user_enters_UserName_and_Password(step, username, password):
    print username, password
    # 浏览器窗口最大化
    world.driver.maximize_window()
    time.sleep(3)
    # 切换进frame控件
    world.driver.switch_to.frame("x-URS-iframe")
    # 获取用户名输入框
    userName = world.driver.find_element_by_xpath(//input[@name="email"])
    userName.clear()
    # 输入用户名
    userName.send_keys(username)
    # 获取密码输入框
    pwd = world.driver.find_element_by_xpath("//input[@name=‘password‘]")
    # 输入密码
    pwd.send_keys(password)
    # 发送一个回车键
    pwd.send_keys(Keys.RETURN)
    # 等待15秒,以便登录后成功进入登录后的页面
    time.sleep(15)

@step(u页面会出现“(.*)”关键字)
def message_displayed_Login_Successfully(step, keywords):
    # print world.driver.page_source.encode(‘utf-8‘)
    # 断言登录成功后,页面是否出现预期的关键字
    assert keywords in world.driver.page_source
    # 断言成功后,打印登录成功信息
    print "Login Success"

@step(u用户从页面单击退出链接)
def LogOut_from_the_Application(step):
    print "====",world.driver
    # time.sleep(5)
    # 点击退出按钮,退出登录
    world.driver.find_element_by_link_text(u"退出").click()
    time.sleep(8)

@step(u页面显示“(.*)”关键内容)
def displayed_LogOut_Successfully(step, keywords):
    # 断言退出登录后,页面是否出现退出成功关键内容
    assert keywords in world.driver.page_source
    print u"Logout Success"
    # 退出浏览器
    world.driver.quit()

七:e7_example 行为驱动实现1+2=3
zero.feature:
Feature: Compute factorial
  In order to play with Lettuce
  As beginners
  Well implement factorial

  Scenario: Factorial of 1
    Given I have the number 1,2
    When I compute its factorial
    Then I see the number 3

step.py:
#encoding=utf-8
from lettuce import *

# 用于计算整数的和函数
def sum(number1,number2):
    return int(number1+number2)

@step(I have the number (\d),(\d+))
def have_the_number(step, number1,number2):
    # 将通过正则表达式匹配的数字存于全局变量world中
    world.number1 = int(number1)
    world.number2 = int(number2)

@step(I compute its factorial)
def compute_its_factorial(step):
    # 从全局变量world中取出匹配的数字,
    # 计算其阶乘,并将结果再存回world中
    world.sum = sum(world.number1,world.number2)

@step(I see the number (\d+))
def check_number(step, expected):
    # 通过正则匹配到预期数字
    expected = int(expected)
    # 断言计算阶乘结果是否等于预期
    assert world.sum == expected, "Got %d" %world.number

 

lettuce行为驱动框架实例

标签:批量   find   print   which   编程   start   app   并且   visit   

原文地址:https://www.cnblogs.com/jingsheng99/p/9332554.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!