简体   繁体   English

在将Cucumber Selenium与Ruby结合使用时JavaScript中出现错误

[英]Error in JavaScript while using in Cucumber Selenium with Ruby

I am trying to select a dropdown using the below code in Selenium ruby with cucumber framework.I have included a Javascript to select the value. 我正在尝试使用带有黄瓜框架的Selenium ruby​​中的以下代码选择一个下拉列表。我包括一个Javascript以选择值。

When(/^user selects year as (\d+)$/) do |arg1|

  $driver.execute_script('document.getElementsByName("param[start_year]")[0].value=arg1;')

end

But I am getting an error like this.Please help me resolve 但是我遇到这样的错误。请帮助我解决

arg1 is not defined (Selenium::WebDriver::Error::JavascriptError)

The bit of JavaScript you are executing has the variable arg1 hard coded in. The script will see this as a JavaScript variable - and as no such variable has been defined within the JavaScript space, you get an error. 您正在执行的JavaScript的位中已硬编码了变量arg1 。脚本会将其视为JavaScript变量-由于JavaScript空间中未定义此变量,因此会出现错误。

From the rest of the code, I assume you actually want to pass the value of the arg1 variable defined in the Ruby space, into the JavaScript code. 在其余的代码中,我假设您实际上想将Ruby空间中定义的arg1变量的值传递到JavaScript代码中。 I'd suggest you try this: 我建议您尝试一下:

When(/^user selects year as (\d+)$/) do |arg1|
  $driver.execute_script(%Q<document.getElementsByName("param[start_year]")[0].value="#{arg1}";>)
end

Note that I've used the %Q text delimiter so that #{} text insertion can be combined with text including double quotes ( " ) 请注意,我使用了%Q文本定界符,以便可以将#{}文本插入与包含双引号( " )的文本组合

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

相关问题 黄瓜异步javascript错误 - cucumber asynchronous javascript error 在硒webdriver中执行javascript时发生错误 - while execuiting javascript in selenium webdriver error occur 在设计Page对象模型时尝试BDD黄瓜硒时出现此错误 - Getting this error while trying BDD cucumber Selenium when designing Page object model 尝试将ruby代码转换为javascript时出错 - Error while trying to convert ruby code to javascript javascript 错误:尝试使用 Selenium 和 Python 检索 navigator.plugins 时出现循环引用错误 - javascript error: circular reference error while trying to retrieve navigator.plugins using Selenium and Python 运行带有capybara / selenium / ruby​​的JS脚本时出错 - I get an error while running an JS script with capybara/selenium/ruby 如何在Selenium + CUCUMBER中使用JavaScript在浏览器中运行自动化测试 - How to use javascript to run automated tests in browser using selenium+CUCUMBER 如何使用Cucumber \\ Ruby \\ PageObject从浏览器JavaScript全局变量获取值? - How to get a value from browser JavaScript global variable using Cucumber\Ruby\PageObject? 如何使用Selenium Ruby查找并单击:javascript链接? - How to find and click :javascript links using Selenium Ruby? 使用Ruby WebDriver(Selenium 2.0)单击javascript链接 - Using Ruby webdriver (Selenium 2.0) to click on a javascript link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM