zoukankan      html  css  js  c++  java
  • ruby+seleniumwebdriver一步一步完成自动化测试(4)—–逻辑与数据分离

    上一篇面向对象编辑,貌似这个用例已经很完美,但是还有不完善的地方,测试数据(用户名、密码、断言的判断数据)还是硬编码,要想做的更完美,实现逻辑与数据分离,这才是自动化测试的真谛。

    方法:运用yaml,读取配置文件中测试数据

    1.D盘新建文件夹test004,test004文件夹下创建文件夹action、config、spec、tool。

    action文件夹中新建文件login_main_page.rb

    config文件夹中新建文件login_data.yml

    spec文件夹中新建文件login_spec.rb

    tool文件夹中新建文件login_dialog.rb

    2.login_dialog.rb文件内容与上一篇“面向对象编程”中login_dialog.rb内容一致

    3.login_main_page.rb文件内容与上一篇“面向对象编程”中login_main_page.rb内容一致

    4.login_data.yml文件中编写如下内容:

    data:
      mainPage:
        url: http://www.soso.com
        title: 搜搜更懂你  
    
      login:
        wrong:
          username: test
          password: test
          message: 您输入的帐号或密码不正确,请重新输入。意见反馈
    

    5.login_spec.rb文件中编写如下内容:

    #encoding: utf-8
    require "rspec"
    require 'yaml'
    require 'selenium-webdriver'  
    
    require File.dirname(__FILE__)+'/../action/login_main_page'
    require File.dirname(__FILE__)+'/../tool/login_dialog'  
    
    describe "soso login" do
      include LoginDialog
      before(:all) do
        @problem=YAML.load(File.open(File.dirname(__FILE__)+'/../config/login_data.yml'))
        @dr=Selenium::WebDriver.for :firefox
        @url=@problem["data"]["mainPage"]["url"]
        #@url='http://www.soso.com'
        @dr.get @url
      end
      before(:each) do
        @login_element=LoginMainPage.new(@dr)
      end
      after(:each) do
          close_browser
      end 
    
      it "should return username and password is wrong" do
        @login_element.login(@problem["data"]["login"]["wrong"]["username"],@problem["data"]["login"]["wrong"]["password"])
        err_message.should eql (@problem["data"]["login"]["wrong"]["message"])
      end
    end
    

      

    6.命令行运行命令:

    cd d:\test004

    rspec -f doc

    得到如下结果:

    soso login
      should return username and password is wrong  
    
    Finished in 9.23 seconds
    1 example, 0 failures
    

      运行通过

  • 相关阅读:
    poj 2676 Suduku (dfs)
    poj 1562 Oil Deposits (dfs)
    poj 2907 Collecting Beepers (dfs)
    poj 1655 Balancing Act (树形dfs)
    poj 3411 Paid Roads (dfs)
    hdu 2896 病毒侵袭 (AC)
    hdu 3065 病毒侵袭持续中 (AC)
    poj 2251 Dungeon Master (bfs)
    java中debug使用
    Swing入门级小项目总结
  • 原文地址:https://www.cnblogs.com/timsheng/p/2682643.html
Copyright © 2011-2022 走看看