Capybara can't find svg element when using ID - ruby

The following element is on the page:
<use xlink:href="pending">
<svg id="pending">
Using this: page.should have_css('svg', :id => 'pending', :count => 1)
Or this: page.should have_css('svg[id="pending"]', :count => 1)
Is returning no matches.
I have tried: page.should have_css('svg', :count => 1)
Which does return matches, but what i need to find is the specific element with the ID of "pending".

How about using has_selector? Since your ID's should be unique across the whole document, no count should be necessary.
page.should have_selector('#pending')
has_selector? docs

Related

How to get Span collection by id in Watir

Is there a method in watir to get list of all the span collection by id. I can traverse the span tags only by index and one item at a time.
i want to get the entire collection of the span tags and loop through it.
irb(main):060:0> ie.span(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl132_lblSubject/).text
=> "30 stuff"
irb(main):065:0> ie.span(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl/).exists?
=> true
irb(main):073:0> spans=ie.span(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl/ ,:index=>3)
=> #<Watir::Span: located: false; {:id=>/ctl00_ContentPlaceHolder1_DataList1_ctl/, :index=>3, :tag_name=>"span"}>
irb(main):074:0> spans.text
=> "Well, morning."
irb(main):075:0>
Yes, you can iterate. Write the following code
ie.spans(:id =>/ctl00_ContentPlaceHolder1_DataList1_ctl/).each do |span|
p span.text
end
It will print all the span's text which is matching your id.

Insert value in a text field using Ruby - watir/selenium

I am trying to insert a number in the 'Mine Id' using Ruby - Watir/Selenium
Somehow the field <input> field is not being recognized as a text box and hence I am unable to enter values through the code.
The id 'inputdrs' is used multiple times on the same page.
Any suggestions how to achieve it.
The URL is this
http://www.msha.gov/drs/drshome.htm
The below don't work:
#browser.text_field(:id => /inputdrs/, :index => 2).set("3607277")
browser.text_field(:name, "inputdrs").set("3607277")
Thank you for your help
require 'watir-webdriver'
$browser = Watir::Browser.start "http://www.msha.gov/drs/drshome.htm"
a = 0
b = $browser.text_fields.length
while a < b
$browser.text_fields[a].set a
a += 1
end
This will put the value of a in each text field on this page. I REALLY drew out the loop so you can see whats going on. This isn't as dynamic as you would like it, but if the page has the same amount of text_fields you should be fine.
OR you can do something like..
$browser.text_field(:name => "MineId").select
$browser.send_keys "hello"
Problem
The html of the Mine ID field is:
<input size="8" maxlength="8" name="MineId" onclick="this.value='';" id="inputdrs" align="middle" type="number">
The line:
#browser.text_field(:id => /inputdrs/, :index => 2).set("3607277")
Will fail because it is inputting into the wrong text field. If you get all of the text fields with that id:
browser.text_fields(:id => 'inputdrs').collect(&:name)
#=> ["q", "MineId", "OperSearch", "MineName", "CntctrId", "CntCtrSearch", "Controller"]
You can see it is the second field. However, because Watir is using a 0-based index, you actually get the OperSearch field. This would have worked by using an :index => 1 instead.
The line:
browser.text_field(:name, "inputdrs").set("3607277")
Will fail because the "inputdrs" is the value of the id attribute and not the name attribute.
Solution
Given that the id attribute value is not unique for this page, you should probably not use it for locating. Instead, use something unique, such as the name attribute.
browser.text_field(:name => "MineId").set("3607277")

How can I do a full text search with a phrase of more than one word, using Sequel full_text_search?

I am using ruby, Sequel and postgres. I have implemented full text search as follows:
get '/full_text_search' do
search_term = params[:search_term]
result = DB[:candidates].full_text_search(:experience, search_term).map do |row|
{ :id => row[:id], :first => row[:first], :last => row[:last], :designation => row[:designation], :company => row[:company], :email => row[:email], :phone => row[:phone], :city => row[:city], :country => row[:country], :industry => row[:industry], :status => row[:status], :remarks => row[:remarks], :experience => row[:experience] }
end
halt 200, result.to_json
end
This is executed by passing a search term from a text box, via an ajax call, so:
$(document).ready(function(){
$("#full_text_search").click(function(){
var search_term = $("#search_box").val();
$.getJSON("/full_text_search?search_term="+search_term, function(data) {
$.each( data, function( key, value ) {
//do something with returned data here
});//end each
});//end getJSON
});//end click
});//end doc
If my search term is just one word, "private" for example, I get the results back no problem but if I try a search term of 2 words (or more) "private equity", for example, I get an error and no data is returned.
So my question is how can I execute full text search (as above) with a phrase of 2 or more words?
I have already tried encapsulating the passed parameter with '' and "" unsuccessfully.
All help gratefully received. Thank you.
PostgreSQL's full text searching does not handle phrase searching natively. The best you can do is look for entries containing both words, using &: full_text_search(:experience, 'term1 & term2'), followed by using LIKE '%term1 term2%' if you want to do actual phrase searching.
I'll look into adding an :phrase=>true option to full_text_search that will make it operate as a phrase search.

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)

Firewatir: Firewatir dynamic drop down issue

I am having any issue with selecting any item from the drop down. Below is the HTML from our site. The HTML looks like this
<div class="x-form-field-wrap x-trigger-wrap-focus"
id="ext-gen157" style="width: 170px;"><input type="hidden"
id="parentEntity" name="parentEntity" value=""><input type="text"
id="cmbParentEntityId" autocomplete="off" size="24" class="
x-form-text x-form-field x-form-focus" style="width: 145px;">
<img class="x-form-trigger x-form-arrow-trigger"
src="../ext/resources/images/default/s.gif" id="ext-gen158"></div>
So I have created a watir code which looks like this:
#browser.text_field(:id,"cmbParentEntityId").set("1")
which search for all the accounts starting with 1.Once the value is set to 1, the drop down is showing only accounts starting with 1. Below is the HTML code from the drop down
<div class="x-combo-list-inner" id="ext-gen336" style="width:
248px; overflow: auto; height: 40px;"><div class="x-combo-list-item
x-combo-selected">10_12_2010</div><div
class="x-combo-list-item ">10_13_2010</div></div>
Based on the above code I have created the Watir code
#browser.div(:class => "x-combo-list-inner", :text => "10_12_2010").click
But nothing is happening, I have searched the web but couldn't find any answers, I really appreciate that if anyone can help me to point to right direction.
Thanks
What do you mean that nothing is happening? If i try the code provided by you, then i'll get an expected UnknownObjectException:
irb(main):003:0> b.div(:class => "x-combo-list-inner", :text => "10_12_2010").click
Watir::Exception::UnknownObjectException: Unable to locate element, using {:class=>"x-combo-list-inner", :text=>"10_12_2010"}
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:57:in `assert_exists'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:315:in `enabled?'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:62:in `assert_enabled'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:259:in `click!'
from c:/Ruby/lib/ruby/gems/1.8/gems/watir-1.6.6/lib/watir/element.rb:229:in `click'
from (irb):3
That is because you're trying to find a div element with a class of "x-combo-list-inner" and a text of "10_12_2010". There isn't such an element. See this:
irb(main):007:0> b.div(:class => "x-combo-list-inner").text
=> "10_12_2010\r\n10_13_2010"
Text of "x-combo-list-inner" includes texts for every child element. You could search for that particular child element like this:
irb(main):008:0> b.div(:class => "x-combo-list-inner").div(:text => "10_12_2010").html
=> "\r\n<DIV class=\"x-combo-list-item
x-combo-selected\">10_12_2010</DIV>"
Or with regexps:
irb(main):009:0> b.div(:class => "x-combo-list-inner", :text => /10_12_2010/).text
=> "10_12_2010\r\n10_13_2010"
And when it comes to clicking then you have to know which exact div you need to click - is it the first one, or the second one. Also, if nothing happens then you have to find out what JavaScript events are binded to these elements exactly and then fire events manually:
irb(main):010:0> div = b.div(:class => "x-combo-list-inner").div(:text => "10_12_2010")
=> #<Watir::Div:0x5846088 located=false how={:text=>"10_12_2010"} what=nil>
irb(main):013:0> div.fire_event("onmousedown")
=> nil
irb(main):014:0> div.fire_event("onmouseup")
=> nil
My guess is that some JavaScript event should be explicitly fired. See How to find out which JavaScript events fired?

Resources