1

我正在努力将 allure 集成到我的测试中,但遇到了第一个测试套件失败并返回未定义的方法 `find_element' for nil:NilClass 错误与套件中的每个测试的问题。当我不使用 allure 时,测试会按预期运行,无论运行什么套件,都会发生同样的事情。

我已经对其进行了研究,似乎我的规范助手中的 before 和 after 钩子没有在失败的测试中使用。有什么想法可能导致这种情况吗?

require 'rspec'
require 'selenium-webdriver'
require 'allure-rspec'
require 'nokogiri'
require 'uuid'
require 'pathname'

RSpec.configure do |c|


c.include AllureRSpec::Adaptor


c.expect_with :rspec do |c|
    ### Enable both should and expect syntax ###
    c.syntax = [:should, :expect]
end

c.before(:all) do
    #Start the Phantom-js driver before running any headless tests
    case ENV['browser']
    when 'headless'
        ## Run in command line before a headless test: phantomjs --webdriver=8001
        system('start cmd /k phantomjs --webdriver=8001')
        sleep 3
    end
end

c.before(:each) do  
    #Find the browser to the used and set the driver to the appropriate one
    case ENV['browser']
    when 'chrome'
        @driver = Selenium::WebDriver.for :chrome
        #@driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :chrome)
    when 'ie'
        @driver = Selenium::WebDriver.for :internet_explorer
        #@driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :ie)
    when 'firefox'
        @driver = Selenium::WebDriver.for :firefox
        #@driver = Selenium::WebDriver.for(:remote, url: 'http://localhost:5555/wd/hub', desired_capabilities: :firefox)
    when 'headless'
        @driver = Selenium::WebDriver.for :remote, url: 'http://localhost:8001'
    end
    #Maximize the browser
    @driver.manage.window.maximize
    @driver.get ENV['base_url']
end

c.after(:each) do |c|
    #Takes a screen shot if an exception is thrown and attaches it to the allure XML when running Rake tests
    if c.exception != nil
        $failure = true

            c.attach_file("screenshot", File.new(@driver.save_screenshot(File.join(Dir.pwd, "test_reports/allure/#{UUID.new.generate}.png"))))

    end 
    @driver.quit
end 

end

AllureRSpec.configure do |c|
   #Outputs the Allure XML
   c.output_dir = "test_reports/allure"
end
4

0 回答 0