lettuce_webdriver主要针对python语言编写UI自动化测试脚本。安装环境,可参考这篇文章,lettuce_webdriver自动化测试,这里,就不多说了。
用过selenium的人可能都知道,编写测试时,需要编写feature文件,以及steps文件。lettuce_webdirver的好处在于,作者已经在源码中集成了下面这些steps,当你在编写feature时,不需要再给这些步骤编写其对应的step文件,很是方便。lettuce_webdriver源码可参看https://github.com/bbangert/lettuce_webdriver/
# urls
I visit "http://google.com/"
I go to "http://google.com/"
# links
I click "Next page"
I should see a link with the url "http://foobar.com/"
I should see a link to "Google" with the url "http://google.com/"
I should see a link that contains the text "Foobar" and the url "http://foobar.com/"
# general
I should see "Page Content"
I see "Page Content"
I should see "Page Content" within 4 seconds
I should not see "Foobar"
I should be at "http://foobar.com/"
I should see an element with id of "http://bar.com/"
I should see an element with id of "http://bar.com/" within 2 seconds
I should not see an element with id of "http://bar.com/"
The element with id of "cs_PageModeContainer" contains "Read"
The element with id of "cs_BigDiv" does not contain "Write"
# browser
The browser's URL should be "http://bar.com/"
The browser's URL should contain "foo.com"
The browser's URL should not contain "bar.com"
# forms
I should see a form that goes to "http://bar.com/submit.html"
I press "Submit"
# checkboxes
I check "I have a car"
I uncheck "I have a bus"
The "I have a car" checkbox should be checked
The "I have a bus" checkbox should not be checked
# select
I select "Volvo" from "Car Choices"
I select the following from "Car Choices":
"""
Volvo
Saab
"""
The "Volvo" option from "Car Choices" should be selected
The following options from "Car Choices" should be selected:
"""
Volvo
Saab
"""
# radio buttons
I choose "Foobar"
The "Foobar" option should be chosen
The "Bar" option should not be chosen
# text entry fields (text, textarea, password)
I fill in "Username" with "Smith"
下面举一个实例,测试界面如下图:
观察该界面,首先看导航栏部分,导航栏共有“排名”,“注册”,“登录”三个超链接,主页部分,为一个登录框,需要输入用户名与密码,以及一个登录按钮和“忘记密码”的链接。接下来,我们对这个页面编写自动化测试脚本。
首先建立好目录:
features文件夹中为feature文件,step.py放在step_definitions文件夹中,support文件夹中,为terrain.py文件。
terrain.py:
from lettuce import before, world
from selenium import webdriver
import lettuce_webdriver.webdriver
@before.all
def setup_browser():
world.browser = webdriver.Chrome()
编写test.feature,放在feature文件夹下:
Feature: test
Scenario: 点击登录
Given I go to "http://127.0.0.1:5000/"
When I click "登录"
Then I should see "忘记密码" within 2 second
Scenario: 点击注册
Given I go to "http://127.0.0.1:5000/"
When I click "注册"
Then I should see "注册新用户" within 2 second
Scenario: 点击排名
Given I go to "http://127.0.0.1:5000/"
When I click "排名"
Then I should see "something" within 2 second
Scenario: login
Given I go to "http://127.0.0.1:5000/"
Given I click "登录"
When I fill in "用户名" with "test"
And I fill in "密码" with "12345455"
And I press "登录"
Then I should see "something" within 2 second
Then I click "注销"
Scenario: 忘记密码
Given I go to "http://127.0.0.1:5000/"
Given I click "登录"
When I click ".....忘记密码?"
Then I should see "something"
Then I close browser
上面的test.feature文件中,基本上,都是用的lettuce_webdriver作者已经写好的step,所以我们不想要再为它们编写step文件了。只有“I close browser”没有被定义,所以steps.py文件如下:
from lettuce import *
from lettuce_webdriver.util import assert_false
from lettuce_webdriver.util import AssertContextManager
@step('I close browser')
def close_browser(step):
world.browser.quit()
到这里,针对这个页面的自动化测试脚本就编写完成了。
在命令行输入lettuce运行即可,运行截图如下:
注意,由于feature文件中涉及到中文,由于编码问题,可能会报错,这时候,找到你安装的lettuce_webdriver,在lettuce_webdriver文件夹下的webdriver.py代码最前面加上如下代码即可:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
欢迎交流!