Capybara : Ruby : not detecting popups - ruby

I have an application where when i click a link (link1) it opens popup, below is the code for the popup
link1
i tried using
page.driver.browser.window_focus page.windows.last.handle
also i tried using
new_window = window_opened_by{click_link link_text}
within_window new_window do
click_link 'overridelink'
end
both of them are not working so finally i tried to get the windows ids
print page.driver.browser.window_handles
result
["ccc7074b-dad0-472f-b134-a6c72e08f7a4"]
so it looks like capybara is not even detecting second window
can you guys help me in this issue & also if there are any work arounds

You've probably run into the following issue with selenium-webdriver - https://github.com/SeleniumHQ/selenium/issues/1273

Related

Ruby Selenium/Watir ".select" does not do anything in Safari (any version)

I'm having a painful time trying to select a value in a select field in Safari. It works fine in Chrome, Firefox and IE.
It finds the object fine, but it doesn't select any value. It also doesn't return any error.
I've also tried this on multiple websites with select fields (same issue):
launch browser
browser = Watir::Browser.new :safari, url: "http://localhost:4444/wd/hub"
Set object (works fine)
elTitle = #browser.element(xpath:'//select[#name="ddTitle"]')
Now things i've tried to select the value:
elTitle.option(:text => "Mrs").select
above returns nil, and nothing actually selected
also tried:
elTitle.options[1].click
above returns nil, and nothing actually selected
also tried:
elTitle.send_keys "Mrs", :tab
(which often results in the listbox options displayed (with the correct value with a tick next to it), but it's on top of the browser and prevents any further code from continuing - but this is another issue altogether)
Ruby: 2.2.6
safari v11.0.3 (High Sierra)
watir gem: 6.10.3
selenium-webdriver: 3.9.0
selenium-standalone-server: 3.9.1
Originally had older versions of these gems, which had the same issue, so updated them, and still have the issue. Had also tried with earlier version of Safari (v10/Sierra), which had the same issue.
I've also been unable to launch Safari Technology Preview (but this is an issue i'll raise separately)
So, in summary, has anyone been able to do ".select" on a select field in Safari?
YOu are doing it wrong, you are calling option method for wrong object.
Try this syntax for select_list always
b.select_list(name: "ddTitle").select "Mrs"
Other syntax is
b.select_list(name: "ddTitle").option(text: "Mrs").select
option method has to be called for Watir::Select object not for Watir::HTMLElement, you are actually calling for Watir::HTMLElement, so it's not working.

Firefox "Mouseover" issues in Selenium Ruby bindings on Windows

I've been struggling with mouse hover for a couple of days and found a couple of threads here on the topic, but none have helped. I've tried dozens of different approaches and have also modified my code to be more in sync with the examples here, especially Dave Haeffner's suggestions. The current code looks like:
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "ADMIN").displayed?
end
#driver.action.move_to(#driver.find_element(link: "ADMIN")).perform
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "ORGANIZATION").displayed?
end
driver.action.move_to(#driver.find_element(link: "ORGANIZATION")).perform
Selenium::WebDriver::Wait.new(timeout: 2).until do
#driver.find_element(link: "TEAMS").displayed?
end
#driver.find_element(link: "TEAMS").click
end
On macs, this code works fine. On Windows however, it produces:
Failure/Error: #driver.action.move_to(#driver.find_element(link: "ADMIN")).perform
Selenium::WebDriver::Error::InvalidElementStateError:
Cannot perform native interaction: Could not load native events component.
I'm sure the element access is fine, because if I change the first mouse hover to a click action, it works great.
Any help would be appreciated.
You need to use Firefox version 31.0.6 . Versions of Firefox after that don't have the native events support. If you need to use a later version of Firefox, then just make sure your test actions are all non-native, such as using a JavascriptExecutor to create a hover (instead of relying on native events in the firefox driver).

Invalid Xpath 2 error for 'Click' selenium function

I have ran a test using Selenium IDE 2.3.0 and copied the Junit4/Remote control script into the Eclispe Classic 4.2.2 and started running my code as 'Junit4'.
I see the following error:
Invalid Xpath 2
The selenium command in Eclipse for Xpath is:
selenium.click("//input[#value='Search Equipment']");
I tried copying the Xpath using Firebug which was:
/html/body/section[2]/section/div/div/div/form/div[2]/div[2]/div/span/input
But I get the same error.
start selenium record and enter some thing to ur textbox or if its a button click the button then click stop.you can get the correct xpath by going to target in the relevant step in selenium IDE.use last xpath.
As per your question, i understood that you need to click on the button "SearchEquipment".
So try with this xpath :
selenium.click("//span[#id= 'rba-keyword-submit']/input")

Headless Browser in ruby, non testing purpose

What are the options for using Headless browser in ruby? Already Tried Watir, but it opens a browser window for every page opened, that is un desired side effect. It would be awesome if there was something like Phanthom JS or if i could Phanthom JS in ruby code.
First, Watir does not "open a browser window for every page" as you describe, unless you are doing something wrong.
Second, you can use phantomjs with Watir. Uninstall the Headless gem, and just do
broswer = Watir::Browser.new :phantomjs
browser.goto "some_web_page.com"
browser.close # When you are done.
I suspect you are opening a new browser instance for every page, rather than just using goto. If that's the case, try using .new() or .start() just once, then .goto() after that. That is, just replace :phantomjs in the code above with :ff for example.
Keep in mind that if you use "headless", last I heard it did not yet work on OS X.

Unable to trigger mouse event in Capybara test

I am using Capybara 1.0.0, and I have a link in my page which gets visible when mouse hover over that block. So I want to trigger mouse over in test so that I can click that hidden link.
I googled it, but couldn't find the solution that suits me. Can you guys help me with this?
I've chosen to use Capybara webkit, and sadly I had to resort to executing javascript using jQuery:
page.execute_script('$(".ClassSelector").trigger("hover")')
This blog has the answer:
http://aokolish.me/blog/2012/01/22/testing-hover-events-with-capybara
page.find('#element').trigger(:mouseover)
This doesn't work with the selenium driver though:
http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Element#trigger-instance_method
This commit added the hover method. It has the advantage of including css hover effects on elements.
Usage:
find('#posts .post .comments .comment').hover
I had a lot of trouble getting this to work as well. There's a lot of conflicting information on the web. Here's how I did it:
Setting: rails 3.2.1, capybara 1.1.2, rspec 2.8.0, selenium-webdriver 2.20.0
page.execute_script "$('tr.user-#{user.id}').trigger('mouseover')"
This will cause the previously hidden links to appear (by virtue of jQuery's hover function), which are then accessible for have_link and click_link.
Note: you do not want to write page.evaluate_script as this won't work.
Edit: Well I just realized that #kelly-sutton's solution is the same as mine. So I can confirm this.
For visibility problems sometimes it helps to change window size for poltergeist. I've done it
in spec_helper.rb
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, window_size: [1280, 600])
end
As well as using either find('#element').hover or page.execute_script "$('#element_2').trigger('mouseover');" one must also pass js: true to the describe block in order to turn on javascript (unless you've done that in your configuration). This tripped me up for a bit.

Resources