介绍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. openssl源码安装

    下载最新的OpenSSL http://openssl.org/source/ ./config    make  make install 通过命令openssl version或者openssl ...

  2. Spring cloud微服务实战——基于OAUTH2.0统一认证授权的微服务基础架构

    https://blog.csdn.net/w1054993544/article/details/78932614

  3. Javaweb之 servlet 开发具体解释1

    1.1  Tip:Servlet简单介绍 Servlet是sun公司提供的一门用于开发动态web资源的技术. Sun公司在其API中提供了一个servlet接口,用户若想用发一个动态web资源(即开发 ...

  4. Windows系统下正确安装MongoDB

    1.下载.安装 官网下载: http://www.mongodb.org/downloads 下载好之后,接下来进行安装了: 2.创建数据文件夹 MongoDB将数据文件夹存储在 db 文件夹下. 可 ...

  5. Windows 驱动入门(二)代码结构

    windows驱动程序基础.转载标明出处:http://blog.csdn.net/ikerpeng/article/details/38777641 windows驱动程序结构: 我想说的是wind ...

  6. 总是想把Linux服务器上的重要文件备份到本地,在此转一篇实现windows和linux互传文件的文章

    尝试从windows xp向ubuntu11.10传文件 ubuntu使用的是ssh windows使用的是putty和其附带的pscp 首先配置ubuntu: 1.先使用netstat -tl或se ...

  7. VC项目文件说明

    .dsp   项目参数配置文件,这个文件太重要,重点保护对象.. .dsw   工作区文件,重要性一般,因为它信息不我,容易恢复.   以下文件在项目中是可丢弃的,有些文件删除后,VC会自动生成的. ...

  8. 一起来学linux:SAMBA服务器搭建

    前面介绍的NFS服务器的用来linux和linux系统之间共享文件和目录的,那如果是linux和windows之间需要共享修改文件该如何操作呢.这据需要用到SAMBA系统.我们首先来看下SAMBA系统 ...

  9. Java for LeetCode 134 Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  10. px sp dp 手机尺寸