Cucumber embed for screenshots not linking to screenshot - ruby

Cross-posted from the Cukes Google Group:
I have experimented with a number of methods of saving screenshots,
but settled on the method that is built into watir-webdriver. No
matter which method I have used, I am not able to successfully embed a
link to this image in the Cucumber HTML report.
In c:\ruby\cucumber\project_name\features\support\hooks.rb, I'm using:
After do |scenario|
if scenario.failed?
#browser.driver.save_screenshot("screenshot.png")
embed("screenshot.png", "image/png")
end
end
A link with text "Screenshot" IS added to the report, but the URL is
the project directory path ("c:\ruby\cucumber\project_name") rather
than a direct link to the file ("c:\ruby\cucumber\project_name\screenshot.png"). I have tried a number of different image formats
and direct paths using Dir.pwd with the same results each time.
What am I missing?
Thanks
Windows XP
Ruby 1.8.7
watir-webdriver (0.2.4)
cucumber (0.10.3)

Aslak:
Try this:
After do |scenario|
if scenario.failed?
encoded_img = #browser.driver.screenshot_as(:base64)
embed("data:image/png;base64,#{encoded_img}",'image/png')
end
end
Aslak
Adam:
Aslak was able to see the embedded
image in the file that I emailed him,
while I was still unable to do so in
IE 8. I tried it out in Firefox 3.6
and the image appears as expected.
The problem may have originally been
with the embedding method itself (or
rather, my use of it), but using
Aslak's base64 solution it only fails
to work in the Internet Explorer
browser.
Aslak:
I believe Base64-encoding of images in
HTML pages [1] works in all decent
browsers (sorry, IE is not one of
them). However, it should work in
IE:
http://dean.edwards.name/weblog/2005/06/base64-ie/
(but maybe they broke it in IE8, or
maybe it only works with gifs, or
maybe IE needs a special kind of
base64 encoding, or maybe you should
just ditch IE)
If being able to read cucumber html
reports with screenshots in IE is
really important to you, you could
always write each image to disk:
png = #browser.driver.screenshot_as(:png)
path = (0..16).to_a.map{|a| rand(16).to_s(16)}.join + '.png' # Or use some GUID library to make a unique filename - scenario names are not guaranteed to be unique.
File.open(path, 'wb') {|io| io.write(png)}
embed(path, 'image/png')
Obviously you have to make sure the
relative path you pass to embed is
right (depending on where you write
the html itself)
[1]
http://en.wikipedia.org/wiki/Data_URI_scheme
HTH, Aslak

Related

Can anyone suggest how to take screenshot of full webpage using ruby selenium?

I want to capture full webpage screenshot in chrome browser using ruby selenium. I am using Rspec testing framework. save_screenshot method captures screenshot only for visible area.
I have gone through the following link,
How to take a screenshot of a full browser page and its elements using selenium-webdriver/capybara in Ruby?
But I don't want to use window resizing or watir gem. Is there any other way or gem to achieve same.
1) You can use https://github.com/samnissen/watir-screenshot-stitch where
Directly employing geckodriver's new full page screenshot functionality (only on Firefox).
Screenshot stitching, paging down a given URL by the size of the viewport, capturing screenshots and adjoining them.
Employing a bundled html2canvas script against the page to generate a png from a canvas element.
2) or use native instrument for your OS - https://paulhammond.org/webkit2png
In code will look like this
webkit2png https://stackoverflow.com/questions/60728482/can-anyone-suggest-how-to-take-screenshot-of-full-webpage-using-ruby-selenium
where:
- main command - webkit2png
- link page - all else
It's old question, but it doesn't hurt to post this. Use Scrot to take screenshot, and later make gif. Since OP only want screenshots, command would be:
require 'screenscrot'
#screen = ScreenScrot.new
#screen.capture(:all)
On linux:
sudo apt install scrot -y && gem install screenscrot

How to download pdf file in ruby without .pdf in the link

I need to download a pdf from a website which does not provide a link ending with (.pdf) using ruby. Manually, when i click on the link to download the pdf, it takes me to a new page and the dialog box to save/open the file appears after some time.
Please help me in downloading the file.
The link
You an do this
require 'open-uri'
File.open('my_file_name.pdf', "wb") do |file|
file.write open('http://someurl.com/2013-1-2/somefile/download').read
end
I have been doing this for my projects and it works.
If you just need a simple ruby script to do it, I'd just run wget. Like this exec 'wget "http://path.to.the.file/and/some/params"'
At that point though, you might as well run wget.
The other way, is to just run a get on the page that you know the pdf is at
source = Net::HTTP.get("http://the.website.com", "/and/some/params")
There are a number of other http clients that you could use, but as long as you make a get request to the endpoint that the pdf is at, it should give you the raw data. Then you can just rename the file, and you'll have the pdf
In your case, I ran the following commands to get the pdf
wget http://www.lawcommission.gov.np/en/documents/prevailing-laws/constitution/func-download/129/chk,d8c4644b0f086a04d8d363cb86fb1647/no_html,1/
mv index.html thefile.pdf
Then open the pdf. Note that these are linux commands. If you want to get the file with a ruby script, you could use something like what I previously mentioned.
Update:
There is an added complication that was not initially stated, which is that the url to the pdf changes every time there is an update to the pdf. In order to make this work, you probably want to do something involving web scraping. I suggest nokogiri. This way you can look at the page where the download is and then perform a get request on the desired URL. Furthermore, the server that hosts the pdf is misconfigured, and breaks chrome within a few seconds of opening the page.
How to solve this problem: I went to the site, and refreshed it. Then broke the connection to the server (press the X where there would otherwise be a refresh button). Then right click next to the download link, and select inspect element. Then browse the dom to find something that is definitively identifying (like an id). Thankfully, I found something <strong id="telecharger"> Download</strong>. This means that you can use something like page.css('strong#telecharger')[0].parent['href'] This should give you a URL. Then you can perform a get request as described above. I don't have time to make the script for you (too much work to do), but this should be enough to solve the problem.

Ruby pdf testing in browser

Has anyone been able to find a way to test pdf's with ruby within the browser? I have tried a few different ways and the only way I have been able to get any pdf testing to work is to save off the pdf and use the pdf_reader gem. This only seems to work on pdf's that, when the link is clicked, opens up a dialog box with the options to open or save the pdf. Unfortunately I have not been able to find a way to do anything like this with pdf's that are opened in browser, with no dialog box options to save it. Any ideas?
Maybe testing it in the browser isnt the best way. When you say test the pdf what are you trying to do? I wouldnt test the pdf in the browser if I was you.
Try docsplit, if you want to verify its contents.
Docsplit is a command-line utility and Ruby library for splitting apart documents into their component parts: searchable UTF-8 plain text via OCR if necessary, page images or thumbnails in any format, PDFs, single pages, and document metadata (title, author, number of pages...)
You are not inventing a browser, or a PDF generator.
Use unit tests to check your back-end modules can take data in, and write PDF out, then serve the PDF in a website and let the browser do its thing. Test (as what Rails calls a "functional test") that the MVC will produce a web page containing a link to the PDF, and you are done.
You can use gem 'mechanize' to download an online PDF (the PDF with in a browser) on your computer and then read it via gem PDF reader.

Multiple swf files not loading in DNN 5.X

We have run numerous tests now and it has now come down to either a DNN-SWFObject loading multiple swf files on a page or Firefox bug.
Here's the outcome we need:
Two swf files on one page:
Header.swf: which holds the nav and some bling animation.
Map.swf: which has different provinces of the country highlighted on rollover. The active province is highlighted by reading the URL via Javascript and then loaded into the Map.swf via FlashVars.
In all of our other tests in other browsers, the scenario works very well but in Firefox 3.5.3 The swf files refuse to show.
We have stripped this test down to the bare minimum, one html page scenario and embedding it the same way using SWFObject 2.2 and this works in Firefox. When it is uploaded in DNN, the swf files refuse to show.
Is there anything anyone can think of?
Many thanks,
James
My first thought would be the way dnn renames element ids - and are you using client ids
If you could post some code it might help to look at it or a url to look at

Load an image from Firefox cache?

I'm trying to load an image from the Firefox cache as the title suggests. I'm running Ubuntu, so the location of my cache is /home/me/.mozilla/firefox/xxxxxx.default/Cache
However, in the Cache (and this is on Mac, too) the filenames are just ridiculous combinations of letters and numbers. Is there a way to pinpoint a certain file?
You should take a look at the source code of the CacheViewer Add-on.
Download the file instead of installing it (right click and save as) and then extract it (it's just a Zip file, even though it has a .xpi extension), then extract the cacheviewer.jar file inside the resulting chrome folder. Finally go into content and then cacheviewer to find the javascript and XUL files.
From my brief investigation, the useful routines are in the cacheviewer.js file, though if you were hoping there would be a simple javascript one liner for accessing cached items you're probably going to be disappointed. The XUL files (which are just XML) are helpful in working out which JS functions are called to perform particular tasks. I'm not too sure how all this maps into Greasemonkey, rather than the extension environment, but hopefully there's enough code to get you started.
Ummm, that really is an internal implementation detail. But I suggest looking at how about:cache?device=disk and about:cache-entry?client=HTTP&sb=1&key=https://stackoverflow.com/Content/img/wmd/blockquote.png are implemented.
Also, http://www.securityfocus.com/infocus/1832 gives details, too. Note that Firefox doesn't use a separate file for everything...
And of course, Firefox may change the format at any time.
Just give your img src= attribute the full URL. If the image happens to be cacheable (the server sends an appropriate Expires: or Cache-control: header, for example) and it's already in the cache, Firefox will not hit the network.
HTTP caching is supposed to be invisible. When you're generating content, you generally shouldn't worry about it.
You can point REDbot at a URL to see all sorts of delicious information about its cacheability.

Resources