checking if a html element is true with ruby and watir? - ruby

at the moment I have this:
def burnChart()
#browser.div(:id => "container").div(:id => "header").img(:class => "cogMenuHover").click
#browser.div(:id => "container").div(:id => "header").div(:class => "sbTopMenu").li(:class => "taskMenuOp", :index =>1).click
if
assert(#browser.send(type.to_sym, :class, "highcharts-grid").exists?)
puts 'Chart has been found!'
else
puts 'No chart was generated'
end
end
originally I thought I had to use to_css? but from what I've seen of others using it, that's incorrect I'm unsure.
Can anyone help me out, I just want to check if a class exists and return a true or fasle to log an error or a confirmation

I believe you want:
def burnChart()
#browser.div(:id => "container").div(:id => "header").img(:class => "cogMenuHover").click
#browser.div(:id => "container").div(:id => "header").div(:class => "sbTopMenu").li(:class => "taskMenuOp", :index =>1).click
#Due to using IE, it looks like .exists? has to be used.
if #browser.element(:class, "highcharts-grid").exists?
puts 'Chart has been found!'
else
puts 'No chart was generated'
end
end
Notice that:
The check for the element should be the condition for the if statement.
Changed the check to be .present?. .exists? returns true as long as the element is on the page, but usually you also want to check that it is visible.
The switch to #browser.element instead of #browser.send. You can do what you were doing, but you were not defining a type anywhere, so seemed unnecessary.

Related

Why can't my script access classes that are in different directory?

I am trying to access classes DealState and NotAnEndState that are in another directory, where I have a lib called move-to-go.
move-to-go folder contains modules where the one in my example is named deal_state.rb. When i open deal_state.rb it contains the following the code below.
Path to lib: F:\Ruby25-x64\lib\ruby\gems\2.5.0\gems\move-to-go-5.3.0\lib\move-to-go
module MoveToGo
module DealState
# This is the default, a deal with a status with this state is
# currently being worked on.
NotAnEndState = 0
# The deal has reached a positive end state, eg we have won
# the deal.
PositiveEndState = 1
# The deal has reached a negative end state, eg we have lost
# the deal.
NegativeEndState = -1
end
end
Path to my code: C:Users/Shahin/MigrationFolder/converter.rb
class Converter
def configure(rootmodel)
rootmodel.settings.with_organization do |organization|
organization.set_custom_field( { :integration_id => 'source', :title => 'Källa', :type => :Link } )
end
rootmodel.settings.with_person do |person|
person.set_custom_field( { :integration_id => 'source', :title => 'Källa', :type => :String} )
end
rootmodel.settings.with_deal do |deal|
assessment is default DealState::NotAnEndState
deal.add_status( {:label => '1. Kvalificering' })
deal.add_status( {:label => '2. Deal closed', :assessment => MoveToGo::DealState::PositiveEndState })
deal.add_status( {:label => '4. Deal lost', :assessment => MoveToGo::DealState::NegativeEndState })
end
end
When I execute my script I get this error message:
C:Users/MyUserName/MigrationFolder/converter.rb:63:in `block in configure': uninitialized constant Converter::DealState (NameError)
Did you mean? DEAL_SHEET
New things have however come to light. The error message seems to have an issue with the Converter-class, but i cant really interpret what it is implying.
This line is the error : assessment is default DealState::NotAnEndState.
First you should use MoveToGo::DealState::NotAnEndState and second assessment is default should be in a spec file not here.
If you juste remove this line, there should be no error anymore.

watir - ruby code "undefined method present? pr exist"

require 'rubygems'
require "watir"
browser = Watir::Browser.new :chrome,:switches => %w[--disable-notifications]
i=0;
begin
#TODO logining to the facebook
browser.goto 'linkedin.com'
browser.text_field(:id => 'login-email').set 'ananthragavendra#gmail.com'
browser.text_field(:id => 'login-password').set 'ru09ec32'
browser.button(type: 'submit').click
puts '**** linkedin logged in ****'
sea = browser.div(:class => 'nav-search-bar')
puts sea
if sea.exits?
browser.div(:class => 'nav-search-bar').click
browser.div(:class => 'type-ahead-input-container').text_field.set 'wipro'
browser.button(:class => 'nav-search-button').click
puts '**** searched, the results for wipro is shown '
else
puts 'div not present'
end
side = browser.div(:class => 'right-rail search-right-rail')
puts side
if side.present?
puts 'div present'
side1 = browser.divs(:class => 'right-rail search-right-rail')
else
puts 'div not present'
end
#TODO exception handling
rescue Exception => e
p "Error Found..... #{e.message}"
end
The error is thrown while executing the above simply, I am able to found the div in real time but I am unable to check the div using if commands, I have also tried some codes like exists.
First of all, this line if sea.exits? is wrong, change this to if sea.exists?
And your code is perfect, but very minute changes require to effectuate the WATIR's predefined wait statement.
You used this statement
side = browser.div(:class => 'right-rail search-right-rail')
If you write this way, then WATIR's predefined wait doesn't work because the method which you are going to call upon Watir::Div object is where four checking happens,
for an example, if you call
browser.div(:class => 'right-rail search-right-rail').click
then these four following checking will happen
1)exist?
2)enabled?
3)present?
4)writable?
But if you don't call any function as I have shown then it will NOT return the element because statement is getting executed even before element is loaded, So as a result of that side.present? is false and it gives you the result that div not present
So change your code in the following way
browser.div(:class => 'right-rail search-right-rail').click
side = browser.div(:class => 'right-rail search-right-rail')
puts side
if side.present?
puts 'div present'
side1 = browser.divs(:class => 'right-rail search-right-rail')
else
puts 'div not present'
end
Now your code will work perfectly. present method will return true for you.
present? is added to Object by Rails. So if you are outside of Rails, you probably mean: !side.nil?

Ruby DataMapper::ImmutableError

get '/watch/:id' do |id|
#results = Twitchtvst.all( :fields => [:Twitchtv ],
:conditions => { :user_id => "#{id}" }
)
#p #results.inspect
#results.each do |result|
puts result.id
end
erb :mystream
end
I get this error message immutable resource cannot be lazy loaded. How do I fix this?
The Error message is:
DataMapper::ImmutableError at /watch/1
Immutable resource cannot be lazy loaded
According to the official documentation:
Note that if you don't include the primary key in the selected columns, you will not be able to modify the returned resources because DataMapper cannot know how to persist them. DataMapper will raise DataMapper::ImmutableError if you're trying to do so nevertheless.
I know that you are not modifying anything here but I think that the same rule applies for lazy loading. So I will suggest to try it like that:
#results = Twitchtvst.all( :fields => [:Twitchtv, :id],
:conditions => { :user_id => "#{id}" }
) ode here
Note the id as an additional field.

Datamapper into String

I want to be able to see the string like the TwitchTV name I have in my database. Here is my current code
get '/watch/:id' do |id|
erb :mystream
#result = Twitchtvst.all( :fields => [:Twitchtv ],
:conditions => { :user_id => "#{id}" }
)
puts #result
end
result in terminal;
#< Twitchtvst:0x007fb48b4d5a98 >
How do I get that into a string (TwitchTV answer in database)
Opppppsss!
Here is the real code sample. Sorry!
get '/livestream' do
erb :livestream
#users_streams = Twitchtvst.all
puts #users_streams
end
If I add .to_s at users_stream it does not work
By adding .to_csv, not exactly a string, but it should show the content:
get '/livestream' do
erb :livestream
#users_streams = Twitchtvst.all
#users_streams.each do |us|
p us.to_csv
end
end
You're getting a Collection of Twitchtvst objects, so you need to convert each to a String:
puts Twitchtvst.all.map(&:to_s).join

Why do I get invalid attribute error when I pass in a css selector?

stackoverflow,
Here's what I'm trying to do
def get_element_from_list(root, item, index)
#browser.elements(:css => root).each do |element|
if element.present?
return element.element(:css => item, :index => index)
end
end
raise Selenium::WebDriver::Error::NoSuchElementError
end
get_element_from_list('div[class*=x-combo-list]', 'x-combo-list-item', index).click
gives me Watir::Exception::MissingWayOfFindingObjectException: invalid attribute: :css
What I don't understand is if I simply do
#browser.elements(:css => 'div[class*=x-combo-list]').each do |element|
if element.present?
return element.element(:css => 'x-combo-list-item', :index => index)
end
end
basically replacing root and item with the actual strings it works without error.
I think there might be a bug that prevents locating elements with the :css and :index locator - Issue 241.
You can work around the issue by getting an element collection and then getting the element at the specific index:
return element.elements(:css => 'x-combo-list-item')[index]
(Note that I think this css-selector might be wrong. It is probably meant to be .x-combo-list-item.)
Alternatively, assuming that x-combo-list-item is actually the element's class, you could do:
return element.element(:class => 'x-combo-list-item', :index => index)

Resources