简体   繁体   English

Ruby Selenium开始救援

[英]Ruby Selenium begin rescue

I use Cucumber with Ruby and Selenium. 我将Cucumber与Ruby和Selenium结合使用。 Here is scenario where I need to check that element is not on the page When I do such within Cucumber step 在这种情况下,我需要检查该元素是否不在页面上,当我在Cucumber步骤中执行此操作时

    Step file:
    begin
     expect(checkout_page.city_acctdetails.displayed?).to eql false
    rescue Selenium::WebDriver::Error::NoSuchElementError
     puts "City dropdown is not present"
     false
    end

it works like a charm. 它就像一个魅力。 When I try to do something like by putting as method in another file/helper 当我尝试通过将as方法放置在另一个文件/帮助器中来进行操作时

 RubyHelper.rb file
   def self.not_present(locator)
    begin
      locator.displayed?
    rescue Selenium::WebDriver::Error::NoSuchElementError
      puts "Not present"
      false
    end
  end

so when I change in Cucumber Step file 所以当我在黄瓜步骤文件中更改时

RubyHelper.not_present(checkout_page.city_acctdetails)

I get error: Unable to locate element: {} (Selenium::WebDriver::Error::NoSuchElementError) 我收到错误消息:无法找到元素:{}(Selenium :: WebDriver :: Error :: NoSuchElementError)

Require statements: 要求声明:

  #BDD framework that makes feature files running
require 'cucumber'

#Control remote browsers
require 'selenium-webdriver'

#Run tests in parallel
require 'parallel_tests'

#Use expect().to for verifying result
require 'rspec'

#Key value format for storing text data
require 'yaml'

Ruby Cucumber magic depends on something called the World object . 红宝石黄瓜的魔术依赖于一种被称为World对象的东西。 To make your RubyHelper file available to your step definitions you need to: 要使您的RubyHelper文件可用于您的步骤定义,您需要:

  1. Wrap the helper methods in a module 将辅助方法包装在模块中
  2. Tell Cucumber to include the module into each World object for each Scenario 告诉Cucumber将模块包含在每个方案的每个World对象中

RubyHelper.rb file RubyHelper.rb文件

require 'cucumber'
require 'selenium-webdriver'
require 'rspec'
module MyHelper
  def not_present(locator)
    begin
      locator.displayed?
    rescue Selenium::WebDriver::Error::NoSuchElementError
      puts "Not present"
      false
    end
  end
end
World(MyHelper)

Maybe you can try to sleep or wait for that event load. 也许您可以尝试sleep或等待该事件加载。 It something that when you run your test, the test was executed but the element is not yet there. 当您运行测试时,测试已经执行,但是元素还不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM