Behave + Selenium(Python) 三
来自T先生
通过之前的2篇文章,大家都了解了如果利用behave和selenium打开网页和进行基本的操作,但是这些对于项目来说,却是往往不够的。
如果对junit或者TestNG熟悉的人都知道有@Before Class等这些在脚本之前完成的任务:如用户登入,以及当浏览器操作完,关闭浏览器等一些操作。那么问题来了,在behave里面是怎么控制的呢?
在behave中有个environment.py文件,environment.py文件可以很好的解决这个问题,除了解决这个问题,还可以用来解决同一条case可以在不同的浏览器运行的问题。
environment.py文件的介绍
environment.py文件定义了一些当测试脚本在run的过程中之前和之后完成的任务:
before_step(context, step), after_step(context, step)
在这里面的脚本会在每一个步骤之前,之后执行
before_scenario(context, scenario), after_scenario(context, scenario)
在这里面的脚本会在每一个场景之前,之后执行
before_feature(context, feature), after_feature(context, feature)
在这里面的脚本会在每一个feature之前,之后执行
before_tag(context, tag), after_feature(context, tag)
在脚本里面可以设置tag(这个之后会介绍),这里面的脚本会在含有tag的模块里面之前,之后执行
before_all(context), after_all(context)
这里面的脚本会在整个脚本开始之前,之后执行 ----- 一般会在这里面添加一些setup的脚本,例如启动浏览器,设置一些变量,连接数据库,关闭浏览器,关闭数据库 等等
那我们接下来来介绍一个简单的例子:
一、还是老规矩在feature文件夹里面新建example03文件夹,然后再新建example03.feature文件,除了这个文件之外,还需要新建environment.py文件:
from selenium import webdriver
import sys
def
before_all(context):
reload(sys)
sys.setdefaultencoding('utf-8')
context.driver =
webdriver.Firefox()
def after_all(context):
context.browser.close()
二、example03.feature文件的内容如下:
#../feature/example03/example03.feature
Feature:Search behave
results in baidu
Scenario: Search behave results in baidu
Given Access baidu website
When Input behave characters
Then There are more than 1 results displaying
三、在example03文件夹里面新建steps文件夹,然后在steps文件夹里面新建example03.py文件:
# This Python file uses the following encoding:
utf-8
#../feature/example03/steps/example03.py
from selenium import
webdriver
import time
@Given('Access baidu website')
def
step_impl(context):
context.driver.get("http://www.baidu.com")
@when('Input behave
characters')
def step_impl(context):
context.ele_input =
context.driver.find_element_by_xpath("//input[@id = 'kw']")
context.ele_input.send_keys("behave")
context.ele_btn =
context.driver.find_element_by_xpath("//input[@id = 'su']")
context.ele_btn.click()
time.sleep(10)
@Then('There are more
than 1 results displaying')
def step_impl(context):
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
这一章简单介绍environment.py文件的作用,这个文件的作用非常强大。
问题:
在我的脚本里面有个不好的地方是 time.sleep(10), 这个地方就是让线程停止10s. 其实这里应该的做法是写一个显示等待的脚本。 如下:
context.ele_results = WebDriverWait(context.driver, 60).until(
EC.presence_of_element_located((By.CSS_SELECTOR, "div.nums")))
如果出一些错误,那是因为你没有把包给加进去的原因, 把下面的代码加到程序的开头部分,就可以了。
from selenium.webdriver.support.ui import WebDriverWait
from
selenium.webdriver.support import expected_conditions
from
selenium.webdriver.common.by import By
Behave + Selenium(Python) 三的更多相关文章
- Behave + Selenium(Python) 四
来自T先生 今天我们开始讲讲behave的厉害的地方. Tag文件的使用 在behave里面,如何来控制哪些case需要run,哪些case不需要run,这个时候就用Tag来控制.好了,接下来我用Ta ...
- Behave + Selenium(Python) 二
介绍feature, py文件和之间关系: example01.feature文件包括5行: Feature行: 介绍这个feature用来干什么的: Scenario行:介绍这个scenario用来 ...
- Behave + Selenium(Python)一:
Behave 介绍:(来自T先生) 最近一个项目用了behave来做测试,因为之前没有接触过,所以写下最近的心得总结. 做自动化的人估计对selenium已经不是很陌生了,但是对于Behave工具,估 ...
- selenium+python环境的搭建的自动化测试
一.安装python: 我安装的是2.7.13版本的:可以在CMD下 运行python命令查看是否安装python,以及安装版本: 在https://www.python.org/getit/这个地址 ...
- selenium python 一些操作和定位收集
(—)滚动条操作 python中selenium操作下拉滚动条方法汇总 selenium_webdriver(python)控制浏览器滚动条 selenium+Python(select定位) Sel ...
- python爬虫积累(一)--------selenium+python+PhantomJS的使用(转)
阅读目录 一.Selenium介绍 二.爬虫为什么要用selenium? 三.PhantomJS介绍 四.PhantomJS安装 五.操作实战 六.在此推荐虫师博客的学习资料 selenium + p ...
- 自动化测试基础篇--Selenium Python环境搭建
学习selenium python需要的工具: 1.浏览器 2.Python 3.Selenium 4.FireBug(Firefox) 5.chromedriver.IEDriverServer.g ...
- python爬虫积累(一)--------selenium+python+PhantomJS的使用
最近按公司要求,爬取相关网站时,发现没有找到js包的地址,我就采用selenium来爬取信息,相关实战链接:python爬虫实战(一)--------中国作物种质信息网 一.Selenium介绍 Se ...
- 引用 自动化测试基础篇--Selenium Python环境搭建
原文链接:https://www.cnblogs.com/sanzangTst/p/7452922.html 鸣谢参藏法师. 学习selenium python需要的工具: 1.浏览器 2.Pytho ...
随机推荐
- Mysql 索引增加与删除
[1]索引 索引,通俗理解,即目录. 之前说过,计算机是对现实世界的模拟.目录应用在数据库领域,即所谓的索引. 目录的作用显而易见,所以建立索引可以大大提高检索的速度. 但是,会降低更新表的速度,如对 ...
- LR中select next row和update value on的设置
LR的参数的取值,和select next row和update value on的设置都有密不可分的关系.下表给出了select next row和update value on不同的设置,对于LR ...
- 【BZOJ3291】Alice与能源计划 二分图最大匹配
[BZOJ3291]Alice与能源计划 Description 在梦境中,Alice来到了火星.不知为何,转眼间Alice被任命为火星能源部长,并立刻面临着一个严峻的考验. 为了方便,我们可以将火星 ...
- Angular入门(一) 环境配置
angular/cli 安装 ♦ npm uninstall -g angular-cli /cnpm install -g angular-cli ※采用npm安装失败: Missing write ...
- computed 计算属性
wepyjs - 小程序组件化开发框架 https://tencent.github.io/wepy/document.html#/?id=wepy%e9%a1%b9%e7%9b%ae%e7%9a%8 ...
- 流畅python学习笔记:第十七章:并发处理二
本章讨论python3.2引入的concurrent.futures模块.future是中文名叫期物.期物是一种对象,表示异步执行的操作 在很多任务中,特别是处理网络I/O.需要使用并发,因为网络有很 ...
- 一起来学linux:网络配置
上网首先需要网卡的支持.在linux中默认的网卡为eth0, 第二张网卡为eth1.如果是用的无线网卡则是wlan0.这个可以通过ifconfig查看到.结果如下.其中lo代表本地端口.root@zh ...
- Windows 安装nginx
http://nginx.org/en/docs/windows.html 在nginx.exe目录,打开命令行工具,用命令 启动/关闭/重启nginx start nginx : 启动ngin ...
- 怎样拆分View Controller进而实现轻量级的View Controller[UIKit]
參考文章来自objcio站点 为什么要编写轻量级的View Controller?? 1.作为iOS项目中最大的文件,ViewControllers中的代码复用率差点儿是最低的 2.重量级的V ...
- Codeforces Round #243 (Div. 1)——Sereja and Two Sequences
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012476429/article/details/24798219 题目链接 题意:给两个长度分别 ...