Module: AssertHelper

Included in:
MyTest, SuperTest
Defined in:
lib/common/http/assert_helper.rb

Overview

case中尽量使用经过封装的assert

  • 扩展assert方法,支持html、json等解析

  • 自定义上层可读message

  • 打印可读日志

Instance Method Summary collapse

Instance Method Details

#assert_attr(xpath, attr, regexp, msg = nil, &blk) ⇒ Object

功能:

验证某Element的属性值

参数解释:

  • xpath 定位,可以使xpath or selector

  • attr 属性名

  • regexp 期待的结果,可以是正则表达式

  • msg 失败之后的提示文字

Example:

Example:

get "http://hi.baidu.com/"
assert_attr "div#m_space div.t1 a.grnlk", "href" "http://hi.baidu.com/hi/more/wonderful/"
# html代码片段
# <div id="m_space"> 
# <div class="t1">
#     <a href="http://hi.baidu.com/hi/more/wonderful/" target="_blank" class="grnlk"><img src="http://img.baidu.com/hi/img/index/i1.gif" border="0" align="top"> 空间中的海外华人  </a>
# ...


253
254
255
256
257
258
259
260
# File 'lib/common/http/assert_helper.rb', line 253

def assert_attr(xpath, attr, regexp, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_attr xpath, attr, &blk
    msg = "assert_attr xpath[#{xpath}] attr[#{attr}] regexp[#{regexp}] fail!" if msg.nil?
    assert_match regexp, expect, msg
    $log.debug "assert_attr xpath[#{xpath}] attr[#{attr}] regexp[#{regexp}] success!"
end

#assert_children(xpath, children, msg = nil, &blk) ⇒ Object

功能:

验证某元素不含有非预计的儿子节点

参数解释:

  • xpath 父亲节点的定位,可以使xpath or selector

  • children 预计的儿子节点名字的集合,如%wauthor body

  • msg 失败之后的提示文字

Example:

Example:

get "http://hi.baidu.com/"
assert_children "head", %w{title script link meta}      
#=> assert_children xpath[head] has children[["title", "script", "link", "meta"]], AND has other children[["style"]]!.


310
311
312
313
314
315
316
317
318
# File 'lib/common/http/assert_helper.rb', line 310

def assert_children(xpath, children, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_children xpath,children, &blk
    
    msg = "assert_children xpath[#{xpath}] has children[#{children}], AND has other children[#{expect}]!" if msg.nil?
    assert expect.empty?, msg
    $log.debug "assert_children xpath[#{xpath}] children[#{children}] success!"
end

#assert_code(code, msg = nil) ⇒ Object

功能:

验证HTTP请求的返回码

参数解释:

  • code Fixnum,返回码

  • msg 失败之后的提示文字

Example:

Example:

get "http://www.google.com/not_exist_page"
assert_code 404


54
55
56
57
58
59
60
# File 'lib/common/http/assert_helper.rb', line 54

def assert_code(code, msg=nil)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    msg = "assert_code fail!" if msg.nil?
    assert_equal @response.code, code.to_s, msg
    $log.debug "assert_code code[#{code}] success!"
end

#assert_exist(xpath, msg = nil, &blk) ⇒ Object

功能:

断言元素存在

参数解释:

  • xpath 定位,可以使xpath or selector

  • msg 失败之后的提示文字



143
144
145
146
147
148
# File 'lib/common/http/assert_helper.rb', line 143

def assert_exist(xpath, msg=nil, &blk)
    return if @is_press
    msg = "assert_exist xpath[#{xpath}] fail!" if msg.nil?
    assert find_exist?(xpath,&blk), msg
    $log.debug "assert_exist xpath[#{xpath}] success!"
end

#assert_grep(regexp, msg = nil) ⇒ Object

功能:

grep方式匹配

参数解释:

  • regexp 正则表达式

  • msg 失败之后的提示文字

Example:

get "http://hi.baidu.com/"
assert_grep /百度空间/


94
95
96
97
98
99
100
# File 'lib/common/http/assert_helper.rb', line 94

def assert_grep(regexp, msg=nil)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    msg = "assert_grep fail!" if msg.nil?
    assert_match regexp, @response.body, msg
    $log.debug "assert_grep regexp[#{regexp}] success!"
end

#assert_has(str, msg = nil) ⇒ Object

功能:

断言包含某字符串

参数解释:

  • str 字符串,不能是正则表达式

  • msg 失败之后的提示文字

Example:

get "http://hi.baidu.com/"
assert_has "百度空间", "something wrong!"


123
124
125
126
127
# File 'lib/common/http/assert_helper.rb', line 123

def assert_has(str,msg=nil)
    return if @is_press
    assert find_has?(str), msg
    $log.debug "assert_has str[#{str}] success!"
end

#assert_has_not(str, msg = nil) ⇒ Object

功能:

与assert_has相反



130
131
132
133
134
# File 'lib/common/http/assert_helper.rb', line 130

def assert_has_not(str,msg=nil)
    return if @is_press
    assert !find_has?(str), msg
    $log.debug "assert_has_not str[#{str}] success!"
end

#assert_html(xpath, regexp, msg = nil, &blk) ⇒ Object

功能:

验证某元素的html片段的内容

参数解释:

  • xpath 定位,可以使xpath or selector

  • regexp 期待的结果,可以是正则表达式

  • msg 失败之后的提示文字

Example:

Example #1:

get "http://hi.baidu.com/"
assert_html "#m_space", "更多精彩空间"

Example #2:

get "http://hi.baidu.com/"
assert_html "//div[@id='m_space']", "更多精彩空间"


214
215
216
217
218
219
220
221
# File 'lib/common/http/assert_helper.rb', line 214

def assert_html(xpath, regexp, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_html xpath, &blk
    msg = "assert_html xpath[#{xpath}] regexp[#{regexp}] fail!" if msg.nil?
    assert_match regexp, expect, "assert_html xpath[#{xpath}] regexp[#{regexp}] fail!"
    $log.debug "assert_html xpath[#{xpath}] regexp[#{regexp}] success!"
end

#assert_no_attr(xpath, attr, regexp, msg = nil, &blk) ⇒ Object

功能:

见 assert_attr



264
265
266
267
268
269
270
271
272
# File 'lib/common/http/assert_helper.rb', line 264

def assert_no_attr(xpath, attr, regexp, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_attr xpath, attr, &blk
    exp = /#{Regexp.escape(regexp)}/ if String === regexp
    msg = "assert_no_attr xpath[#{xpath}] attr[#{attr}] regexp[#{regexp}] fail!" if msg.nil?
    assert_no_match exp, expect, msg
    $log.debug "assert_no_attr xpath[#{xpath}] attr[#{attr}] regexp[#{regexp}] success!"
end

#assert_no_exist(xpath, msg = nil, &blk) ⇒ Object

功能:

断言元素不存在

参数解释:

  • xpath 定位,可以使xpath or selector

  • msg 失败之后的提示文字



157
158
159
160
161
162
# File 'lib/common/http/assert_helper.rb', line 157

def assert_no_exist(xpath, msg=nil, &blk)
    return if @is_press
    msg = "assert_no_exist xpath[#{xpath}]" if msg.nil?
    assert_false find_exist?(xpath,&blk), msg
    $log.debug "assert_no_exist xpath[#{xpath}] success!"
end

#assert_no_grep(regexp, msg = nil) ⇒ Object

功能:

见 assert_grep



104
105
106
107
108
109
110
111
# File 'lib/common/http/assert_helper.rb', line 104

def assert_no_grep(regexp,msg=nil)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    msg = "assert_no_grep fail!" if msg.nil?
    exp = /#{Regexp.escape(regexp)}/ if String === regexp
    assert_no_match exp, @response.body, msg
    $log.debug "assert_no_grep regexp[#{regexp}] success!"
end

#assert_no_html(xpath, regexp, msg = nil, &blk) ⇒ Object

功能:

见 assert_html



225
226
227
228
229
230
231
232
233
# File 'lib/common/http/assert_helper.rb', line 225

def assert_no_html(xpath, regexp, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_html xpath, &blk
    exp = /#{Regexp.escape(regexp)}/ if String === regexp
    msg = "assert_no_html xpath[#{xpath}] regexp[#{regexp}] fail!" if msg.nil?
    assert_no_match exp, expect, msg
    $log.debug "assert_no_html xpath[#{xpath}] regexp[#{regexp}] success!"
end

#assert_no_text(xpath, regexp, msg = nil, &blk) ⇒ Object

功能:

见 assert_text



188
189
190
191
192
193
194
195
196
# File 'lib/common/http/assert_helper.rb', line 188

def assert_no_text(xpath, regexp, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_text xpath, &blk
    exp = /#{Regexp.escape(regexp)}/ if String === regexp
    msg = "assert_no_text xpath[#{xpath}] regexp[#{regexp}] fail!" if msg.nil?
    assert_no_match exp, expect, msg
    $log.debug "assert_no_text xpath[#{xpath}] regexp[#{regexp}] success!"
end

#assert_ok(msg = nil) ⇒ Object

功能:

验证返回码是200

参数解释:

  • msg 失败之后的提示文字

Example:

Example:

get "hi.baidu.com"
assert_ok


35
36
37
38
39
40
41
# File 'lib/common/http/assert_helper.rb', line 35

def assert_ok(msg=nil)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    msg = "assert_ok fail!" if msg.nil?
    assert_equal @response.code, "200", msg
    $log.debug "assert_ok success!"
end

#assert_redirected_to(url, msg = nil) ⇒ Object

功能:

验证重定向

参数解释:

  • url 要跳转的url

  • msg 失败之后的提示文字

Example:

Example:

get "http://hi.baidu.com/sys/not_exist_page"
assert_redirected_to "error.html"


73
74
75
76
77
78
79
80
81
# File 'lib/common/http/assert_helper.rb', line 73

def assert_redirected_to(url, msg=nil)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    msg = "assert_redirected_to fail!" if msg.nil?
    assert_equal @response.code, "302", msg
    assert @response.header.key?("Location"), msg
    assert_equal @response.header["Location"], url, msg
    $log.debug "assert_redirected_to url[#{url}] success!"
end

#assert_size(xpath, size, msg = nil, &blk) ⇒ Object

功能:

验证匹配的个数,如当前页某栏目的内容数

参数解释:

  • xpath 定位,可以使xpath or selector

  • size 期待的结果,Fixnum

  • msg 失败之后的提示文字

Example:

Example:

get "http://hi.baidu.com/"
assert_size "div#m_space ul li", 12
# 百度空间首页,精彩空间栏会出12个推荐空间


287
288
289
290
291
292
293
294
# File 'lib/common/http/assert_helper.rb', line 287

def assert_size(xpath, size, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_size xpath, &blk
    msg = "assert_size xpath[#{xpath}] size[#{size}] expect[#{expect}] fail!" if msg.nil?
    assert expect == size, msg
    $log.debug "assert_size xpath[#{xpath}] size[#{size}] expect[#{expect}] success!"
end

#assert_text(xpath, regexp, msg = nil, &blk) ⇒ Object

功能:

验证某元素内text的内容

参数解释:

  • xpath 定位,可以使xpath or selector

  • regexp 期待的结果,可以是正则表达式

  • msg 失败之后的提示文字

Example:

Example #1:

get "http://hi.baidu.com/"
assert_text "#m_space", "更多精彩空间"


177
178
179
180
181
182
183
184
# File 'lib/common/http/assert_helper.rb', line 177

def assert_text(xpath, regexp, msg=nil, &blk)
    return if @is_press
    raise "must send http request before assert!" if @response == nil
    expect = find_text xpath, &blk
    msg = "assert_text xpath[#{xpath}] regexp[#{regexp}] fail!" if msg.nil?
    assert_match regexp, expect, msg
    $log.debug "assert_text xpath[#{xpath}] regexp[#{regexp}] success!"
end