您的位置:軟件測試 > 開源軟件測試 > 開源功能測試工具 > Watir
Ruby+watir自動化測試中實現(xiàn)識別驗證碼圖片
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2013/3/22 10:54:28 ] 推薦標(biāo)簽:

復(fù)制代碼

  def CheckCode_ok(gifurl,CheckCode)

  #獲取驗證碼圖片

  require 'net/http'

  #Net::HTTP.start(url) do |http|

  #resp = http.get(gifurl)

  resp = Net::HTTP.get_response(URI(gifurl))

  File.open("C:\CheckImg.jpg", "wb") do |file|

  file.write(resp.body)

  file.close

  end

  #end

  #執(zhí)行批處理文件

  system("c:\CheckBat.bat")

  #獲取txt中的驗證碼

  if File.exists?("c:\CheckCode.txt") ==true

  File.open("c:\CheckCode.txt","r") do |line|

  CheckCode=line.readline

  line.close

  end

  end

  puts CheckCode

  return CheckCode

  end

復(fù)制代碼

  調(diào)用:

復(fù)制代碼

  def xxx_www_login(user,pwd)

  LoadObject("../../testcase/xxx/xxx.yaml")

  times = 0

  loop do

  times += 1

  code = CheckCode_ok("http://xxx.xxxx.com/CheckImg.aspx/CheckImg.gif",code)

  puts code

   sleep 10 #獲取新的驗證碼以后,加個sleep等待時間,貌似IE需要時間緩存新的驗證碼code,否則@b對象獲取不到新的驗證碼code而登陸失敗

  if @b.text.include?(ExpectData("expect1")) == true #是否存在“商家登錄”

  xxx_login(user,pwd,code)

  elsif @b.text.include?(ExpectData("expect2")) == true #是否存在“退出”

  xxx_www_logout

  xxx_login(user,pwd,code)

  end

  break if times >= 5 or @b.text.include?(ExpectData("expect2")) == true #是否存在“退出”

  end #loop end end

上一頁12下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd