2

我在 Selenium Web 驱动程序中编写了一个代码来测试我网站的“联系我们”页面的健康状况。

代码工作正常。我的进一步目标是:

  1. 使用 Rescue(在此脚本失败时获取错误消息。特别是在函数 >“def 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 "mrityunjay@gmail.com"
    @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
4

3 回答 3

1

这是一个答案——只需使用“find_elements”代替它给出一个空数组而不是抛出异常。

于 2012-05-31T20:27:35.767 回答
0

你的意思是这样的吗

begin
    @driver.find_element(:name, "submit") 
rescue
    @driver.save_screenshot("Submit_button_not_present.png")
    assert_match "true","false","Your error message"
end
于 2012-05-30T14:53:12.897 回答
-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

在救援中,我们可以使用 Log 来跟踪错误。为此,我们必须生成日志文件。代码如下:

首先在代码开头将 log 定义为:

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

接下来,我们将使用下面给出的代码将错误消息移动到我们的日志文件中:

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

日志将是这样的:E,[2012-06-04T16:03:44.546875 #796] 错误 - :找不到元素时出错!:无法找到元素:{“method”:“id”,“selector ":"公司"}

于 2012-06-04T11:03:12.470 回答