waitr自动化测试
Watir利用autoit处理JavaScript Pop Ups
2009年9月23日
阅读评论 发表评论
问题:
最近朋友做了一个投票活动,给每个活动参与者加200积分,他使用的discuz论坛,看他一个一个加,我就是用Watir写了几行代码自动提交,无奈的 是提交的时候弹出的confirm确认框,要挨个挨个点击确定,很不方便,最终在OpenQA上找到了解决方案,利用autoit可以解决 confirm、prompt、alert等弹出框问题。
代码:
require ' watir ' require ' win32ole ' def check_for_popups ( title = " Window Internet Explorer " , button = " OK " ) popup = Thread . new { autoit = WIN32OLE . new ( ' AutoItX3.Control ' ) ret = autoit . WinWait ( title , "" , 30 ) if ( ret == 1 ) puts " There is popup. " autoit . WinActivate ( title ) button . downcase ! if button . eql ? ( " ok " ) || button . eql ? ( " yes " ) || button . eql ? ( " continue " ) autoit . Send ( " {Enter} " ) else autoit . Send ( " {tab} " ) autoit . Send ( " {Enter} " ) end elsif ( ret == 0 ) puts " No popup, please check your code. " end } at_exit { Thread . kill ( popup ) } end ie = Watir :: IE . new ie . speed = : fast username = ' username ' password = ' password ' nones = ' 无 ' filename = ' username.txt ' ie . goto ( ' http://www.eetop.cn/bbs/logging.php?action=login ' ) if ie . contains_text ( ' 用户名 ' ) ie . text_field ( : name , " username " ) . set ( username ) ie . text_field ( : name , " password " ) . set ( password ) ie . button ( : name , " loginsubmit " ) . click end file = File . new ( filename , ' r ' ) ; while line = file . gets line . to_s . strip ! if line != nones puts line ie . goto ( ' http://www.eetop.cn/bbs/memcp.php?action=credits ' ) ie . text_field ( : name , " password " ) . set ( password ) ie . text_field ( : name , " to " ) . set ( line . to_s ) ie . text_field ( : name , " amount " ) . set ( ' 200 ' ) ie . text_field ( : name , " transfermessage " ) . set ( ' 积分奖励 ' ) ie . button ( : name , " creditssubmit " ) . click_no_wait check_for_popups ( " Message from webpage " , " OK " ) end end ie . close
参考:
http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups