Behave + Selenium (Python)------(fourth)

Source: Internet
Author: User

Recently busy, behave project ended, and then joined a new project, at the outset to use the C # language to write Selenium automation, later with the client to determine the use of QTP to write. Individuals still prefer to use the C # language and selenium framework. Because of the QTP, the market is not used much, the development prospects are limited, and the VBScript language is now basically no one to use. OK, start entering the topic:

Today we begin to talk about the behave of the place.

Use of the tag file

In behave, how to control which cases need to run, which case does not need to run, this time with tag to control. OK, next I use the tag file to implement the same script can be used firefox,chrome and IE three different browsers to test.

First, create a new example04 file inside the feature file, and then create a new environment.py file with the following code:

From selenium import Webdriver
Import Sys

def before_all (context):
Reload (SYS)
Sys.setdefaultencoding (' Utf-8 ')
Context.baidu_url = ' http://www.baidu.com '

def before_tag (context, TAG):
If Tag.startswith ("browser."):
Browser_type = Tag.replace ("browser.", "", 1)
if Browser_type = = "Firefox":
Context.driver = Webdriver. Firefox ()
elif Browser_type = = "Chrome":
Context.driver = Webdriver. Chrome ()
Else
Context.driver = Webdriver. Ie ()

def after_tag (context, TAG):
Context.driver.close ()

Second, in the Example04 folder inside the new Example04.feature file, the code is as follows:

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

From selenium import Webdriver
From Selenium.webdriver.support.ui import webdriverwait
From Selenium.webdriver.support import expected_conditions
From selenium.webdriver.common.by Import by



@Given (' Access Baidu website ')
def step_impl (context):
Context.driver.get (Context.baidu_url)
@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 ()

@Then (' There is more than 1 results displaying ')
def step_impl (context):

Context.sign_link = webdriverwait (Context.driver). Until (
Expected_conditions.presence_of_element_located ((By.css_selector, "Div.nums")))
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

Third, in the Example04 folder inside the new steps folder, and then in the steps folder inside the new example04.py, the code is as follows:

Feature:search behave results in Baidu

@browser. Firefox
Scenario:search behave results in Baidu with Firefox browser
Given Access Baidu website
When Input behave characters
Then there is more than 1 results displaying

@browser. Chrome
Scenario:search behave results in Baidu with Chrome browser
Given Access Baidu website
When Input behave characters
Then there is more than 1 results displaying

@browser. ie
Scenario:search behave results in Baidu with IE browser
Given Access Baidu website
When Input behave characters
Then there is more than 1 results displaying

Open the CMD,CD to the appropriate path, and then enter behave, and you will find that the code will be executed separately in Firefox, Chrome, and IE. But if you want to do it only in Firefox, you can use the command behave--tags=browser.firefox, and then you'll find that Firefox is just turned on.

Extension: Through the tag, you can give a scenario plus n tags, each tag with a space between the OK, so you can also give scenario plus regression make it into regression test.

In addition to scenario tag, you can also add tag to feature. Behave script will put all the feature files in the same feature folder directory, and then the steps inside all the feature files corresponding to the py file.

Then the final base directory is like this.

Behave + Selenium (Python)------(fourth)

Related 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.