简体   繁体   中英

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.

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.

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. 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 ( " )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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