I am writing a test for desktop web automation using the
selenium cucumber ruby.
My test includes both predefined and custom steps.
However, my test fails to run due to an error caused by a custom step.
The feature file that I run:
Feature: Login a customer
Scenario: Go in a call
Given I navigate to <url>
...
When I submit the form with id "custSearchSimple"
....
And I wait for 5 sec
Then element having id "accNumber" should be present
The When I submit the form with id "custSearchSimple" is a custom step.
This step is defined in the custom_step.rb. I have used the submit command of the Selenium Cucumber Ruby API. The custom_step.rb is the following file:
require 'selenium-cucumber'
# Do Not Remove This File
# Add your custom steps here
# $driver is instance of webdriver use this instance to write your custom code
#firefox browser instantiation
driver = Selenium::WebDriver.for :firefox
driver.navigate.to "https://localhost/telebet"
When(/^I submit the form with id "(.*?)"$/) do |arg1|
element= driver.submit(:id,"#{arg1}")
end
When I run the feature file by running cucumber features/name_of_the_file.feature, I get the NoMethodError error:
When I submit the form with id "custSearchSimple" # features/step_definitions/custom_steps.rb:12
private method `submit' called for #<Selenium::WebDriver::Driver:0x94c4bde4bdff4d6 browser=:firefox> (NoMethodError)
I could not find any example using the Selenium Cucumber Ruby API for writing custom steps. I suspect that I might have omitted some commands for the selenium web driver ruby. Something is missing that I could not find. Does anyone know why I get this error?
Maybe I am confused here but:
When(/^I submit the form with id "(.*?)"$/) do |arg1|
submit_form arg1
end
def submit_form(form_id)
submit("id", form_id)
end
Would do what you want? submit_form isn't a cucumber step, it's a ruby method - probably why you're getting Cucumber::UndefinedDynamicStep.
Related
I am new to this so it may be obvious, but I am struggling with getting my test to run the "navigate" element. Or Object. Whatever it is. Here is my code:
require "selenium-webdriver"
require "rspec"
# TEST: Sign up for blog
describe "Blog application" do
describe "signup to the blog application" do
it "confirm that a user can successfully signup" do
driver = Selenium::WebDriver::Firefox
# Go to signup form
driver.get "https://selenium-blog.herokuapp.com/signup"
# Fill out and submit form
username_field = driver.find_element(id: 'user_username')
username_field.send_keys("user")
There's more to this test but it isn't relevant to my question. What am I missing? Do I need to install another gem or driver? I've searched everywhere for this answer and can't find it.
I've also tried running it with:
driver.navigate.go
and that fails as well. Says it cannot find the "navigate" element.
Thanks for your help!
You are missing initializing of your wanted web driver. However you didn't post your current version of selenium, so I assumed it was last version.
Here is what documentation says about Selenium::WebDriver.
.for(browser) ⇒ Driver
.for(browser, opts) ⇒ Driver
Create a new Driver instance with the correct bridge for the given browser
One special argument is not passed on to the bridges, :listener. You can pass a listener for this option to get notified of WebDriver events. The passed object must respond to #call or implement the methods from AbstractEventListener.
So in your case you need to change you driver variable
driver = Selenium::WebDriver.for(:firefox)
# ...
WebDriver docs
Here is the simple code which should help you
require 'selenium-webdriver'
driver=Selenium::WebDriver.for :firefox
driver.navigate.to("https://www.google.com/")
I am new to Capybara. I have a question about why my Capybara doesn't work when I use it together with Selenium Webdriver.
This is my sample code:
Given(/^I am on the Youtube homepage$/) do
# visit 'http://www.youtube.com'
driver = Selenium::WebDriver.for :firefox
driver.navigate.to('http://www.youtube.com')
end
When(/^I search for "([^"]*)"$/) do |search_term|
fill_in 'search_query', :with => search_term
click_on 'search-btn'
end
Then(/^videos of large rodents are returned$/) do
expect(page).to have_content 'Making Friends with a Capybara'
end
When I run it, it just open Firefox and go to Youtube homepage. But it gets error:
Capybara::ElementNotFound: Unable to find field "search_query".
Everything works with visit 'http://www.youtube.com' command.
You're creating a driver, telling it to navigate to a page and then it's going out of scope so it's getting deleted. the visit line works because it's using the current capybara driver which stays around between test steps. Rather than creating a driver manually you should be registering a driver with Capybara and then specifying which driver to use for the specific test. See drivers
Since capybara sets up a selenium driver by default for use with firefox you can just do
Capybara.default_driver = :selenium
somewhere before running your tests to make all your tests run using selenium with firefox, or since capybara registers selenium as the default javascript driver you can tag any scenarios you want to run in firefox with #javascript as shown here
#javascript
Scenario: do something something
Given ...
I have JavaScript in my application that submits a form when the user hits Ctrl-S or Cmd-S. I want to write an automated test for this using RSpec, Capybara, and Capybara Webkit. I don't think I can just have Capybara execute JavaScript to trigger Ctrl-S/Cmd-S because that's not normally allowed with JavaScript in Chrome as a security concern. I see with Selenium there are page.driver.browser.action.key_down/key_up methods available. Is there anything similar with Capybara Webkit? If not, how can I send Ctrl-S and Cmd-S to the browser in my test?
Edit: I also can't get this to work using the regular Selenium driver with Firefox:
describe 'edit a template and hit Ctrl-S', js: true do
render_views
it 'saves the template' do
visit my_path
page.execute_script("$('#hidden_textarea').val('Fabulous new content')")
builder = page.driver.browser.action
builder.key_down(:control).send_keys('s').key_up(:control).perform
expect(page).to have_text('Record was saved.')
expect(page).to have_text('Fabulous new content')
end
end
It looks like the builder.key_down(:control).send_keys('s').key_up(:control).perform isn't doing anything--the page loads in Firefox but just sits there. This is with Firefox 19 on OS X with selenium-webdriver 2.35.1.
Any suggestions on how to get this to work in either Firefox or Chrome, with either Selenium or Capybara Webkit?
I'm trying to do a similar thing in Chrome using Capybara and SitePrism. This actually works for me in Firefox though:
page.element.native.send_keys :command, 'a'
so I suggest trying this
builder.native.send_keys :control, 's'
I'm using Cucumber to test a comment form that doesn't have a submit button. I found that selenium has a method called send_key, which in theory should allow me to do this:
find_field('my-field').native.send_key(:enter)
But when I run my test, I get:
undefined method `send_key' for #<Nokogiri::XML::Element:0x007f874b361828> (NoMethodError)
Not a clue what I'm doing wrong. Any ideas?
You have to use the Selenium driver and not the :rack_test driver in Capybara to access the send_keys method:
Install the gem selenium-webdriver and add it to your gem file if you are using bundler.
Mark your test using :js => true so that it runs with the Selenium driver.
You get an error because by default, Capybara uses the :rack_test driver. Calling native on an element access the driver specific methods. :rack_test driver elements are implemented natively as Nokogiri::XML::Element, therefore the send_keys methods do not exists and you get this error.
Try with xpath
within(:xpath, "//form[#id='the_form']") do
locate(:xpath, "//input[#name='the_input']").set(value)
locate(:xpath, "//input[#name='the_input']").node.send_keys(:return)
end
You could also have a hidden button. Capybara can see it and click it.
<%= form.submit "OK", style: "display: none;" %>
Then in your test:
Capybara.ignore_hidden_elements = false
click_on "OK"
I have the following:
setup do
Capybara.current_driver = Capybara.javascript_driver
#project.user = #user
#project.save
Project.any_instance.stubs(:price_all)
end
And yet I have a test failing because the Project.price_all method is being run:
/Users/me/code/rails/myapp/app/models/project.rb:178:in `price_all'
This was working properly until I upgraded to Capybara 2 and the latest version of capybara-webkit.
Why is that method still being run? And how can I fix?
When you say "the Project.price_all method is being run", is that a typo, or is price_all really a class method? If it is indeed a class method, you'll want to use Project.stubs(:price_all) instead of including any_instance so the stub is directly on the Project class rather than an instance of Project. If that's not your issue, I'm not sure what else to suggest based on the code sample you've provided.