July 5
——————————————————————————————————
环境安装:
参考文章
踩坑:ide安了ruby插件也不能建ruby文件,找不到SDK,于是安了个windows visual code,结果这玩意一run就报错,最后被迫用terminal终端ruby xxx.rb执行
反正是搞好了,不想太耽误时间在这上面,回头玩利索了再换工具
原来ruby的sdk就是ruby.exe,转战idea了
先gem一波环境
gem install json
gem install httparty
gem install cucumber
gem install rspec
gem install watir-webdriver
gem install selenium-client
gem install websocket
windows下加颜色:word/ansi/x64
ansicon -i 安装(没有反馈)
cucumber初始化和测试项目初始化
运行单个
rule.feature
# features/rule.feature
Feature: Rule Sample
Rule: This is a rule
Example: A passing example
Given this will pass
When I do an action
Then some results should be there
Example: A failing example
Given this will fail
When I do an action
Then some results should be there
运行命令
cucumber rule.feature
保存html测试结果
cucumber --format summary --format html --out report.html
一直提示没有功能undefined
研究好久,原来是因为cucumber没找到step文件,要手动指定一下路径
cucumber -r features/step_definitions/ features/printer.feature
然后报了定义重复ambiguous match of
删掉一个匹配就行
然后报了匹配必须是正则表达式或字符串,改
Expression must be a String or Regexp (ArgumentError)
终于pass了
计算器
参考
Calculator.rb
class Calculator
def push(n) #记数
@args ||= [] #初始化空数组
@args << n
end
def sum() #返回全部数字和
sum = 0
@args.each do |i|
sum += i
end
@result = sum
return sum
end
def result
@result
end
end
踩坑:
sum没有参数,括号删不删都行,也就是个warning
cal.feature
Feature: Addition
Scenario: Add two numbers
Given i have a calculator
And i have entered 50 into the calculator
And i have entered 70 into the calculator
When i press add
Then the result should be 120 on the screen
cal.rb
Given /^i have a calculator$/ do
@c = Calculator.new
end
When /^i have entered (\d+) into the calculator$/ do |num|
puts "num:#{num}"
@c.push(num.to_i)
end
When /^i press add$/ do
puts "sum:#{@c.sum()}"
end
Then /^the result should be (\d+) on the screen$/ do |result|
#raise "expect"+result+"in fact"+sum
@c.result.should == result.to_i
end
一开始给的是
Given /^我有一个计算器$/ do
@c = Calculator.new
end
Given /^我向计算器输入(\d+)$/ do |num|
@c.push(num.to_i)
end
When /^我点击累加$/ do
@c.sum
end
Then /^我应该看到结果(\d+)$/ do |result|
@c.result.should == result.to_i
end
所以只是因为sum后面少了方法的括号所以一直过不去
全修好跑通了
把Calculator.rb里sum+=i的加号去了再测
July 6
——————————————————————————————————
Friday
rb
Given /Today is (\w+)/ do |str|
@str=str
end
When /I ask if its Friday/ do
if @str=='Friday' then
@result="yes"
else @result='no'
end
end
Then /I should be told (\w+)/ do |res|
@result.should==res
end
feature
Feature:Another test
Scenario: not Friday
Given Today is Sunday
When I ask if its Friday
Then I should be told no
Scenario: is Friday
Given Today is Friday
When I ask if its Friday
Then I should be told yes
idea的不知道为啥有问题就是跑不通中文测试
换到git就好了
访问网页
require 'watir'
ie = Watir::Browser.new
ie.goto 'www.baidu.com'
遇到问题如下:
Browser默认启动的是chrome,如果要启动其他的浏览器
ie = Watir::Browser.new :chrome
new后面有个空格(很重要)
ruby hao.rb报错
in `binary_path': Unable to find chromedriver. Pl
ease download the server from (Selenium::WebDriver::Error::WebDriverError)
https://chromedriver.storage.googleapis.com/index.html and place it somewhere on your PATH.
More info at https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver.
是因为缺少相应driver文件,去后面那个网址下一个(一定要跟浏览器版本对应不然还会报错,win32版和win64版兼容所以没有win64版本),解压后放到ruby/bin下面
ruby访问百度并搜索
require 'watir'
require 'rubygems'
ie = Watir::Browser.new
ie.goto 'www.baidu.com'
ie.text_field(:id => 'kw').set "test"
ie.button(:id => "su").click
sleep 3
ie.quit
require丢到env.rb里
做了个feature
#language:zh-CN
功能:百度测试
场景:点击搜索
假如我打开百度
当我搜索abc
那么我应该得到搜索结果
配套step
Given /我打开百度/ do
@ie = Watir::Browser.new :chrome
@ie.goto 'www.baidu.com'
end
When /我搜索(.*)/ do |str|
@str = str
@ie.text_field(:id => 'kw').set @str
@ie.button(:id => "su").click
end
Then /我应该得到搜索结果/ do
#@ie.text.should include(text)
sleep 5
end
结果:三个pass,打开页面看一眼关了
把@ie封装进对象
page.rb
class Page
def initialize
@ie = Watir::Browser.new
@ie.goto 'www.baidu.com'
end
def search str
@ie.text_field(:id => 'kw').set "test"
@ie.button(:id => "su").click
end
def has_text text
#@id.text.should include(text)
sleep 5
end
end
修改step
#encoding:utf-8
require File.join("d:\\work\\rubycode\\code\\",'page.rb')
Given /我打开百度/ do
@page=Page.new
end
When /我搜索(.*)/ do |str|
@str = str
@page.search @str
end
Then /我应该得到搜索结果/ do
@page.has_text @str
sleep 5
end
模仿登录
feature
#language:zh-CN
功能:登录管理
场景:登录成功
假如我打开登录网页
假如用户名是tomsmith
并且密码是SuperSecretPassword!
那么返回是成功
场景:登录成功
假如我打开登录网页
假如用户名是foobar
并且密码是barfoo
那么返回是失败
step
Given /我打开登录网页/ do
@Login=Login.new
end
Given /用户名是(.*)/ do |username|
@username=username
@Login.input_id @username
#@Login.text_field(:id => 'username').set @username
end
Given /密码是(.*)/ do |password|
@password = password
@Login.input_pw @password
end
Then /返回是(.*)/ do |tips|
@tips=tips
if @tips == "成功" then
@Login.is_correct "true"
else
@Login.is_correct "false"
end
end
Login类
class Login
def initialize
@ie = Watir::Browser.new
@ie.goto 'https://the-internet.herokuapp.com/login'
end
def input_id str
@ie.text_field(:id => 'username').set str
end
def input_pw str
@ie.text_field(:id => 'password').set str
@ie.button(:class => "radius").click
end
def is_correct str
@result=str
#if @ie.div(:id => 'flash').text == str then
@tip = @ie.div(:id => 'flash').text
if @result == "true" then
if @tip == "You logged into a secure area!
×" then
puts "correct&success"
else puts "correct but failure"
end
else if @tip == "Your username is invalid!
×" then
puts "wrong&success"
else
puts "wrong but failure"
end
end
end
end
运行
cucumber -r features/step_definitions/ features/login.feature
July 7
————————————————————————————————————————————————
改成读取数据表 Scenario Outline
feature
Feature: table test
Scenario Outline: test
When I log in the website
When I read the username <username>
When I read the password <password>
When I read the message <message>
Then I got the result <result>
Examples:
| username | password | message |result|
| tomsmith | SuperSecretPassword! | You logged into a secure area! |success|
| foobar | barfoo | Your username is invalid! |failure|
踩过的坑
场景后面有Outline不然不认例子
例子是Examples
读表变量要加<>
在feature中,将前面Scenario改为了Scenario Outline,表示会有表格参数,参数使用<>来进行标注,在用例结束后Examples关键字中写入参数的具体值。
step
When /I log in the website/ do
@Login=Login.new
end
When /I read the username (.*)/ do |username|
@username=username
@Login.input_id @username
end
When /I read the password (.*)/ do |password|
@password=password
@Login.input_pw @password
end
When /I read the message (.*)/ do |message|
@message=message
end
Then /I got the result (.*)/ do |result|
@result=result
if @result == "success" then
@Login.is_correct "true"
else
@Login.is_correct "false"
end
end
继续调用login类
July 8
————————————————————————
水厂测试
Water类
class WaterLogin
def initialize
@ie = Watir::Browser.new
@ie.goto '水厂网址'
@ie.window.maximize
end
def temp
@ie.goto'水厂网址'
end
def input_id str
@ie.text_field(:placeholder => '请输入用户名').set str
end
def input_pw str
@ie.text_field(:placeholder => '请输入密码').set str
@ie.button(:type => "button").click
end
def click_the_icon
@ie.span(:class => 'tool-list large').i(:title => '业务系统').click
#@ie.image(:title => '业务系统',:index => 0).click
#@ie.i(:class => 'iconfont iconxiangmuguanlix1',:text => '项目管理').click
@ie.div(:class => "happy-scroll-content").ul().li(:index => 18).a().click
end
def input_projectname str
sleep 1
@ie.iframe(:src => '/projectManage/basicInfo').text_field(:class => 'ivu-input ivu-input-default',:index => 0).set str
end
def input_projectadd str
puts str
@ie.iframe(:src => '/projectManage/basicInfo').text_field(:class => 'ivu-input ivu-input-default',:index => 4).set str
end
def input_projectapa str
puts str
@ie.iframe(:src => '/projectManage/basicInfo').text_field(:class => 'ivu-input ivu-input-default',:index => 1).set str
end
def submit
@ie.iframe(:src => '/projectManage/basicInfo').button(:type => 'button',:class => 'ivu-btn ivu-btn-primary').click
end
def close
@ie.window.close
end
end
feature
Feature: Water Project
Scenario Outline: User Login
When I login the water
Given my username is 账号
Given my password is 密码
When click the icon
Then set projectname <projectname>
Then set projectadd <projectadd>
Then set projectapa <projectapa>
Then I submit
Then I see the new data
Examples:
| projectname | projectadd | projectapa |
| 项目1 | 北京 | 数字部 |
| 项目2 | 辽宁 | 事业部 |
step
When /I login the water/ do
@Login=WaterLogin.new
end
Given /my username is (.*)/ do |username|
@username=username
@Login.input_id @username
end
Given /my password is (.*)/ do |password|
@password=password
@Login.input_pw @password
end
When /click the icon/ do
sleep 1
@Login.click_the_icon
#@Login.temp
end
Then /set projectname (.*)/ do |projectname|
@projectname=projectname
@Login.input_projectname @projectname
end
Then /set projectadd (.*)/ do |projectadd|
@projectadd=projectadd
@Login.input_projectadd @projectadd
end
Then /set projectapa (.*)/ do |projectapa|
@projectapa=projectapa
@Login.input_projectapa @projectapa
end
Then /I submit/ do
@Login.submit
end
Then /I see the new data/ do
@Login.temp
sleep 5
#看看效果
@Login.close
end
踩过的坑
iframe处理
这是个嵌套在iframe里的页面,提取里面的内容要先iframe进去
对浏览器的操作都在window里,如window.close,window.maximize
Watir
Watir是近期比较流行的由Ruby构建的自动化测试框架,它的全称是Web Application Testing in Ruby。与其他的商业工具相比Watir不但灵活小巧,而且功能也十分强大。Watir是基于Ruby这种面向对象的脚本语言的,它用的是一种纯粹的编程语言,并且它是一个开源的产品,同时它还允许对网页上的对象的直接操作,例如html和JavaScript 。不过Watir仅仅是对IE进行控制的,如果想要操控其他的浏览器例如Firefox或Safari就需要安装相应的Watir库,参考这里。总之,Watir是一个面向浏览器来开发和运行的一个自动化测试工具。
cucumber -r features/step_definitions/ features/water_cn.feature
Ferrum
GitHub - rubycdp/ferrum: Headless Chrome Ruby API
Ruby语言的实现的测试工具,支持有头和无头模式,支持多种测试方法
cucumber -r step_definitions/ features/furrum_water.feature
Capybara
GitHub - teamcapybara/capybara: Acceptance test framework for web applications
通过模拟真实用户与浏览器进行互动,无需设置,不必手动等待中间过程完成
cucumber -r step_definitions/ features/capybara_water.feature
find的智能寻找元素很好用