zoukankan      html  css  js  c++  java
  • Behave + Selenium(Python) ------ (第三篇)

    通过之前的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_tag(context, tag):
        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

  • 相关阅读:
    分布式id生成
    DB主从一致性架构优化4种方法
    Mysql在大型网站的应用架构演变
    win10下iis绑定局域网ip无效的解决方案
    css随笔
    html标签说明
    C# JSON序列化日期格式问题
    使用事件机制相比直接调用函数的优势
    C#绑定事件时使用匿名函数
    C# t4模版引擎笔记 引入外部dll
  • 原文地址:https://www.cnblogs.com/tman/p/4117658.html
Copyright © 2011-2022 走看看