Alert box in Waitr Webdriver - macos

I had a look here: http://wiki.openqa.org/display/WTR/JavaScript+Pop+Ups
Every solution is for IE on Windows. I am using Firefox on Mac. Is there a way to click on OK of a JavaScript alert box?

Proper handling of alerts and prompts is still being worked on in WebDriver, but a common workaround is to overwrite the window functions using execute_script(), i.e.
browser.execute_script("window.alert = function(msg) { window.lastAlert = msg; }")
browser.button(:id => "trigger-alert").click
browser.execute_script("return window.lastAlert") #=> "the message"
Since I'd like to avoid a bunch of monkey patches floating around (a common problem in the Watir community), I've added some helper methods as an optional require - after the next release you should be able to do:
require "watir-webdriver/extensions/alerts"
browser.alert do
browser.button(:id => "alert").click
end #=> "the alert message"
browser.confirm(true) do
browser.button(:id => "confirm").click
end #=> "the confirm message"
browser.prompt("returned value") do
browser.button(:id => "prompt").click
end #=> { :message => "foo", :default => "bar" }
Note that this is temporary and the API may be removed in the future when the issue is resolved in WebDriver.
UPDATE:
Proper alert handling is now implemented. The above example would now be done like this:
browser.button(:id => "alert").click
browser.alert.ok
browser.button(:id => "confirm").click
browser.alert.ok # or browser.alert.close
browser.button(:id => "prompt").click
alert = browser.alert
alert.text #=> "foo"
alert.ok

Related

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?

RAutomation Drag Mouse and Select Text

I am using the Ruby gem Rautomation to test a windows based application. The application is running behind a remote Citrix server and therefore I am not able to interact with the app in the usual way. I cannot access any elements on the app using a scraper like windows inspect.exe. What i would like to do is use the keyboard and/or mouse to select text on the app, and then copy it to a file for verification purposes.
Here is a code snippet of what i would like to do:
window = RAutomation::Window.new(:title => /Manager App/i, :adapter => 'ms_uia')
window.move_mouse(60,95)
window.click_mouse_and_hold # is there a 'hold' method??
window.move_mouse(80,95)
window.release_mouse # is there a 'release' method??
window.send_keys [:left_control, 'c']
OR
window.move_mouse(60,95)
window.click
window.send_keys :shift # hold this key down somehow??
window.move_mouse(80,95)
window.release_shift # is there a 'release' key method??
window.send_keys [:left_control, 'c']
What I would like to do is drag the mouse to select a section of text on this app, OR select the start of some text, hold shift, then select the end of the text i need. Basically replicating how a user would select and copy text in real life but through Rautomation. Is this possible?
Thanks
I have not tested it, but with win32 and autoit adapters you should be able to do it with Mouse#press and Mouse#release. Here's a spec for:
it "#press/#release" do
window = RAutomation::Window.new(:title => "MainFormWindow")
window.maximize
text_field = window.text_field(:index => 2)
text_field.set("start string")
text_field.value.should == "start string"
mouse = window.mouse
mouse.move :x => 146, :y => 125
mouse.press
mouse.move :x => 194
mouse.release
window.send_keys [:control, "c"]
text_field.set("new string")
text_field.value.should == "new string"
mouse.move :x => 146
mouse.press
mouse.move :x => 194
mouse.release
window.send_keys [:control, "v"]
text_field.value.should == "start string"
end
Check out documentation for RAutomation to help you even more.

Rhomobile Migration from Rho3.1 to Rho 4.0 Alert Msg. Error

I am trying to migrate my rho application from 3.1 to 4.0. In 3.1 i have defined alert using Alert.show_popup :title => "Please Wait", :message => "Fetching Data..." But as specified in documentation now i have changed it to the
dataPopProps = Hash.new
dataPopProps['message'] = "Fetching Data...";
dataPopProps['title'] = "Please Wait";
Rho::Notification.showPopup(dataPopProps)
But i am still getting the same error.
Error: Button list has been incorrectly defined. DIalog will not Launch
Any help will be great.
Try like this,
dataPopProps = Hash.new
dataPopProps['message'] = "Fetching Data...";
dataPopProps['title'] = "Please Wait";
dataPopProps['buttons'] = ["Ok"]
Rho::Notification.showPopup(dataPopProps)
For future reference, the official docs are often incorrect, so working with Rhodes can be frustrating. However, the example listed here seems to be a good solution. Note that the example for Notification is in Javascript.
Here is an elegant way to write this in Ruby:
dataPopProps = {
'message' => 'Fetching Data...',
'title' => 'Please Wait',
'buttons' => [{ :id => 'no', :title => 'no' }]
}
Semicolons are eggregiously unecessary in Ruby, and you can clean up your code by using literals instead of Hash.new.

Blinking string in curses application

In the meagre documentation for ruby curses I found this method
A_BLINK
Blinking
See ::attrset
However, I don't know how to utilize it.
win1 = Window.new
win1.addstr.a_blink "Blinking" #=> error
Please don't blame me, there is literally no help on google regarding curses. Honestly, at least not for ruby.
You can set attributes with Curses::Window#attrset. Here's an example:
require "curses"
include Curses
init_screen
begin
attrs = {
A_NORMAL => 'Normal display (no highlight)',
A_STANDOUT => 'Best highlighting mode of the terminal.',
A_UNDERLINE => 'Underlining',
A_REVERSE => 'Reverse video',
A_BLINK => 'Blinking',
A_DIM => 'Half bright',
A_BOLD => 'Extra bright or bold',
A_PROTECT => 'Protected mode',
A_INVIS => 'Invisible or blank mode',
A_ALTCHARSET => 'Alternate character set',
}
attrs.each { |a, s|
attrset(a)
addstr("#{s}\n")
}
refresh
getch
ensure
close_screen
end

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

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.

Resources