Behave + Selenium (Python)------(second article)

Source: Internet
Author: User

Describes feature, py files, and relationships between:

The Example01.feature file consists of 5 lines: feature: Describes what this feature is used for; Scenario Line: Introduce what this scenario is used for; Given: Initialization of general data is performed here; When: performing actions ; then: Verify the results.

example01.py files include @given, @when, @then. The specific steps are implemented in each of the corresponding steps.

Next we use the selenium to launch the Firefox browser, do some work on the page and verify. --You can use Exclipse or notepad++ tools to write code

First, create a new folder Example02, create a new Example02.feature file inside the folder:

#.. /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 is more than 1 results displaying

Second, create a new steps folder inside the Example02 folder, and then creating the example02.py file:

# 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, it defaults to acsii, otherwise it will be reported 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 is more than 1 results displaying ') def step_impl (context):

Context.ele_results = Context.driver.find_element_by_css_selector ("Div.nums")

Context.expected_results = ' Related results '

If Context.expected_results in Context.ele_results.text:

Assert True

Else

Assert False

Three, open CMD,CD to example02.feature the path, and then enter behave, the results run normally:

You will find that for the first time I failed to run because the default encoding for Python was not set.

Problem solving:

    1. Syntaxerror:non-ascii character '/xe6 ' error occurs when using Chinese characters, this time, the first line of the Python statement plus the # This Python file uses the following enc Oding:utf-8 or #encoding: Utf-8 can solve this problem. The following are the reference URLs:

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

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

    1. Appears unicodedecodeerror: ' ASCII ' codec can ' t decode byte 0xe7 in position 0:ordinal not in range (128): Add the following code to To solve the problem.

Import sys
Reload(sys)
SYS. Setdefaultencoding (' utf-8 ')

Behave + Selenium (Python)------(second article)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.