I need to test some functionality where I need to drag and drop some UI elements.
I tried to do this on this page http://www.html5rocks.com/en/tutorials/dnd/basics/ :
browser.divs(:class => "column")[-2].drag_and_drop_on browser.divs(:class => "column")[-3]
In chrome, I don’t see anything happening.
In firefox, I see the that the mouse button is down, but nothing else happens – the element didn't move.
I tried in other pages as well and this never seemed to work.
I also tried this workaround (which is recommended in multiple threads) and it doesn’t work either:
my_element = browser.divs(:class => "column")[-4]
target = browser.divs(:class => "column")[-3]
my_element.fire_event("onmousedown")
driver = browser.driver
driver.action.click_and_hold(my_element.wd).perform
driver.action.move_to(target.wd).perform
target.fire_event("onmouseup”)
I'm using ruby 1.9.3 on mac. I also tried ruby 2.1.5 on windows and the result was the same.
Is there a way to drag and drop with watir?
This is what eventually got it to work (not fancy at all, but it did it for me):
# monkey patch for webdriver: copy and paste in IRB
module Selenium
module WebDriver
class ActionBuilder
def drag_and_drop(source, target)
click_and_hold source
move_to target, 0, 0
release target
self
end
end # ActionBuilder
end # WebDriver
end # Selenium
Related
How do I force my browser to come up on the monitor opposite Rubymine in a dual monitor setup?
I am trying to test/debug Feature Scenarios. My tool-set is RubyMine, Ruby, watir, page-object-gem, and Cucumber. I have a dual monitor setup, but the browser always comes up on the same monitor as Rubymine.
I made some changes to my hooks.rb file:
Before do
#browser = Watir::Browser.new :firefox
#browser.window.move_to(1985,24)
#browser.window.maximize
puts " Browser Position " + #browser.window.position.to_s
end
After do
#browser.close
end
This is a very clunky solution. It always comes up on the same monitor as rubymine then moves. I had to hard-code position to use 2nd monitor, so it probably won't work on a different dual-monitor setup.
Question:
If there is a config item for this, what is it?
Can you suggest a way to implement the following and where would it go?
If(dual_monitor)
then browser.move.window_to(other_monitor)
Since I write tests at home and in the office and I have dual monitors in both places, and they are different sizes, I just use an environment variable:
if ENV['MOVE_BROWSER'] =~ /home/i
browser.window.move_to(2080,0)
elsif ENV['MOVE_BROWSER'] =~ /office/i
browser.window.move_to(-2562, -600)
end
screen_width = browser.execute_script("return screen.width;")
screen_height = browser.execute_script("return screen.height;")
browser.driver.manage.window.resize_to(screen_width,screen_height)
This also ensures other testers are not affected by my preferences. I use Sublime on the largest available screen, and then have the browser on the other.
I've got a test where I need to use the action click. I can't directly click the element I need because it's a ::before element, and the element it's before is 0x14 so it's not interactable. I'm using watir-webdriver instead of selenium directly, which is why wd is used. It accesses Selenium's objects instead of Watir's.
I have an element, I am going to use the parent to find the location I need to click.
$browser.wd.action
.move_to(element.parent.wd)
.move_by(12, 0)
.click
.perform
This code works fine in Chrome, however nothing gets clicked in Firefox. Both zooms are set to 100%. Relative to the parent, the object I need to click is in the same place.
I don't know where else to look.
I've tried this with: Selenium 2.42 on Firefox 29 and Selenium 2.44 on Firefox 34.
Thanks in advance.
UPDATE
I've downgraded to FF 31.1.1 and have changed my Driver instantiation code to include native events:
if $BROWSER == :firefox
profile = Selenium::WebDriver::Firefox::Profile.new
profile.native_events = true
return Watir::Browser.new $BROWSER, profile: profile
else
return Watir::Browser.new $BROWSER
end
No luck with click.
I'm trying it on a website where I can see mouse movement and clicking: http://www.escapemotions.com/experiments/flame/. It's an interactive drawing program. The mouse moves just fine, click_and_hold seems to work,and drag_and_drop_by works fine too. I run into trouble on click.
Chaining click_and_hold.release works. I'm going to submit a defect for 2.44.
Seems to be a defect. As a work around click_and_hold and release can be chained to simulate a click.
driver.action.click_and_hold.release.perform
The defect is logged: https://code.google.com/p/selenium/issues/detail?id=8353
I'm writing tests using Selenium WebDriver and rautomation to handle system popup. I tried it on irb like following:
require 'selenium-webdriver'
require 'rautomation'
driver = Selenium::WebDriver.for :firefox
driver.get "http://rubygems.org/gems/rautomation-0.9.2.gem"
window = RAutomation::Window.new :title => "Opening rautomation-0.9.2.gem"
ok_button = window.button(:text => "&OK")
ok_button.exists?
cancel_button = window.button(:text => "&Cancel")
cancel_button.exists?
ok_button.exists? and cancel_button.exists? are returning false. Hence I can't click on the buttons.
I also tried:
window.buttons.length
to find number of buttons, but it's returning 0.
Could someone please help me why the buttons aren't detected using rautomation? Please correct me if I'm doing something wrong.
Here is a popup:
For my condition, I have to send two :tab key and then send :enter to save the file. like:
driver.get "http://rubygems.org/gems/rautomation-0.9.2.gem"
window = RAutomation::Window.new :title => /Opening/i
if window.exist?
window.activate
window.send_keys :tab;
sleep 2;
window.send_keys :tab;
sleep 2;
window.send_keys :enter
end
I don't know why I can't just save the file with:
window.activate; sleep 1; window.send_keys :enter
The problem with this dialog is that it does not use native Windows controls. When you use Spy++ or AutoIt Window Info Tool then they do not show you any controls in that window either.
When using RAutomation you can check if it has native controls on it or not like this:
win = RAutomation::Window.new :title => /Opening rautomation/
p win.present?
p win.controls.length
p win.text
win.close
The output of this script will be:
true
0
""
In other words - window was present, it had zero controls of any kind and text was an empty string. Also, closing the window really closed it which you can verify visually - this means that we were interacting with the correct window and not accidentally with some other empty window (beware: this might sometimes happen too).
This all means that you cannot interact with the controls directly with AutoIt, RAutomation or many other automation tools. There might be some specific automation tools available for handling these kind of dialogs - i'm not sure.
There is however a workaround how to work with these kind of windows - send needed keystrokes to the window. In this case, sending a return/enter key would do the trick since that is the same as clicking on the "OK" button - you can try that manually.
Here is example code, which works the same as clicking on the "OK" button:
win = RAutomation::Window.new :title => /Opening rautomation/
win.activate
sleep 1
win.send_keys :enter
I'm not sure why, but for some reason you have to activate the window manually by calling Window#activate and wait a second before sending that enter key.
After doing that a new dialog will pop up, which uses native Windows controls - you can handle that as you would have expected RAutomation to work in the first place.
However, if you would use a :ms_uia adapter instead of the default :win32 then you don't need to activate and sleep.
Here is a fully working example with :ms_uia adapter:
win = RAutomation::Window.new :title => /Opening rautomation/, :adapter => :ms_uia
win.send_keys :enter
file_dialog = RAutomation::Window.new :title => /Enter name of file/
file_dialog.button(:value => "&Save").click
To click "Cancel" on the first dialog instead of "OK" you can just use Window#close as i was using to test the window above.
I would recommend you to use :ms_uia adapter instead of :win_32 since it is getting more stable every day and will be a new default one in the far future.
To set :ms_uia adapter for default one you can use environment variable RAUTOMATION_ADAPTER before loading RAutomation itself like this:
ENV["RAUTOMATION_ADAPTER"] ||= :ms_uia
require "rautomation"
I do not see any popup when I click that link. Chrome just downloads a file. :) This could help: http://watirwebdriver.com/browser-downloads/
This code worked for me:
window = RAutomation::Window.new(:title => /Opening rautomation-0.9.2.gem/i)
window.activate
p window.exists? # => true
sleep 2
window.send_keys(:down)
window.send_keys(:enter)
I have multiple features when run together throw this error. If I run the scenarios by them self there is no issue. I think the issue is that popups are generated during the test run and are not closed properly. I have code in an After hook that closes all open windows except the very first window opened.
Error: Session [2c50a228-3ad7-a544-a6ca-5d173b86bc86] has no driver. The browser window may have been closed. (Selenium::WebDriver::Error::UnknownError)
I have added a bunch of print statements in my code to get the current state:
After:Session -> #<>Capybara::Session:0x00000100f811b8>
Before:Driver -> selenium
Before:Session Object -#<>Capybara::Session:0x00000100f811b8>
Before: (start) Driver Object #<>Capybara::Selenium::Driver:0x000001028ad790>
The scenario before this particular scenario fails, this HAS to be the culprit, but why? Can anyone point me in the right direction?
After hook
#assume ONLY last window opened is to be closed
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
page.execute_script "window.close();"
#switch back to first window opened, make it the default window now
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
Env:
capybara (2.0.3)
cucumber (1.1.9)
selenium-webdriver (2.29.0)
ruby 1.9.3p0
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :firefox
driver.get "https://www.google.co.in/"
address = driver.find_element(:link_text, "Gmail").attribute('href')
driver.execute_script( "window.open()" )
p driver.window_handles.length
p driver.window_handles.first #=> "{f17eac79-daf9-4a6c-a1ff-1b524fef9faf}"
driver.switch_to.window( driver.window_handles.last )
driver.get address
driver.execute_script "window.close()"
driver.execute_script "window.close()"
# => Window not found. The browser window may have been closed. (Selenium::WebDriver::Error::NoSuchWindowError)
In the above code,I just tried to re-generate the error,and it happened.Error is very logical as I tried to close an already closed,non existent window.
Now I want to debug this way:
p driver.browser.window_handles.length # if this is 0,then below line obvious throw error.
page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
page.execute_script "window.close();"
p driver.browser.window_handles.length # if this is 0,then below line obvious throw error.
page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
Now debug and let me know your result.
The solution for this is to maximise the window at the first place. I've not been able to find a way to maximise the window yet. In the middle of the test i manually click the maximise browser button and the test runs perfectly.
https://github.com/fahenao/_bot
I'm new to WATIR testing (and do I love it!) and have run into a wall on how to refocus my WATIR script to a newly opened window.. Here's my (simplified) script....
#!/usr/bin/ruby
require 'rubygems'
require 'watir-webdriver'
browser=Watir::Browser.new
browser.goto("http://0:3050")
browser.text_field(:name,"userkey300203830").set("OKJHNB")
browser.button(:id, "interview48").click
puts "Expected Result:"
puts "A successful display of cars"
if browser.window(:title=>"300203830").exists?
puts " Test passed. New window opened!"
else
puts " Test Failed! No window found"
end
It all works right up to the end. After the key "interview48" is clicked, a new window is opened with the title "300203830". Looks like I find it but I just don't know how to now focus on that window.
browser.window(:title => "300203830").use do
# do something
end
More information: http://watir.github.io/docs/browser-popups/
Additionally for more than 2 windows you can use the following:
browser.windows[n].use
#n is variable for which window. n will access them in order of opened or tabs from left to right
browser.windows.last.use
browser.windows.first.use
You can use the above commands if you open a new window from first browser instance and would like to toggle between the two.
There are 3 primary selectors for windows:
:title - typically the easiest
:url - often used with a Regexp value
:element - a unique element might be the least brittle (new as of Watir 6.18!)
browser.window(title: 'new window')
browser.window(url: /my_page.html/)
browser.window(element: browser.div(id: 'my-element'))
Locating by index is no longer supported
More information: Watir Browser Windows
If there is only one other window you want to use, so as of Watir 6.18, the easiest way to work with that window is with Browser#switch_window. It can only be used if there are only 2 windows, and all it does is switch to the other one, no additional locating required.
browser.switch_window