介绍feature, py文件和之间关系:

example01.feature文件包括5行: Feature行: 介绍这个feature用来干什么的; Scenario行:介绍这个scenario用来干什么的;Given: 一般数据的初始化在这里执行;When:执行操作;Then:验证结果。

example01.py文件包括@given, @when, @then. 具体步骤实现在每一个对应的步骤里面实现。

接下来我们使用selenium来启动firefox浏览器,做一些页面上的操作和验证。 --可以使用exclipse或者Notepad++工具来写代码

一、新建文件夹example02,在文件夹里面新建example02.feature文件:

#../feature/example02/example02.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

二、在example02文件夹里面新建steps文件夹,然后创建example02.py文件:

# This Python file uses the following encoding: utf-8
#../feature/example02/steps/example02.py

from selenium import webdriver

import time

import sys

@Given('Access baidu website')

def step_impl(context):

  reload(sys)

  sys.setdefaultencoding('utf-8') #设置python的编码方式为utf-8,它默认的是ACSII,
否则会报UnicodeDecodeError

  context.driver = webdriver.Firefox()

  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

三、打开cmd,cd到example02.feature所在的路径,然后输入behave, 结果运行正常:

你会发现第一次我运行失败,原因是没有设置python的默认编码方式。

问题解决:

  1. 当使用中文字符的时候,会出现  SyntaxError:
    Non-ASCII character '/xe6'
    错误,这个时候,在python语句的第一行加上  # This Python file uses
    the following encoding: utf-8 或者 #encoding: utf-8 即可以解决这个问题。以下为参考网址:

  http://blog.csdn.net/mayflowers/article/details/1568852

  https://www.python.org/dev/peps/pep-0263/

  1. 出现 UnicodeDecodeError:      'ascii' codec can't decode
    byte 0xe7 in position 0: ordinal not in     
    range(128):加入以下代码进去,即可以解决问题。

  import sys
  reload(sys)
  sys.setdefaultencoding('utf-8')

Behave + Selenium(Python) 二的更多相关文章

  1. Behave + Selenium(Python) 四

    来自T先生 今天我们开始讲讲behave的厉害的地方. Tag文件的使用 在behave里面,如何来控制哪些case需要run,哪些case不需要run,这个时候就用Tag来控制.好了,接下来我用Ta ...

  2. Behave + Selenium(Python) 三

    来自T先生 通过之前的2篇文章,大家都了解了如果利用behave和selenium打开网页和进行基本的操作,但是这些对于项目来说,却是往往不够的. 如果对junit或者TestNG熟悉的人都知道有@B ...

  3. Behave + Selenium(Python)一:

    Behave 介绍:(来自T先生) 最近一个项目用了behave来做测试,因为之前没有接触过,所以写下最近的心得总结. 做自动化的人估计对selenium已经不是很陌生了,但是对于Behave工具,估 ...

  4. Selenium自动化测试Python二:WebDriver基础

    WebDriver基础 欢迎阅读WebDriver基础讲义.本篇讲义将会重点介绍Selenium WebDriver的环境搭建和基本使用方法. WebDriver环境搭建 Selenium WebDr ...

  5. selenium + python自动化测试unittest框架学习(五)webdriver的二次封装

    因为webdriver的api方法很长,再加上大多数的定位方式是以xpath方式定位,更加让代码看起来超级长,为了使整体的代码看起来整洁,对webdriver进行封装,学习资料来源于虫师的<se ...

  6. python+selenium十:selenium的二次封装

    python+selenium十:基于原生selenium的二次封装   from selenium import webdriverfrom selenium.webdriver.support.w ...

  7. [译]Selenium Python文档:二、初步开始

    2.1.简单使用 如果已经安装好了Selenium Python,你就可以像下面这样编写Python代码来使用它了: from selenium import webdriver from selen ...

  8. python+selenium十:基于原生selenium的二次封装

    from selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium ...

  9. selenium + python自动化测试unittest框架学习(二)

    1.unittest单元测试框架文件结构 unittest是python单元测试框架之一,unittest测试框架的主要文件结构: File >report >all_case.py &g ...

随机推荐

  1. 循序渐进学Python 1 安装与入门

    1 安装 2 使用 2.1 运行程序 3 艺搜参考 by 2013年10月16日 安装 Windows安装版,源码,帮助文档: 使用 打开开始菜单中的Python GUI启动Python解释器: 启动 ...

  2. 进程间通信(IPC)+进程加锁解锁

    [0]README 0.1) source code and text description are from orange's implemention of a os: 0.2) for com ...

  3. 异常: 2 字节的 UTF-8 序列的字节 2 无效。

    具体异常: 十二月 08, 2015 7:16:55 下午 org.apache.catalina.core.StandardWrapperValve invoke 严重: Servlet.servi ...

  4. POJ 1163 The Triangle(经典问题教你彻底理解动归思想)

    The Triangle Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 38195   Accepted: 22946 De ...

  5. Hibernate连接池设置

    在公司第一次做项目放到服务器上测试,发现每隔一段时间不用数据库就连接不上了(以前学过连接池,很久没用就忘了),在myeclipse上的时候没发现,网上搜索才发现是hibernate连接池配置问题. 1 ...

  6. Java for LeetCode 112 Path Sum

    Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all ...

  7. BZOJ3295 [Cqoi2011]动态逆序对 —— CDQ分治

    题目链接:https://vjudge.net/problem/HYSBZ-3295 3295: [Cqoi2011]动态逆序对 Time Limit: 10 Sec  Memory Limit: 1 ...

  8. ubuntn14.04 使用 nvm创建多版本node环境

    1. 下载 nvm wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh | bash 2. 然后 ...

  9. Word怎么在方框里打勾

    插入--->符号--->其他符号--->在“子集”中选择“数学运算符”.第三行就可以找到“勾”符号,选中后点击“插入”即可. 或插入-->符号-->其他符号-->在 ...

  10. Linux-DHCP服务器的搭建

    DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理.分配IP地址,使网络环境中的主机动态的获得I ...