how can i use watir to test ruby chosen dropdown list jquery - ruby

I need to test a ruby page which has some chosen dropdown list and i don't succeed to select an option from the list. the options are not shown in firebug until i choose one of them.
can anyone help?

This will make the dropdown to appear:
require "watir-webdriver"
browser = Watir::Browser.new
browser.goto "http://harvesthq.github.com/chosen/"
browser.div(id: "selEA2_chzn").a.click

Related

How to scrape website with search button

I have this website: codigos if You look at it, it has a selection field at left, and a go button at right, I need to scrape some of the items on left.
But, how can I tell to mechanize in ruby how to access that selection field and then make the search and scrape it?
I've seen examples with login forms but I don't know if it can really suit this case though.
The <select> tag is contained within a <form> tag, so you need to locate the form and then you can set the option by passing the name of the select list and specifying the appropriate option:
require 'mechanize'
mechanize = Mechanize.new
page = mechanize.get('http://comext.aduana.cl:7001/codigos/')
form = page.forms.first
form["wlw-select_key:{actionForm.opcion}"] = "Aduana"
result_page = form.submit
result_page.uri #=> http://comext.aduana.cl:7001/codigos/buscar.do;jsessionid=2hGwYfzD76WKGfFbXJvmS2yq4K19VnZycJfH8hJMTzRFhln4pTy2!1794372623!-1405983655!8080!-1

Unable to click on link using Link text/Partial Link text using ruby

I am new here and even to Ruby and Selenium. I am trying to click a link on web page which has following code:
<a> target="mainFrame" href="dynamic_Utility_Index.htm">Dynamic Utilities</a>
So basically I want to click on this dynamic utilities. The script which I have written so far is:
require 'selenium-webdriver'
require 'win32ole'
driver = Selenium::WebDriver.for:firefox
driver.manage().window().maximize();
driver.navigate.to 'xyz.com'
wait = Selenium::WebDriver::Wait.new(:timeout =>10) # seconds
#Click on Dynamic Utilities
wait.until{driver.find_element(:link_text,'dynamic_Utility_Index.htm').click}
puts "Clicked"
I have even used link, partial_link_text in place of link_text but keep getting the following error
(Unable to locate element: {"method":"link
text","selector":"dynamic"})
(Selenium::WebDriver::Error::TimeOutError)
I am using Ruby and Selenium Web driver.
Did you try with:
wait.until{driver.find_element(:link_text,'DYNAMIC UTLITIES').click}
?? Sometimes I had to use the text link by capitalized.
Another option could be:
driver.find_element(:xpath, "//a[contains(#target, 'mainFrame')]").click
Hope works for you :D
The item you consider to be a link text (dynamic_Utility_Index.htm) actually just a value of href attribute. Actual link text is "Dynamic Utilities". Also you can use xpath locator //a[#href="dynamic_Utility_Index.htm"] instead of search by link text:
wait.until{driver.find_element(:xpath,'//a[#href="dynamic_Utility_Index.htm"]').click}

Watir-webdriver throws 'not clickable' error even when element is visible, present

I am trying to automate tests in Ruby using the latest Watir-Webdriver 0.9.1, Selenium-Webdriver 2.53.0 and Chrome extension 2.21. However the website that I am testing has static headers at the top or sometimes static footers at the bottom. Hence since Watir auto-scrolls an element into view before clicking, the elements get hidden under the static header or the static footer. I do not want to set desired_capabitlites (ElementScrollBehavior) to 1 or 0 as the websites I am testing can have both - static header or static footer or both.
Hence the question are:
1) Why does Watir throw an exception Element not clickable even when the element is visible and present? See ruby code ( I have picked a random company website for an example) and the results below.
2) How can I resolve this without resorting to ElementScrollBehaviour?
Ruby code:
require 'watir-webdriver'
browser = Watir::Browser.new :chrome
begin
# Step 1
browser.goto "shop.coles.com.au/online/mobile/national"
# Step 2 - click on 'Full Website' link at the bottom
link = browser.link(text: "Full website")
#check if link exists, present and visible?
puts link.exists?
puts link.present?
puts link.visible?
#click on link
link.click
rescue => e
puts e.inspect
ensure
sleep 5
end
puts browser.url
browser.close
Result:
$ ruby link_not_clickable.rb
true
true
true
Selenium::WebDriver::Error::UnknownError: unknown error: Element is not clickable at point (460, 1295). Other element would receive the click: div class="shoppingFooter"...div
(Session info: chrome=50.0.2661.75)
(Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Mac OS X 10.10.5 x86_64)>
http://shop.coles.com.au/online/mobile/national
thanks!
You can do a click at any element without getting it visible. Check this out:
link.fire_event('click')
BUT It is very very very not good decision as far as it will click the element even if it is not actually visible or in case when it is just impossible to click it (because of broken sticky footer for example).
That's why much better to wait the fooler, scroll the page and then click like:
browser.div(id: "footerMessageArea").wait_until_present
browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
link.click
The sticky footer is blocking webdriver from performing the click, hence the message that says 'other element would receive the click'.
There are several different ways you can get around this.
Scroll down to the bottom of the page before the click
Hide/Delete the sticky footer before any/all link clicks
Focus on an element below the element you want to click before you perform the click
I Guess your element is visible in the screen.
Before clicking on the element first you have to scroll the webpage so that element is visible then perform the click. Hope it should work.
I had similar issue,
I just used following javascript code with watir:
link = browser.link(text: "Full website")
#browser.execute_script("arguments[0].focus(); arguments[0].click();", link)
Sometimes I have to use .click! which i believe is the fire_event equivalent. Basically something is layered weird, and you just have to go around the front end mess.

Capybara with PhantomJS / Poltergeist: Selecting value from drop-down doesn't trigger Ajax call

For non-commercial purposes I am trying to build a scraper. I have worked with PhantomJS before, and it usually works like a charm. However, I have stumbled upon the following problem:
What I want to do:
Go to http://de.soccerway.com/national/germany/bundesliga/2010-2011/regular-season/r11840/
Click on the button 'per Spieltag'
Then select an option from the drop-down menu that holds the matchdays
Wait for Ajax call to be complete and then get the information I need
Unfortunately, I can select the option I want to select, but nothing happens afterwards. I created a mini script to demonstrate this behavior:
require 'capybara/poltergeist'
require 'capybara/dsl'
include Capybara::DSL
Capybara.register_driver :poltergeist do |app|
Capybara::Poltergeist::Driver.new(app, :js_errors => false)
end
Capybara.default_driver = :poltergeist
Capybara.javascript_driver = :poltergeist
Capybara.default_wait_time = 20
Capybara.ignore_hidden_elements = true
Capybara.current_session.driver.resize(1200, 1000)
visit('http://de.soccerway.com/national/germany/bundesliga/2010-2011/regular-season/r11840/')
# click on button 'per Spieltag'
find(:xpath, ".//a[text() = 'per Spieltag']").click
# drop down menu with matchdays
matchday_selector = find(:xpath, ".//select[#name='page']")
# save screenshot 'before'
save_screenshot('before.png', full: true)
# for demonstration purposes, select option with value '5'
matchday_selector.find(:xpath, ".//option[#value='5']").select_option
# wait some time for ajax call to be complete and save screenshot 'after'
sleep 5
save_screenshot('after.png', full: true)
If you compare the before and after screenshots, you will see that the number of the drop-down menu has changed, but the content of the table didn't. Also, the dropdown menus are not displayed at the correct position, but instead are shown in the top left corner of the screenshot.
If I use Selenium as my default driver, then it works, but I'd rather user PhantomJS because it is much faster in my experience.
How can I get PhantomJS to trigger the Ajax call?
I use PhantomJS 1.9.1, Capybara 2.1.0 and Poltergeist 1.3.0

how do I loop a click action(refresh) until a specific string is found?

I am writing a watir script where I need to loop a click action untill a text is found. Please let me know how to do this.
thanks
You could do something like this:
require 'watir'
b = Watir::Browser.new
b.goto('http://www.example.com')
until b.text.include? "example"
b.refresh
end
But watir and watir-webdriver have methods to handle dynamic page elements. This link has examples on waiting with watir-webdriver.

Resources