Cucumber + Watir webdriver + Ruby 功能自動化測試

因爲工作需要,這段時間在使用Cucumber + Watir webdriver + Ruby做網站的功能自動化測試。

第一步:搭建環境

A。安裝ruby

下載ruby的exe安裝文件並安裝

下載ruby的devkit文件並安裝

B。打開安裝目錄下的ruby》start command prompt with ruby窗口

安裝cucumber

gem install cucumber

確定否安裝成功cucumber -v,顯示出cucumber的版本號時表明安裝成功

安裝Watir webdriver

gem install watir-webdriver

gem install rspec

第二步:構造cucumber運行目錄

auto

|----features

|-----∟----Define_steps

|-----|-------------∟---defined_steps.rb(對feature文件進行解析,並調用對應的Actions.rb中的方法)

|-----∟----aaa.feature(測試用例,數量不限)

|----lib

|-----∟----Actions.rb(方法庫)


詳細介紹:

測試用例*.feature文件,用語言描述出測試用例的具體步驟,如

# language: zh-CN
功能: 登錄管理
場景: 使用錯誤的用戶名或密碼登錄會報錯
假如進入51cto首頁
並且點擊登錄鏈接
而且以錯誤帳號abc或錯誤密碼111111登錄
那麼看到出錯的文字信息

defined_steps.rb文件,對*.feature文件中的文字進行解析並調用Actions.rb文件中對應的方法,如

#encoding=UTF-8
#調用需要用到的庫
begin require 'rspec/expectations'; rescue LoadError; require 'spec/expectations'; end
require 'cucumber/formatter/unicode'
require 'watir-webdriver'
require 'watir-webdriver-performance'
#調用Actions.rb文件
$:.unshift(File.dirname(__FILE__) + '/../../lib')
require 'Actions'
#執行用例前需要做的工作
Before do
#啓動谷歌瀏覽器
$browser=Watir::Browser.new:chrome
#將Actions.rb方法爲載入內存
$autotest=Actions.new
end
#執行測試用例後需要做的工作
After do
#關閉瀏覽器
$browser.close
end
# 假如,而且
Given /^進入51cto首頁$/ do ||
$autotest.navigate_to()
end
Given /^以錯誤帳號(.*)或錯誤密碼(.*)登錄$/ do |username, password|
$autotest.invalid_login_action(username, password)
end
Given /^點擊(.*)鏈接$/ do |link_name|
$autotest.click_link(link_name)
end
# 當
When /判斷預期結果時/ do ||
sleep(3)
end
# 那麼,並且
Then /^看到(.*)文字信息$/ do |information|
$autotest.display_text(information)
end


Actions.rb文件,測試用例執行時的方法庫,如

#encoding=UTF-8
require 'watir-webdriver'
$LOAD_PATH << File.dirname(__FILE__)
class Actions
#跳轉頁面
def navigate_to()
#進入到社區首頁
$browser.goto('http://www.51cto.com/')
#確認出現登錄鏈接,來判斷頁面加載完畢
$browser.link(:text, '登錄').wait_until_present
$browser.link(:text, '登錄').flash
end
#錯誤用戶名和密碼登錄
def invalid_login_action(username, password)
#判讀用戶名輸入域顯示
$browser.text_field(:name, 'email').wait_until_present
#輸入用戶名
$browser.text_field(:name, 'email').send_keys(username)
#判斷用戶密碼域顯示
$browser.text_field(:name, 'passwd').wait_until_present
#輸入用戶密碼
$browser.text_field(:name, 'passwd').send_keys(password)
#判斷登錄鍵顯示
$browser.button(:name, 'button').wait_until_present
#點擊登錄鍵
$browser.button(:name, 'button').click
end
#點擊鏈接
def click_link(link_name)
$browser.link(:text, link_name).wait_until_present
$browser.link(:text, link_name).click
sleep(1)
end
#判斷內容是否在頁面上顯示
def display_text(information)
$browser.span(:style, 'color:#FF0000;').wait_until_present
$browser.span(:style, 'color:#FF0000;').flash
end
end


第三步:執行測試用例

1、打開命令行窗口,進入到feature文件的上級目錄

2、輸入cucumber\tfeature\xxx.feature按下回車後,用例即開始執行

142337861.png

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章