2012-05-30 83 views
2

我在Selenium Web驅動程序中編寫了一個代碼來測試我網站的聯繫我們頁面的健康狀況。Selenium Webdriver中的ScriptError救援?

代碼工作正常。我的另一個目的是:

  1. 使用救援(得到的錯誤消息時該腳本失敗特別是當沒有在功能>「高清test_contact_us_screen_shot」執行過程中發現的一些元素。)。

參考代碼。

require 'rubygems' 
require "selenium-webdriver" 
require "test/unit" 

class ContactUsScreenShot < Test::Unit::TestCase 

    def setup 
    @driver = Selenium::WebDriver.for :firefox 
    @base_url = "http://www.mycompany.com" 
    @driver.manage.timeouts.implicit_wait = 30 
    @verification_errors = [] 
    end 

    def teardown 
    @driver.quit 
    assert_equal [], @verification_errors 
    end 

    def test_contact_us_screen_shot 
    @driver.get(@base_url + "/contact_us") 
    #This method will take screenshot and save it in the same folder from where script is executed. 
    @driver.save_screenshot("./screen1.png") 
    puts "screen shot taken" 
    assert_equal "mycompany.com - Discuss your requirements to Outsource Software Product Development, Offshore Software Testing,", @driver.title 
    @driver.find_element(:id, "compan").clear 
    @driver.find_element(:id, "company").send_keys "My Company" 
    @driver.find_element(:id, "first_name").clear 
    @driver.find_element(:id, "first_name").send_keys "Mrityunjay" 
    @driver.find_element(:id, "last_name").clear 
    @driver.find_element(:id, "last_name").send_keys "Chauhan" 
    @driver.find_element(:id, "email").clear 
    @driver.find_element(:id, "email").send_keys "[email protected]" 
    @driver.find_element(:id, "phone1").clear 
    @driver.find_element(:id, "phone1").send_keys "9999999999" 
    @driver.find_element(:id, "addr1").clear 
    @driver.find_element(:id, "addr1").send_keys "306 Office Address" 
    @driver.find_element(:id, "addr2").clear 
    @driver.find_element(:id, "addr2").send_keys "Office Address" 
    @driver.find_element(:id, "city").clear 
    @driver.find_element(:id, "city").send_keys "Dehradun" 
    @driver.find_element(:id, "state").clear 
    @driver.find_element(:id, "state").send_keys "UK" 
    Selenium::WebDriver::Support::Select.new(@driver.find_element(:id, "country")).select_by(:text, "India") 
    @driver.find_element(:id, "zip").clear 
    @driver.find_element(:id, "zip").send_keys "248001" 
    @driver.find_element(:id, "website").clear 
    @driver.find_element(:id, "website").send_keys "http://www.mycompany.com" 
    @driver.find_element(:id, "comments").clear 
    @driver.find_element(:id, "comments").send_keys "This is the Testing Script for Contact Us page. Please Ignore.\nThanks,\nMrityunjay Chauhan" 
    @driver.find_element(:name, "submit").click 
    assert_equal "mycompany.com - Discuss your requirements to Outsource Software Product Development, Offshore Software Testing,", @driver.title 
    begin 
     rescue Exception => e 
     puts e.message 
     @driver.save_screenshot("./error1.png") 
    end 
    puts "I am Here" 
    end 

    def element_present?(how, what) 
    @driver.find_element(how, what) 
    true 
    rescue Selenium::WebDriver::Error::NoSuchElementError 
    false 
    end 

    def verify(&blk) 
    yield 
    rescue Test::Unit::AssertionFailedError => ex 
    @verification_errors << ex 
    end 
end 

回答

-1

以下救援代碼工作正常在我的代碼...

rescue Exception => e 
     puts e.message 
     puts "Element Not Found" 
     @driver.save_screenshot("./Test-Results/Contact_Us_Error.png") 
     puts "I am Here" 
    end #rescue 

在救援,我們可以通過日誌跟蹤誤差。爲此我們必須生成日誌文件。下面的代碼給出:

首先,在你的代碼的開始定義日誌爲:

rescue Exception => e 
      $log.error "This is the Error occurred when Element/s not found: #{e}" 
      @driver.save_screenshot("./Test-Results/Contact_Us_Error.png") 
      puts "I am Here" 
     end #rescue 

# Here I have created a log file in my "Test-Result" folder 
$log = Logger.new('./Test-Results/logfile.log') 

接下來,我們使用如下代碼將移動的錯誤信息給我們的日誌文件

日誌將如下所示: E,[2012-06-04T16:03:44.546875#796]錯誤 - :找不到元素時出錯!:無法找到元素:{「method」:「id」, 「selector」:「compan」}

0

你的意思是這樣

begin 
    @driver.find_element(:name, "submit") 
rescue 
    @driver.save_screenshot("Submit_button_not_present.png") 
    assert_match "true","false","Your error message" 
end 
+0

KS,我想在我的劇本上面的代碼,並得到了以下錯誤: 1)故障: test_contact_us_screen_shot(ContactUsScreenShot)[聯繫-US-ScreenShot.rb:56]: 我的錯誤消息。 <"false">預期= 。 – Mrityunjay

+0

在執行帶救援的腳本,如下所示,事情工作正常。 開始 救援異常=>電子 放e.message @ driver.save_screenshot( 「./ error1.png」) 結束 – Mrityunjay

+0

所以沒工作?如果它確實幫助你,請將此標記爲你的答案。 – Amey