In my test automation project, I am using ruby with capybara, cucumber and selenium.
(I already have devkit installed and chrome is starting normally)
When looking for an element in the site to select it, I am using the method driver.findElement (By.xpath (.... etc"), but when executing cucumber, it is indicating the following error:
I already removed and reinstalled the selenium-webdriver gem but it did not resolve.
Can someone help me in resolving why WebDriver does not seem to be valid in this context?
code example
(finding button logout using tag img, because the element don't have name or id)
After('#logout') do
element = driver.findElement(By.xpath("//img[#src='/Portal/img/user.png']"));
element.click
end
Result cucumber execution
Feature: Login
Description feature
DevTools listening on ws://127.0.0.1:60121/devtools/browser/c0bacc6e-697a-4614-b82c-eb324d587df5
#logout
Scenario: Login_OK # features/support/login.feature:14
Given that i access the main portal page HRP # features/step_definitions/login_steps.rb:1
When do login using "abc123" and "abc123password" # features/step_definitions/login_steps.rb:5
Then system do login # features/step_definitions/login_steps.rb:10
And show message "Welcome to Portal." # features/step_definitions/login_steps.rb:14
undefined local variable or method `driver' for # (NameError)
./features/support/hooks.rb:4:in `After'
Failing Scenarios:
cucumber features/support/login.feature:14 # Scenario: Login_OK
1 scenario (1 failed)
4 steps (4 passed)
0m5.457s
If you're using Capybara with Cucumber in a standard setup then you shouldn't be really ever be calling the selenium webdriver instance directly - driver (except for in some very rare circumstances). Instead you should be using the Capybara methods. What is available in your After hook depends on exactly how where you've includeed what but one of these will probably be avaialble
After('#logout') do
element = page.find(:xpath, ".//img[#src='/Portal/img/user.png']"));
element.click
end
or
After('#logout') do
element = Capybara.current_session.findElement(:xpath, ".//img[#src='/Portal/img/user.png']");
element.click
end
Note: there is probably a better way to locate the logout button than an XPath on the contained images source, but without seeing the HTML of the button it's impossible to say for sure (for instance, Capybaras button finder will match against the alt attribute of an img element nested inside a <button> element)
Related
Newbie engineer question here, but I've searched and can't find a solution.
Environment: Mac OS, Chrome, Capybara with Selenium Chrome-driver, Ruby with Rspec running the test.
Situation:
Testing a React app where the user logs in using a username and password, followed by clicking on a sidebar nav link that loads up....followed by other actions. Capybara continues to fail to find the sidebar nav link to click on. I believe the sidebar nav is its own React component and loads asynchronously
App & Test Behavior:
Sometimes Capybara finds the link element, clicks the link and the test passes. Other times it completely fails to find the element and fails the test.
Solutions I've tried:
Upping the default time for finder methods to continue to search to 15+ seconds(I've never noticed the app take more than 5 seconds on anything)
I only have used finder methods that SHOULD be repeat-searching for the default time for the element to appear (page.find, page.has_css?, etc)before attempting the .click action. What I have found is that after the login happens, and while the nav is loading, the test just seems to fail with element not found. The page.find does NOT seem to continue to search for 15 seconds before failing - instead, the login happens, then a second later I get a fail with element not found.
I have tried passing a wait into the find (example: page.find(some element, wait:15).click . This runs into the same problem as above where it doesn't seem to continue searching for 15 seconds for the element to appear and then click it.
What does seem to work is adding in sleeps before searching for an element (example: login, sleep(5), page.find(something).click).
Unfortunately I'm having this same problem with other elements all over in the app - for example a div may have a bunch of cards in it. When a new card is added it takes 2-3 seconds to show up in the list (probably due to sending out the card info to the database, and refreshing the cards on the page). If I try and page.find immediately after adding a card, the test will almost immediately fail with an element not found message. If I add the card, sleep(3), THEN try page.find, it will find it.
I can't just add sleep all over the place in the app, because its huge and it would slow down the tests immensely. I think I've tried all the typically suggested fixes for asynchronous loading. Any idea what is going on here and what I can do to fix it
editing to add some requested code.
I'm using capybara 3.2.
We are using a bit of a page object style framework so I"ll try and post the actual test with its methods in bold and helper methods nested in it.
Its getting caught in the before action of this particular feature on the final method to click on the sidebar. I'm a little limited on what I can show, but I think this will make sense....
The actual before action:
before do
**app.launch_app(app.url)**
# this instantiates an instance of the framework and helper methods and # goes to the app url
**app.login.full_login(app.username('Some User'), app.password)**
# def full_login(user, pass)
# enter_email(user)
# def enter_email(user)
# return if already_logged_in?
# def already_logged_in?
# page.has_css?("a[href*='/me']", wait: false)
# end
# fill_field('email', user)
# def fill_field(field, text)
# sleep 0.1
# page.find("input[type=#{field}]").send_keys text
# end
# click_button 'Log In'
# def click_button(text)
# page.click_on text
# end
# end
# login_using_second_auth(user, pass)
# def login_using_second_auth(user, pass)
# page.fill_in 'username', with: user
# page.fill_in 'password', with: pass
# click_button 'Sign In'
# end
# end
app.nav.click_sidebar_link('Admin Account', 'Admin')
# def click_sidebar_link(link, section)
# sleep(2)
# page.find('div[class^=app__wrapper] > div > div > div', text:
# section)
# .find('span[class^=nav-item]', text: link).click
# end
end
Sorry that looks so messy, but I think you guys can make sense of it.
The test is flaky so after running it a few times I can't get the exact error, but its usually element not found on the span with the text Admin
I am using Capybara and getting errors from the finders 'find_field' & 'has_selector'.
I have tried using them like this:
page = visit "http://www.my-url-here.com"
next if page.has_selector?('#divOutStock')
page.find_field('#txtQty').set('9999')
has_selector returns the error: "NoMethodError: undefined method `has_selector?' for {"status"=>"success"}:Hash"
find_field cannot find the field. (It is present on the page and is not a hidden field.)
I have also tried using fill_in to set the field value, that doesn't work either.
How can I get this to work with Capybara?
You have a couple of issues in your code
page is just an alias for Capybara.current_session. If you assign to it you're creating a local variable and it's no longer a session
find_field takes a locator - http://www.rubydoc.info/gems/capybara/Capybara/Node/Finders#find_field-instance_method - which will be matched against the id, name, or label text. It does not take a CSS selector
Your code should be
page.visit "http://www.my-url-here.com"
next if page.has_selector?('#divOutStock')
page.find_field('txtQty').set('9999')
and you could rewrite the last line as
page.fill_in('txtQty', with: '9999')
Also you should note that (if using a JS capable driver) has_selector? will wait up to Capybara.default_max_wait_time for the #divOutStock to appear. If it's not usually going to be there and you want to speed things up a bit you could do something like
page.visit "http://www.my-url-here.com"
page.assert_text('Something always on page once loaded') #ensure/wait until page is loaded
next if page.has_selector?('#divOutStock', wait: 0) # check for existence and don't retry
page.fill_in('txtQty', with: '9999')
Recently we upgraded our selenium web driver from 2.47.1 to 2.48.0.
With this upgrade I need to add sleep for a few seconds in rspec to pass. Spec was working properly without sleep with the older version.
sleep(inspection_time=5) // why do I need this?
my_form_page.save_button.click
// some assertion here
Edit
I tried using implicit wait instead of sleep.But it's not working. Is there any specific reason behind it?
Capybara.current_session.driver.browser.manage.timeouts.implicit_wait = 50
Generally speaking, rspec selenium tests are known to be "flakey". Sometimes rspec tries to search for an element before it appears on page due to many reasons (ie: element appears upon ajax response).
Here's a tip that may help you solve this, if you will wrap your capybara finders inside of a within block, your tests will wait until it finds that within selector FIRST before trying to run the code inside of it.
This more-often-than-not will help solve a test running too fast on a page that takes a while to load and your button or selector or whatever isn't actually on the page yet (which is why it fails).
So take a look at these 2 examples and try the within method...
# spec/features/home_page_spec.rb
require "spec_helper"
describe "the home page", type: :feature do
context "form" do
# THIS MIGHT FAIL!!!!
it "submits the form", js: true, driver: :selenium do
visit "/"
find("#submit_button").click
end
# THIS PROBABLY WILL PASS!!!
it "submits the form", js: true, driver: :selenium do
visit "/"
within "form" do
find("#submit_button").click
end
end
end
end
When using Capybara, what is the difference between calling page.find('#name') and find('#name').
Is it that same thing, as this answer states What's the meaning of page and page.body in Capybara
I am just looking for more of an explanation and when I would need to use page outside of asserts.
As it is described in source code:
# Shortcut to accessing the current session.
# #return [Capybara::Session] The current session object
def page
Capybara.current_session
end
When you do find('#name') current session's find method is called. So there is no difference between calling page.find('#name') and find('#name').
I guess this shortcut was created just to keep asserts code intuitive and understandable:
expect(page).to have_css(#name)
looks better than
expect(Capybara.current_session).to have_css(#name)
I'm working on test automation with cucumber, selenium-webdriver and page-object gem.
When I try to run simple test cucumber catch the following error:
Scenario: Going to billing # features/test.feature:10
When I click 'Платные услуги' # features/step_definitions/test_steps.rb:13
Unable to locate element: {"method":"link text","selector":"Платные услуги"} (Selenium::WebDriver::Error::NoSuchElementError)
[remote server] file:///tmp/webdriver-profile20130412-21410-z4p1ez/extensions/fxdriver#googlecode.com/components/driver_component.js:8405:in `FirefoxDriver.prototype.findElementInternal_'
[remote server] file:///tmp/webdriver-profile20130412-21410-z4p1ez/extensions/fxdriver#googlecode.com/components/driver_component.js:8414:in `FirefoxDriver.prototype.findElement'
[remote server] file:///tmp/webdriver-profile20130412-21410-z4p1ez/extensions/fxdriver#googlecode.com/components/command_processor.js:10421:in `DelayedCommand.prototype.executeInternal_/h'
[remote server] file:///tmp/webdriver-profile20130412-21410-z4p1ez/extensions/fxdriver#googlecode.com/components/command_processor.js:10426:in `DelayedCommand.prototype.executeInternal_'
[remote server] file:///tmp/webdriver-profile20130412-21410-z4p1ez/extensions/fxdriver#googlecode.com/components/command_processor.js:10366:in `DelayedCommand.prototype.execute/<'
./features/pages/job_main_page.rb:38:in `go_to_billing'
./features/step_definitions/test_steps.rb:14:in `/^I click 'Платные услуги'$/'
features/test.feature:11:in `When I click 'Платные услуги''
Here is the cucumber feature:
Scenario: Going to billing
When I click 'Платные услуги'
Then I should see "Коммерческие услуги"
Step definition where test falls:
When(/^I go to billing$/) do
#job_myroom_billing = #job_myroom.billing_element.when_visible.go_to_billing
end
And page object:
class BasePage
include PageObject
include RSpec::Matchers
end
class JobMyroom < BasePage
link :billing, link: 'Платные услуги'
def go_to_billing
billing
JobMyroomBilling.new #browser
end
end
class JobMyroomBilling < JobMyroom
#some code
end
Whats wrong? Driver don't wait the element's presence
I think Sheg is giving some good advice here although I would tend to do it slightly different:
wait_until do
billing_element.visible?
end
Another thing you can do that is very similar is to replace the code in your method with this:
def go_to_billing
billing_element.when_present.click
JobMyroomBilling.new #browser
end
In this case we are waiting until the link is present and when it is the when_present method returns the element and we simply click on it.
Another thing I might suggest is using watir-webdriver as the driver instead of selenium-webdriver. watir-webdriver is built on top of selenium-webdriver but seems the have far better handling of waiting for items to actually be loaded on the DOM. If your link is being added dynamically using Ajax then you will have to write some code to wait until it is there before actually interacting with it. The only thing you will have to do to use the other gem is change the code in your Before block to this:
Before do
#browser = Watir::Browser.new :firefox
end
Another pointer I would give you is to not have one page object return an instance of the next page object. I am eating my own words here because a few years ago this is how I did it but I have found over time that this is a brittle approach. Instead, I would use the PageObject::PageFactory to manage the creation of the correct pages and when you have a step that is a generic step you can simply use the #current_page instance variable.
The last pointer I will give you is to remove the assertions from your page object. I would have the page object simply provide the abstraction and perform all verification in the step definitions. This is a much cleaner separation of concerns and will make maintaining your tests easier over time.
either you can use sleep methode from java with try catch. or implicitwait for selenium webdriver.
Webdriver never wait for the element to be displayed... basically you need the delay.
The page object when_visible method is meant to wait for Ajax events to finish loading, as mentioned here https://github.com/cheezy/page-object/wiki/Ajax-Calls.
If webdriver is trying to click on the link element before it is completely rendered, and is not Ajax related, consider waiting until the element is displayed with webdriver first. For example:
wait.until { driver.find_element(:link_text => "Платные услуги").displayed? }
#job_myroom.go_to_billing
You could (highly recommended) implicitly do this with all elements by invoking the following right after you instantiate your webdriver:
driver.manage.timeouts.implicit_wait = 3
In addition, in many situations, I have found that locating links by href performs a lot better than link_text. Perhaps these could solve your element timing issues.