object is disabled {:element=>#<Selenium::WebDriver::Element: - ruby

We are using a drop-down to select 'Yes' 'No' values from the list. This method is being used across multiple pages. When I run my script it fails saying "Object is disabled" even though it is running fine for first drop down of the page.
Below is the code we are using
def select_value(value, field)
get_screen_element(field).options.map(&:text).include? value }
Watir::Wait.until{get_screen_element(field).select value}
end
Sometimes the script runs fine without errors sometimes it dosen't, its not consistent.

Related

Mongoid Validation Error "Email Can't be blank" when testing with rspec & Capybara, but fields are not blank

I am running into an error when writing an rSpec Capybara test to mock a user signing up for the website.
It should be noted that, unfortunately, I am writing a test for a codebase that is entirely new to me, so a lot of the code for the main program is unknown to me. If asked for something I will try and dig up the relevant code, but I'm not certain what else to include at the moment. However I can say it has been successfully running in production for a while, and I can manually test it successfully - so I think the error is probably in my test, or perhaps some configuration used just while testing.
rspec test
RSpec.describe 'New User Sign Up', type: :feature do
scenario 'valid signup inputs' do
visit("/users/sign_up")
fill_in 'user_name', with: 'TEST'
fill_in 'user_email', with: 'TEST#test.com'
fill_in 'user_username_with_caps', with: "TEST"
fill_in 'user_password', with: 'TESTpw123'
puts find_field('user_email').value
puts expect(find_field('user_email').value).to eq 'TEST#test.com'
puts expect(page).to have_selector("input[value='TEST#test.com']")
puts page.should have_field('user_email', with: 'TEST#test.com')
click_on 'Create an account'
end
end
The output is:
bgc#jadzia:~/Documents/Work/BadgeList/code/badgelist/backend/spec$ bundle exec rspec sign_up_spec.rb
TEST#test.com
true
true
true
F
Failures:
1) New User Sign Up valid signup inputs
Failure/Error: click_on 'Create an account'
Mongoid::Errors::Validations:
message:
Validation of User failed.
summary:
The following errors were found: Email can't be blank
resolution:
Try persisting the document with valid data or remove the validations.
Note the puts statements and the corresponding output before the error message.
As far as I can tell, the fields ARE getting filled in properly. However, somehow this does not get recognized when it attempts to complete the sign up. The error is coming from Mongoid, so somehow mongo reacts differently to an testing auto-entered field vs. a manually entered one.
It should also be noted that, if I disable database_cleaner-mongoid, and run the same test twice... I get a -DIFFERENT- outcome. The test technically passes, but there is a warning prompt on the page that says "This email is taken".
So, somehow the value in the email field is...
Being entered/read properly when directly querying the field value on the page
Not recognized when it immediately afterwards tries to use that value to Create an account, instead the field is seen as blank.
But ALSO the field is successfully saved into the DB for a new account with this information, so running the same test again creates a conflict with the entry from the previous test if the DB is not cleaned first.
All click_on does is click on the button. It doesn't wait for the server to do anything, or anything on the page to change, etc. So ending the test with click_on isn't actually testing for any behavior, and the DB is going to get reset while the action triggered by click_on is still occurring, which is unlikely to be what you want. You need to add an expectation after the click_on testing for what you expect to see in the page like
...
click_on 'Create an account'
expect(page).to have_text('User created!!!)
Also note that the expectations you are calling puts on aren't actually defined to return anything specific, so the fact you're seeing true really is just luck. They're defined to not raise an error when successful, and raise an error when not.

Don't run a cucumber feature if a element on the page is not available but exit without failing

I want to check a page for an element before I start running the feature file for it (It's an element that periodically appears with an event so I only want to run the feature if its present).
The approach I wanted to use was a tagged before hook to see if the element was present and if it wasn't just don't run the feature but exit without 'failing' the step just exit with a message. I tried variants on the below but
1. If I don't have a rescue clause it obviously fails the scenario when the element isn't present
2. If I do have the rescue clause it handles it and passes moving onto the features which will then fail as the event isn't available.
Is there a way to halt running the feature file if the rescue clause is invoked without the 'fail'?
Before('#event') do
begin
find('.event').visible?
rescue Capybara::ElementNotFound
puts 'THE EVENT IS NOT ON'
end
end
You shouldn't be using find if you want to make a decision based on existence. Instead you should be using the predicate methods provided by Capybara (has_selector?, has_css?, has_xpath?, etc) so you don't have to rescue exceptions.
The other thing to know is the Cucumber skip_this_sceanrio method, which means you should end up with something like
Before('#event') do
# visit '/some_page' # May not be needed if you have another `Before` already visiting the needed page
skip_this_scenario('Skipping due to missing event') unless page.has_css?('.event')
end

Capybara find_field & has_selector not working

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')

Item missing from WebList

I am writing auto test scripts in QTP (UFT).
I have multiple columns in an external datasheet which could contain data or be blank. I was trying write some code, that if it was blank to click a submit button, if not blank then add in the fields. Please see code below:
If IsNull(DataTable("Available_Qualifications_1", dtLocalSheet)) = False then
Browser("Create Qualification Types").Page("Create Qualification Types").WebList("qavailable").Select DataTable("Available_Qualifications_1", dtLocalSheet)
Browser("Create Qualification Types").Page("Create Qualification Types").Link("Add Qualifications").Click
ElseIf IsNull(DataTable("Available_Qualifications_1", dtLocalSheet)) then
Browser("Create Qualification Types").Page("Create Qualification Types").WebButton("Submit").Click
End if
However, I receive the error below:
Cannot identify the specified item of the qavailable object. Confirm that the specified item is included in the object's item collection.
Line (16): "Browser("Create Qualification Types").Page("Create Qualification Types").WebList("qavailable").Select DataTable("Available_Qualifications_1", dtLocalSheet)".
What UFT is saying is that you're trying to set a value into the WebList which isn't one of the WebList's options.
Try outputing the value to see if UFT is correct, if it is then correct your test (by entering the correct values in the data table). If it's not correct you can try using the index by using the Select "#3" syntax (and report the problem to HP's support).

Grails filter stops working after

I have a filter set up as follow to control users login status.
class SecurityFilters {
def filters = {
login(controller:'login|logout|proxy|API|error', action:'*', invert: true) {
before = {
if (!session.isLoggedIn){
switch(controllerName){
case "enroll":
switch(actionName){
...
default:
log.warn "Permission Denied. Default action for enroll."
render(view: '/permissionDenied', model: [message: "You must be logged in to access the enroll system. If you are a consumer, please contact your agent for more information."])
break
}
break
...
}
else {
switch(controllerName){
case "agent":
if (!session.user.isAgent) {
render view: "/permissionDenied", model: [message: 'This portion of the site is only available to agents.']
return false
}
break
....
}// switch
}// else
}// before
...
}// login
}// filters
The problem I am having is that when I run this in development it works fine but when I run it on our QA system it works fine for a while and then suddenly it stops working correctly.
I added logging and I can see that the session information is available in the filter and the session variable (session.user.isAgent) is set correctly (true) but the code inside the if(!session.user.isAgent) gets executed regardless.
I can's seem to find the cause for odd behaviour.
My question is has anyone seen this behaviour before and how did they solve it or have any ideas of where to look for probable cause for the sudden change in the way the filter is working.
Thanks in advance.
UPDATE (02/19/2014):
After adding more logging in an effort to hunt down the cause the filter did not execute the code in the if(!session.user.isAgent) as it was doing before. Now it runs normally and then executes only the render line for when the user is not logged in. The logging still show that the user is logged in and that (s)he is an agent but then it runs the render but not the lines of code above it. It is as if there was a "goto" the render line after it completes checking if the agent is logged in.
Again any information or solution would be appreciated
I've had a few issues with filters and Groovy truth. The problem I was seeing was that no errors were logged, even with aggressive exception catching (i.e. catching Throwable) and the only output in the browser was a blank page. This seems to happen only in Filters- everywhere else the errors get logged.
In my case, the issue was down to Groovy truth. I was trying to set a Boolean attribute on the session, but every time I did this it failed. In the end I had to convert the value to a String, and then set it, and it worked.
I know this isn't a direct answer, but I've been bitten a few times by the above and for example, lazy GString evaluation.
If you're still debugging, I'd log some output showing the underlying Class type of what you think you're dealing with. It may be that when your boolean conditions are evaluated above an exception is being thrown and swallowed. I'd log the actual values of each of your conditional statements to see what they are. Also remove each line one by one, to see if the failure goes away. And/or replace your conditionals with absolute values i.e. true/false to see if the code gets executed. If it does, it points to an error in the current conditional evaluation.

Resources