How to find items in a lightbox / fancybox with Capybara - html

So with my lightbox recognized, how do I access what is inside of it?
When I type:
within("div.fancybox-wrap.fancybox-desktop.fancybox-type-iframe.fancybox-opened") do
within(:xpath, "//table[#id='small_calendar'][0]") do
page.find('td', :text => '5').click
end
end
For
<table id="small_calendar">
I get...
Unable to find xpath "//table[#id='small_calendar'][0]" (Capybara::Element
NotFound)
Modified search....
sleep(10)
within("div.fancybox-wrap.fancybox-desktop.fancybox-type-iframe.fancybox-opened") do
within_frame('fancybox-frame') do
#within(:xpath, ".//table[#id='small_calendar'][0]") do
page.find(:xpath, ".//table[#id='small_calendar'][0]/td[#text='5']").click
#end
end
end
gets me this bug...
findElements execution failed;
Element does not exist in cache (Selenium::WebDriver::Error::StaleElement
ReferenceError)

If the elements are within a frame, you have to explicitly state that by using the within_frame method:
within_frame('fancybox-frame') do
within(:xpath, "//table[#id='small_calendar']") do
page.find('td', :text => '5').click
end
end

Got it!
The problem seems to be that within-frame and xpath do not get along. Once I removed it from the equation, I got the desired result.
within_frame('fancybox-frame') do
#within(:xpath, ".//table[#id='small_calendar'][0]") do #chokes
within(all("table[id='small_calendar']").first) do #works!
#page.find(:xpath, ".//table[#id='small_calendar'][0]/td[#text='5']").click #chokes
page.find("td[id='2013_0_6']").click #works!
end
end

Related

Implement Bootstrap Collapse with rails content_tag

I'm trying to implement BootStrap collapse with rails content_tag, see GIF below (I have BootStrap intergrated on the project)
Here's how mine currently looks like:
Edit:
I made some progress from Dave Kruz's answer, styling has taken effect, I just need help identifying what I'm missing to implement the collapse and expand.
Here's my code so far
c = content_tag(:button, "Show", class: "btn btn-primary", "data-toggle" => "collapse", "data-target" => "volunteerCollapse", type: "button")
v = volunteer_opportunity.users.map{ |user| user.volunteer_opportunities.map{ |vp| content_tag(:div, vp.title.to_s, class: 'collapse', id: "volunteerCollapse") }}.join(" ")
(c + v).html_safe
What am I missing that I should implement to have the collapse working well. Thanks!
Try calling html_safe on v.
(c + v.html_safe)
Rails already understands that c is safe html because it is generated by content_tag. However, v is only a string - rails doesn't trust that it is html. When you concatenate them in your example, you get html with an escaped string added on to it.
Better yet, use raw which is pretty much the same but will not throw an error if v is nil.
(c + raw(v))
And if security is a concern, you can go one better with sanitize.
(c + sanitize(v))
https://api.rubyonrails.org/classes/ActionView/Helpers/SanitizeHelper.html
EDIT: To fix the expand, collapse on the Bootstrap side of this, you need to correct a couple things. First, your button should reference the target with a query selector syntax: data-target="#collapsibleID". Second, the collapsible content should be wrapped in an element with that id. You should not have an id appear more than once.
Here's how I'd revise your code:
button = content_tag(:button, "Show", class: "btn btn-primary", "data-toggle" => "collapse", "data-target" => "#volunteerCollapse", type: "button")
collapse_content = volunteer_opportunity.users.map{ |user| user.volunteer_opportunities.map{ |vp| content_tag(:div, vp.title.to_s) }}.join
collapse_container = content_tag(:div, id: "volunteerCollapse", class: "collapse") do
sanitize(collapse_content)
end
(button + collapse_container)

If word.ActiveDocument exists close it [VBA]

Here's my code:
If Not (word.ActiveDocument Is Nothing) Then
word.ActiveDocument.Close SaveChanges:=wdSaveChanges
End If
I get an error if there is no active document. How can I prevent this?
You can check if there are open documents in the following way:
If word.Documents.Count Then '0 is falsy, all other values are truthy
word.ActiveDocument.Close SaveChanges:=wdSaveChanges
End If

vbscript namedItem() change select dropdown

In Excel VBA change option in the HTML select tag, I used the following code to change options within the <select> tag:
For Each objOption In objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).Options
If objOption.Value = SelQ Then
objOption.Selected = True
objIE.Document.GetElementsByTagName("table")(0).GetElementsByTagName("td")(tdNode).GetElementsByClassName("txt_input1")(0).OnChange
Else
objOption.Selected = False
End If
Next
This seems to work for web sites with nested <table> tags, but the web site was updated without the tags, so, to compensate for finding the selected option, I used this:
For Each objOption In objIE.Document.getElementById("frmProduction").elements.namedItem("WQ").Options
If objOption.Value = strVal Then
objOption.Selected = True
objIE.Document.getElementById("frmProduction").elements.namedItem("WQ").onchange
Exit For
Else
objOption.Selected = False
End If
Next
This is giving me the following error: Run-time error '5002': Application-defined or object-defined error
I used the above solution because it worked in another Internet Explorer application that used <frames> tags, so I modified it a little:
objIE.document.frames("DemographicsIFrame").document.GetElementByID("DropDownPayerID").value = PayerID
objIE.document.frames("DemographicsIFrame").document.GetElementByID("DropDownPayerID").onchange
I've tried to get around it with no success. I can get the selected option to change, but that's it. It won't update the page with required info related to the selected option. In the example above, that's what the onchange event was used for...to change the page contents after the PayerID was updated.
Any advice on how to make this work?
We were actually able to come up with a solution:
For Each objOption In objIE.document.getElementById("frmProduction").elements.namedItem("WQ").Options
If objOption.Value = strVal Then
objOption.Selected = True
Set evtFiroz = objIE.document.createEvent("HTMLEvents")
evtFiroz.initEvent "change", False, True
objIE.document.getElementById("WQ").dispatchEvent evtFiroz
Exit For
Else
objOption.Selected = False
End If
Next

Download HTML Text with Ruby

I am trying to create a histogram of the letters (a,b,c,etc..) on a specified web page. I plan to make the histogram itself using a hash. However, I am having a bit of a problem actually getting the HTML.
My current code:
#!/usr/local/bin/ruby
require 'net/http'
require 'open-uri'
# This will be the hash used to store the
# histogram.
histogram = Hash.new(0)
def open(url)
Net::HTTP.get(URI.parse(url))
end
page_content = open('_insert_webpage_here')
page_content.each do |i|
puts i
end
This does a good job of getting the HTML. However, it gets it all. For www.stackoverflow.com it gives me:
<body><h1>Object Moved</h1>This document may be found here</body>
Pretending that it was the right page, I don't want the html tags. I'm just trying to get Object Moved and This document may be found here.
Is there any reasonably easy way to do this?
When you require 'open-uri', you don't need to redefine open with Net::HTTP.
require 'open-uri'
page_content = open('http://www.stackoverflow.com').read
histogram = {}
page_content.each_char do |c|
histogram[c] ||= 0
histogram[c] += 1
end
Note: this does not strip out <tags> within the HTML document, so <html><body>x!</body></html> will have { '<' => 4, 'h' => 2, 't' => 2, ... } instead of { 'x' => 1, '!' => 1 }. To remove the tags, you can use something like Nokogiri (which you said was not available), or some sort of regular expression (such as the one in Dru's answer).
See the section "Following Redirection" on the Net::HTTP Documentation here
Stripping html tags without Nokogiri
puts page_content.gsub(/<\/?[^>]*>/, "")
http://codesnippets.joyent.com/posts/show/615

Alternatives of goto and jump commands

I am working on a language where there is not any goto,jump function. For example, Matlab.
Can you please help me on how to avoid using it? Is there any simple trick solving my problem?
You should consider using break and continue
Instead of:
for ...
...
if ...
goto nextstuff:
end
end
nextstuff:
You can do:
for ...
...
if ...
break
end
end
And as #andrey said, you can often replace goto by if-else
Instead of doing:
if cond
goto label
end
...
foobar()
...
label:
foobar2()
you can do:
if ~cond
...
foobar()
...
end
foobar2()
When you use a goto to go back, you can replace it by a while:
Instead of doing:
redothat:
foobar()
...
if cond
goto redothat;
end
you can do:
while cond
foobar()
...
end
Well, first of all you might as well ask it without the matlab tag, you might get better answers. That is because this kind of question is common to almost all of the modern languages.
Instead of goto and jump you should use either conditionals like if,if-else or loops like while,for, depending on what you want to achieve.
Check out GOTO still considered harmful?, Why is goto poor practise?.
As #Andrey mention you can use if or if-else statement. In many cases loops like while, for is one-to-one replacements to the if-else and goto.
You should also consider to use break and continue statement as #Oli said above.
In some rare cases you can use exception (I don't know whether the Matlab supports it) in order to "go back". This is somewhat controversial, but maybe in your case it will fit.
redothat:
foobar()
...
And inside foobar() in some place you have
if cond
goto redothat;
end
you can do:
while(true){
try {
foobar();
...
break;
}
catch(YourApplicationException e){
//do nothing, continiue looping
}
}
And inside foobar() in some place you have
if cond
throw YourApplicationException();
end
Or you can do something like this:
you can do:
boolean isOk = false;
while(! isOk){
try {
foobar();
...
isOk=true;
}
catch(YourApplicationException e){
//do nothing, continiue looping
}
}