How to take full page screenshots with Watir and geckodriver + Firefox? - ruby

I upgraded my Watir / Firefox automation stack to the latest version, and added geckodriver with it. I was surprised to see that now screenshots are of the viewport only by default.
require 'watir'
require 'mini_magick'
b = Watir::Browser.new :firefox
b.goto "https://daringfireball.net"
base = b.screenshot.base64
blob = Base64.decode64(base)
image = MiniMagick::Image.read(blob)
image.height
=> 1760 # macOS 'Retina' resolution doubling
b.execute_script "return window.innerHeight"
=> 880 # 880 * 2 = 1760
b.execute_script "return document.documentElement.scrollHeight"
=> 34692
geckodriver does not have any API for full page screenshots, though reintroducing this feature is planned (on an infinite timescale).
How can I take screenshots of the full page with Watir driving Firefox without rolling back my environment?

Using Watir's .execute_script, it is possible to repeatedly take screenshots of the viewport while moving the scroll position. It is then possible to stitch images together using MiniMagick.
I developed the watir-screenshot-stitch gem to encapsulate my best approach to solving this problem, though it comes with caveats, which you can read about there. It is also memory intensive and can be slow.
This is not a true full-page screenshot solution, and I would gladly accept any alternative approaches that improve on this.

I solved the problem in C#. But the solution I guess can be rewritten on any language. I used a JavaScript library called HTML2Canvas to generate the full page screenshots. Here is the C# code:
[Test]
public void TakingHTML2CanvasFullPageScreenshot()
{
using (var driver = new ChromeDriver())
{
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(5);
driver.Navigate().GoToUrl(#"https://automatetheplanet.com");
IJavaScriptExecutor js = driver;
var html2canvasJs = File.ReadAllText($"{GetAssemblyDirectory()}html2canvas.js");
js.ExecuteScript(html2canvasJs);
string generateScreenshotJS = #"function genScreenshot () {
var canvasImgContentDecoded;
html2canvas(document.body, {
onrendered: function (canvas) {
window.canvasImgContentDecoded = canvas.toDataURL(""image/png"");
}});
}
genScreenshot();";
js.ExecuteScript(generateScreenshotJS);
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.IgnoreExceptionTypes(typeof(InvalidOperationException));
wait.Until(
wd =>
{
string response = (string)js.ExecuteScript
("return (typeof canvasImgContentDecoded === 'undefined' || canvasImgContentDecoded === null)");
if (string.IsNullOrEmpty(response))
{
return false;
}
return bool.Parse(response);
});
wait.Until(wd => !string.IsNullOrEmpty((string)js.ExecuteScript("return canvasImgContentDecoded;")));
var pngContent = (string)js.ExecuteScript("return canvasImgContentDecoded;");
pngContent = pngContent.Replace("data:image/png;base64,", string.Empty);
byte[] data = Convert.FromBase64String(pngContent);
var tempFilePath = Path.GetTempFileName().Replace(".tmp", ".png");
Image image;
using (var ms = new MemoryStream(data))
{
image = Image.FromStream(ms);
}
image.Save(tempFilePath, ImageFormat.Png);
}
}
You can find more examples and explanations in the article.

It is now possible to do this in Firefox, employing a geckodriver feature. As far as I know, this feature is not baked into Selenium / probably not a part of the W3C spec.
require 'watir'
browser = Watir::Browser.new :firefox
bridge = browser.driver.session_storage.instance_variable_get(:#bridge)
server_uri = bridge.instance_variable_get(:#http).instance_variable_get(:#server_url)
sid = bridge.instance_variable_get(:#session_id)
driver_path = "session/#{sid}/moz/screenshot/full"
request_url = server_uri.to_s + driver_path
url = URI.parse(request_url)
req = Net::HTTP::Get.new(request_url)
raw = Net::HTTP.start(url.host, url.port) {|http| http.request(req) }.body
base64_screenshot = JSON.parse(raw, symbolize_names: true)[:value]
This approach is also now an option in the watir-screenshot-stitch gem:
require 'watir-screenshot-stitch'
b = Watir::Browser.new :firefox
b.goto "https://github.com/mozilla/geckodriver/issues/570"
base64_screenshot = b.base64_geckodriver

Related

Swift : Share text through Twitter

So basically I am making an event app. Everything has been going smoothly but there's just sharing the event to twitter.
I have searched the internet but all I am getting is using the native app of twitter which I don't want. I want to use the browser to tweet.
I have implemented this method for FB sharing.
Any idea would help me a lot.
let content = FBSDKShareLinkContent()
content.contentURL=NSURL(string: "http://facebook.com")
content.imageURL = NSURL(string: "http://facebook.com")
content.contentTitle = "Shou 3emlin test app "
content.contentDescription = "testing testing testing"
let shareDialog = FBSDKShareDialog()
shareDialog.fromViewController = self
shareDialog.mode=FBSDKShareDialogMode.Browser
shareDialog.shareContent = content
if !shareDialog.canShow() {
shareDialog.mode=FBSDKShareDialogMode.Native
shareDialog.shareContent = content
}
if shareDialog.canShow() {
shareDialog.show()
}
Put this in an action method of a button or in the method where you want to use the browser to tweet your text Swift 3.0:
let tweetText = "your text"
let tweetUrl = "http://stackoverflow.com/"
let shareString = "https://twitter.com/intent/tweet?text=\(tweetText)&url=\(tweetUrl)"
// encode a space to %20 for example
let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
// cast to an url
let url = URL(string: escapedShareString)
// open in safari
UIApplication.shared.openURL(url!)
Result:
Take a look at Fabric.io. This SDK allows you to compose tweets directly from your app.
let composer = TWTRComposer()
composer.setText("just setting up my Fabric")
composer.setImage(UIImage(named: "fabric"))
// Called from a UIViewController
composer.showFromViewController(self) { result in
if (result == TWTRComposerResult.Cancelled) {
print("Tweet composition cancelled")
}
else {
print("Sending tweet!")
}
}
let tweetText = "hy"
let tweetUrl = "http://rimmi/"
let shareString = "https://twitter.com/intent/tweet?text=\(tweetText)&url=\(tweetUrl)"
// encode a space to %20 for example
let escapedShareString = shareString.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
// cast to an url
let url = URL(string: escapedShareString)
// open in safari
UIApplication.shared.openURL(url!)
#ronatory's solution worked like charm. It also opens a Twitter application if it's already installed on the user's device.
For swift 5+ use UIApplication.shared.open(url!) instead of UIApplication.shared.openURL(url!) as it's deprecated.

Disable Chrome 'Multiple Automatic Download' Prompt with Ruby Watir ChromeDriver-DesiredCapabilites [duplicate]

I developed a crawler with ruby watir-webdriver that downloads some files from a page. My problem is that when I click to download the second file, Chrome opens a bar in the top asking for confirmation that I am downloading multiple files from this website.
Once this is used by webdriver, I cannot confirm the download. Is there anyway to avoid this confirmation? I am thinking if is there any configuration to avoid it or if is there an extension to do this or even if I can click on the confirmation with webdriver.
thanks
I'm using Chrome 49 and none of the other solutions worked for me.
After some research I found a working solution:
ChromeDriver createChromeDriverWithDownloadFolder(String folder) {
Map<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", folder);
chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(cap);
}
It seems as if these settings are constantly changing. Therefore, here's how I found the right solution for my setup:
Open Chrome and go to chrome://version/ to find the path of your profile
In Default/Preferences is a json file called Preferences. Open it and search for automatic_downloads.
In my case the interesting part of the file looked like this:
..."profile": {
"avatar_bubble_tutorial_shown": 1,
"avatar_index": 0,
"content_settings": {
"clear_on_exit_migrated": true,
"exceptions": {
"app_banner": {},
"auto_select_certificate": {},
"automatic_downloads": {
"[.]localhost:63342,": {
"setting": 1
},...
From that I could derive that the right setting would be chromePrefs.put("profile.content_settings.exceptions.automatic_downloads.*.setting", 1 );
As of Chrome 56.0.2924.87, February 17, 2017, the only preference you need to set (however you set them for your webdriver) is:
'profile.default_content_setting_values.automatic_downloads': 1
Giving an updated answer because most answers here use outdated preferences or show other preferences that are unnecessary.
for new chrome (version 46 or newer) this options was changed
now your hash must looks like this:
prefs = {
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1}, #for chrome version olde ~42
'default_content_setting_values' => {'automatic_downloads' => 1}, #for chrome newer 46
}
}
browser = Watir::Browser.new :chrome, options: {prefs: prefs, args: ['--test-type', '--disable-infobars'}
Here is the solution for Java - Selenium implementation
We faced hard time fixing this, as we wanted to add automation test for functionality which downloads set of PDFs on a single download link.
Map<String, Object> prefs = new HashMap<String, Object>();
//To Turns off multiple download warning
prefs.put("profile.default_content_settings.popups", 0);
prefs.put( "profile.content_settings.pattern_pairs.*.multiple-automatic-downloads", 1 );
//Turns off download prompt
prefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOptions("prefs", prefs);
driver = new ChromeDriver(options);
Hope this help to someone.
It seems that the solution is different for older and newer chromedriver versions and that is adding to the confusion.
chromedriver
profile = Selenium::WebDriver::Chrome::Profile.new
profile['download.prompt_for_download'] = false
profile['download.default_directory'] = download_directory
b = Watir::Browser.new :chrome, :profile => profile
chromedriver2
prefs = {
'profile' => {
'default_content_settings' => {'multiple-automatic-downloads' => 1},
}
}
b = Watir::Browser.new :chrome, :prefs => prefs
Today most people are probably using chromedriver2 version and that is a solution that should work fine. It worked ok in my watir scripts as I am not getting the message: "This site is attempting to download multiple files. Do you want to allow this?" anymore.
Java solution:
cap = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
Map<String, Object> content_setting = new HashMap <>();
content_setting.put("multiple-automatic-downloads",1);
prefs.put("download.prompt_for_download", "false");
prefs.put("profile.default_content_settings", content_setting);
options.setExperimentalOption("prefs", prefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
this is what worked for me:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("profile.default_content_setting_values.automatic_downloads", 1);
chromePrefs.put("download.prompt_for_download", false);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
This bug/enhancement has been raised in the chromedriver page at the below URL:
http://code.google.com/p/chromedriver/issues/detail?id=130
Bug/Enhancement Status: Yet to be resolved.
I have tried to do it on page load client-side using markups.
<META HTTP-EQUIV="Content-Disposition" CONTENT="inline" />
It seems to work (it is working at this moment, in overriding).
But time will tell (might not have effect on future CHROME's, you know what I mean).
There are a list of available header fields published on a couple of sites which I find extremely helpful. Hope it will help you, as well.
https://www.w3.org/Protocols/HTTP/Issues/content-disposition.txt
https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-2

playing vimeo in IE8 with popcorn.js

I can't get popcorn.js to play ball with IE8. I'm using the popcorn-ie8 wrapper, the media proto and vimeo plugin ONLY.
My code is as simple as this:
var video = Popcorn.HTMLVimeoVideoElement('#video');
video.src = 'http://vimeo.com/18359846';
var example = Popcorn(video);
example.play()
IE8 throws an error:
'MediaError is undefined', popcorn._MediaElementProto.js (58).
If I use a full package distro of popcorn.js and put the IE8 shim at the top, my IE leaks memory rapidly and crashes. I also had to disable memory protection in IE8 in order to capture the error, since otherwise the whole browser crashes.
So I've cut it down to just popcorn+ie8shim, the mediaelement core and the vimeo plugin.
I have set up a jsFiddle here: http://jsfiddle.net/EtepN/
You have to use this url to see the result in IE8, since jsFiddle doesn't work in IE8 : http://jsfiddle.net/EtepN/embedded/result/
-- edit --
getting closer; I've created a closure for MediaError so that IE8 doesn't throw an error on it being undefined - just define it as another object and set that to the MediaError object.
(function(w) {
var _MediaError = w.MediaError;
if(!_MediaError) {
w.MediaError = _MediaError = (function() {
function MediaError(code, msg) {
this.code = code || null;
this.message = msg || "";
}
MediaError.MEDIA_ERR_NONE_ACTIVE = 0;
MediaError.MEDIA_ERR_ABORTED = 1;
MediaError.MEDIA_ERR_NETWORK = 2;
MediaError.MEDIA_ERR_DECODE = 3;
MediaError.MEDIA_ERR_NONE_SUPPORTED = 4;
return MediaError;
}());
}
}(window));
Still not getting it to work in IE8 but that's one error down at least ....

Dynamically added Raddocks Optimization?

We have a optimization problem regarding rad dock controls. The requirement of project is such that we are creating dynamic raddocks on the fly and adding it to a raddockzone, then we are saving the raddock "type" etc in a mssql db. We also have a collector window/raddockzone in which we have build a functionality where we can drag a dock and save it in collector. As with the first raddockzone we are adding the dock in collector on the fly. Now while adding a dock or moving it to another raddockzones it takes sometime. Our client is comparing it with the example of the demo link : http://demos.telerik.com/aspnet-ajax/dock/examples/content/defaultcs.aspx
Following is our code snippet to add dock on the fly:
private RadDockNew CreateRadDock()
{
//string[] allowedZones = { "RDZCollector", "RadDockZone2" };
int width = Convert.ToInt32((hdnWidth.Value == "") ? "520" : hdnWidth.Value);
RadDockNew dock = new RadDockNew();
dock.DockMode = DockMode.Docked;
dock.UniqueName = Guid.NewGuid().ToString().Replace("-", "a");
dock.ID = string.Format("RadDock{0}", dock.UniqueName);
//dock.Title = dock.UniqueName.Substring(dock.UniqueName.Length - 3);
dock.Width = Unit.Pixel(width);
dock.CssClass = "RadDockZoneMain";
//dock.AllowedZones = allowedZones;
dock.Style.Add("min-height", "290px");
dock.OnClientDockPositionChanged = "DropInCollector";
//dock.EnableViewState = false;
DockCommand cmd = new DockCommand();
cmd.Name = "Setting";
cmd.Text = "Setting";
cmd.OnClientCommand = "showSettings";
dock.Commands.Add(cmd);
DockCommand dc = new DockCommand();
dc.Text = "Trash";
dc.Name = "Trash";
dc.OnClientCommand = "CloseDock";
dc.CssClass = "rdClose";
dc.AutoPostBack = true;
dock.Commands.Add(dc);
DockToggleCommand cmd2 = new DockToggleCommand();
cmd2.CssClass = "rdCollapse";
cmd2.AlternateCssClass = "rdexpand";
cmd2.OnClientCommand = "ChangeImage";
//DockCommand collapse = new DockCommand();
//collapse.Text = "Collapse/Expand";
//collapse.Name = "Collapse/Expand";
//collapse.OnClientCommand = "CollapseDock";
//collapse.CssClass = "rdCollapse";
dock.Commands.Add(cmd2);
return dock;
}
Please tell if there is any way to optimize / make it faster.
Thanks.
I examined the attached code sample and I think that it is correct. My suggestions are to check if the problem persists if you use other storage medium, instead of a database or if there are client script errors on the page, containing the RadDocks.
As the setup of your project seems to be similar to the one, implemented in the My Portal demo, I would recommend using the example in the Code Library article Saving State of Dynamically Created RadDocks in DataBase using Hidden UpdatePanel as a reference.

Creating a single page proxy using Ruby Sinatra

I am trying to use Ruby Sinatra to create a simple proxy for a specific web page. I can do it in C#, I just can't seem to work it out for Sinatra, the C# code is below:
<%# WebHandler Language="C#" Class="Map" %>
using System;
using System.Web;
using System.Net;
using System.IO;
public class Map : IHttpHandler {
static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[0x1000];
int read;
while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
output.Write(buffer, 0, read);
}
public void ProcessRequest(HttpContext context)
{
string gmapUri = string.Format("http://maps.google.com/maps/api/staticmap{0}", context.Request.Url.Query);
WebRequest request = WebRequest.Create(gmapUri);
using (WebResponse response = request.GetResponse())
{
context.Response.ContentType = response.ContentType;
Stream responseStream = response.GetResponseStream();
CopyStream(responseStream, context.Response.OutputStream);
}
}
public bool IsReusable {
get {
return false;
}
}
}
The Ruby Sinatra code I have tried is as follows:
require 'rubygems'
require 'sinatra'
get '/mapsproxy/staticmap' do
request.path_info = 'http://maps.google.com/maps/api/staticmap'
pass
end
I am assuming that the Sinatra one does not work (get a 404) as is is only passing the request to pages in the same domain. Any hep would be greatly appreciated.
EDIT:
With the Tin Man's help I've come up with a nice succinct solution, which works well for me:
get '/proxy/path' do
URI.parse(<URI> + request.query_string.gsub("|", "%7C")).read
end
Thanks for all the help.
If you want your Sinatra app to retrieve the URL, you'll need to fire up a HTTP client of some sort:
get '/mapsproxy/staticmap' do
require 'open-uri'
open('http://maps.google.com/maps/api/staticmap').read
end
I think this will work and is about as minimal as you can get.
You could use HTTPClient if you need more tweakability.
Also, I think that Rack can do it. Sinatra is built on top of Rack, but it's been a while since I played at that level.
I still need to find a way to extract the contentType from the response
From the Open-URI docs:
The opened file has several methods for meta information as follows since
it is extended by OpenURI::Meta.
open("http://www.ruby-lang.org/en") {|f|
f.each_line {|line| p line}
p f.base_uri # <URI::HTTP:0x40e6ef2 URL:http://www.ruby-lang.org/en/>
p f.content_type # "text/html"
p f.charset # "iso-8859-1"
p f.content_encoding # []
p f.last_modified # Thu Dec 05 02:45:02 UTC 2002
}
For your purposes something like this should work:
content_type = ''
body = open("http://www.ruby-lang.org/en") {|f|
content_type = f.content_type # "text/html"
f.read
}
I haven't tested that, but I think the return value of the block will be assigned to body. If that doesn't work then try:
content_type = ''
body = ''
open("http://www.ruby-lang.org/en") {|f|
content_type = f.content_type # "text/html"
body = f.read
}
but I think the first will work.
With the help of the Tin Man and TK-421 I've worked out a solution, see the Sinatra route below:
get '/proxy/path' do
require 'open-uri'
uri = URI.parse(<URI>)
getresult = uri.read
halt 200, {'Content-Type' => getresult.content_type}, getresult
end
Just replace the <URI> with the page you require, and you're good to go.
After some more playing this is what I've come up with:
get '/proxy/path' do
URI.parse(<URI> + request.query_string.gsub("|", "%7C")).read
end
As mentioned else where you need to require 'open-uri' at the top of the code. The reason for the gsub is that for some reason the parse fails if they are left in, and my browser doesn't encode them automatically.

Resources