How do I download a file in Watir? - watir

I'm starting to use Watir and I can't find anything about downloading a file (not an image, I've seen there's a specific method for that) and I don't care about configuring the download directory, nor where a button is clicked, just when I'm on a page, be it .html, .png, .rb how am I supposed to "Save as"? This really isn't made clear
I can right click on a link to a page but can't send_keys :down (it just moves the page down in the background of the right click context menu) as follows:
require "watir-webdriver"
browser = Watir::Browser.new :chrome
browser.goto "https://github.com/lmmx/watir-paper-scanner/blob/master/bookworm.rb"
browser.a(:text => "Raw").right_click
Edit: just put in the above code used to get an example of what I was actually trying to do as a little test, the last bit. .right_click works but .send_keys doesn't act on the context menu it produces, it moves the page instead, this could be substituted for some sort of .download function if there is one
What am I missing here...?

is the 'context menu' that is opening up actually a operating system level 'file finder' dialog? if so that is outside of the DOM and nothing based on webdriver is going to be able to touch it. at that point you are talking to the OS, not the browser. The browser has basically told the OS 'hey ask the user to pick a file and return the file path to me' and you the user are no longer interacting with the browser.
proof of this would be to right click on something in the dialog, if you do not see an 'examine element' option, you are very likely not in the browser.
at that point, look into something like RAutomation or AutoIt

Related

applescript display dialog with custom icon

Is there a way to use custom icons with applescript display dialog and notifications?
In the AppleScript documentation it says about the display dialog:
with icon (text | integer)
The resource name or ID of the icon to
display.
with icon (stop | note | caution) The type of icon to show.
You may specify one of the following constants:
stop (or 0): Shows a stop icon
note (or 1): Shows the application icon
caution (or 2): Shows a warning icon, badged with the application icon
with icon (alias | file) An alias or file specifier that specifies a .icns file.
So it seams like you can use your own icons, but I cannot get the following code to work.
display dialog "Text" with icon "/Users/user/Desktop/asd.icns"
It gets me the following error: "Resource not found."
The goal is to not even use a display dialog, but a display notification instead.
First of all you can't display a custom icon with display notification. The reason is that notifications are strongly related to a target application. As AppleScript scripts and applets aren't applications in terms of the Notification framework, the notification is related to the current application, the AppleScript Runner.
But you can display a custom icon with display dialog
The line
with icon (alias | file) An alias or file specifier that specifies a .icns file.
means what it says: The parameter must be an alias or file specifier rather than a POSIX or HFS string path.
Either
display dialog "Text" with icon alias ((path to desktop as text) & "asd.icns")
or
display dialog "Text" with icon file ((path to desktop as text) & "asd.icns")
path to desktop as text represents the HFS path to the desktop of the current user:
"Macintosh HD:Users:user:Desktop:"
Noted this question was three years ago, but I stumbled across this looking for a solution to a similar problem. Mine needs display alert rather than ... notification but the problem is the same because display alert doesn't have a custom icon option.
As noted in the other answer here, AppleScript has at least three interactive message type commands: display dialog, display alert, display notification, and probably others. It seems odd that only the first has the option to add a custom icon. I don't really understand why that is when it would be simple to make them consistent.
Needless to say, this question and #vadian's answer, inspired my solution - a "duh" moment for me once I realized it. It may or may not be a solution to this question. Posting it in case it is...
If the icon in question belongs to an app, you can tell that app to display the notification, regardless of whatever else your script is doing.
Your script can do whatever it needs to do to whatever other apps, or System Events, or itself (if your script is saved as its own application), or whatever else. In the middle of all of that you can have a single line that says:
tell application "MyApp" to display notification ...
The notification will have My App's icon, the result of the notification if any will be returned to the rest of the script and then your script will continue on inside whatever other tell statement or context its in.
If your icon isn't the icon for an app, then I believe there are ways to create an empty app with whatever icon you like, which can behave this way. Admittedly that's a bit of a kludge, but depending on how badly you want this, it's an option - though not one I'll expand on here.
My specific case in detail if interested (but doesn't particularly add to the solution above, just covers how I got there):
I'm writing a script that quits and re-opens another application after confirmation from the user. However, let's say I just wanted to provide a notification as per this question.
Options:
1. display dialog - has the option to provide a custom icon but lacks features of the other two options.
2. display alert - no custom icon but has other desired features, in my case the message parameter which adds extra smaller explanatory text below the primary text.
3. display notification - no custom icon but has other features as desired by this question's poster.
In my case I want alert because I want that extra message parameter (but this works for notification as well).
In my case, ideally the icon of the alert would be the icon of the app I'm restarting, but I can't tell the app itself to display the alert and then restart because the script loses connection with the app when it quits and it kills the running of the script.
If I tell System Events or the script itself to do all that then it can quit and reopen the app independently of the app, but the alert has the generic icon of itself or the System Events icon.
However, if I do what I described above - have my script do all its stuff, but have it tell the app in question to display the alert (and only that as described above), then the alert has the icon of the app in question, but the script still does its stuff independently of the app outside of that alert.
Solved my problem. May or may not solve this question.
#DavidT's answer is a perfect solution when your script is controlled by another application. However, things have changed a bit on Mac OS since.
Notably, if you run your script with tell application "MyApp" to display alert ... you will be promoting the user to give your app permissions to control itself, at least starting since Catalina. Not only this annoys the user with a new permission request, but it also looks kinda dumb since the dialogue is asking the user to allow "MyApp" to control "MyApp" and if the user denies it, your script will fail.
To avoid the permission request, just use tell me to display alert ... this will work just fine.
Another issue you might run into is that osascript may throw an exception if your script is launched as root. I found a nice workaround for that here.
This is a small example of how to launch the dialogue with the right user:
uid=determineUserIdFunction(...)
launchctl asuser $uid /usr/bin/osascript <<-EOS
tell me to display dialog "Now you see me" buttons {"OK"} default button 1 with title "WARNING!"
EOS
You have your path specification wrong. If you have a posix path to your icns file, use the POSIX file class coercion:
display dialog "Text" with icon POSIX file "/Users/user/Desktop/asd.icns"
This coerces the string path into a file reference the system understands, and works just fine.

Chrome extension, popup does not display correctly for dynamic popup DOM modification

I am building a chrome extension, a simple feature I want it to have is that when some clicks on the extension icon, it finds some info on the current page that my extension is visiting, and send the message from my content script to my popup, and my popup will add those info to it. now I pretty much finished all the work, there is only one problem left which is the layout of my popup does not display correctly. I have some thought about why it happens, I think it is because since the popup opens when you click on the icon, and then it finds the info on the page, and then it sends the message back to my popup, but the size of popup is already defined and displayed, so it does not rend it correctly, am I right? How should I fix it.
I think your content script should retrieve the info from the current page immediately (on load) and send this information to the background script.
The background script can store this information either in a variable, or in localStorage/Chrome storage. When opening the popup you can retrieve this information and show it immediately. This way you would avoid the racing condition.

Can a Chrome extension change its large (new tab page) icon?

My question is along the same lines as this: Change the Chrome extension icon
But I'm wondering instead about the large icon of the extension on a new tab page. Can it change itself based on data? All I have in mind is a simple countdown-calendar (as in, the whole point of the extension is to be a big ol' number on your new tab page), so the actual code wouldn't have to be very long, if icon-changing is possible. (It wouldn't even need to sync or connect to the Internet for any reason.)
My strong hunch is no, because I've never seen an extension do so, and I would expect that if it could, my Chrome's Gmail button would probably display the number of new messages or something. But I figured it didn't hurt to ask.
PS: I've never created a Chrome extension, I just had that idea for one just now. Anyone reading this can feel free to do it themselves, but otherwise I'll make it when I get the time, as a learning exercise.
Only apps (not extensions) can have an icon on the New Tab page (NTP).
This icon have to be declared via the icons property in the manifest file, and cannot dynamically be updated.
I can imagine two ways to get a dynamic icon on the options page:
Create an extension that replaces the New Tab page. Have a look at the docs for Override Pages.
Create an extension that uses the chrome.management API to enable/disable apps. This method might work for your personal setup, but it requires a new App for each icon. This feels a bit hacky, but hey, it might work.

Can I hide my extension's icon by default?

My Chrome extension doesn't need an icon - it's a one-liner extension that doesn't want to take up space on your extension bar.
How can I hide the icon by default?
Due to a change spearheaded by Google, all extensions now must have an icon in the toolbar area or the "overflow" in the menu.
Even if you don't have a browser_action, your extension will display an icon (or failing that, a tile with extension name's first letter). If you do not declare a browser_action, it will be greyed out and non-interactive, but will still be there.
The idea of this change is to provide visibility of otherwise potentially stealthy extensions ("Hey user, did you even know you have those installed?"). It's, let's put it, debatable whether it's the best approach, but that was Google's decision.
To suppress this behavior, add "converted_from_user_script": true to the extension's manifest.json. This works as of Chrome 61.
It used to be that visiting a link to a user script (ending in .user.js) caused Chrome to package up the user script as a content script extension with a generated manifest which included the converted_from_user_script key. Of course, this no longer works because all extensions (except in developer mode) now have to come from the Chrome Web Store.
Google thought it best to make the icon mandatory and I think so too, for reasons already pointed out by Xan.
Now of course you 're right, when you say that many extensions have no need for an icon, but the requirement of one still gives your extension an identity and Google still gives the user the opportunity to remove it from the toolbar. He simply has to right click the extension's icon and then choose Hide in Chrome menu.
I know, many users don't really bother to read all of the options under right click, or even right click it at all. But most of them still will make a single left click, to see if there is any option in the fancy new extension they added. You can take advantage of this by making a simple window where you point out their option of hiding the icon.
If you don't want an icon, you can omit the browser_action parameter from your manifest.json (the default_icon option is where you usually specify the icon). The browser_action section is what controls what you see in the toolbar.

showing content on the blank screen after downloading a file

I've observed that under certain circumstances, a web browser will navigate to a blank page and then prompt the user to download a file. In my current situation, it's navigating to a URL that generates an Excel file. The download of the file works perfectly, but the user is now stranded on a blank page. There are two things I would like to figure out:
What causes the blank page to be displayed? It doesn't happen all the time. Is it the difference between using GET and POST (I can't recall seeing a hyperlink do it, but forms usually do)? Is it something to do with the Content-Disposition? In my current case, I've set the Content-Disposition to be "inline" because I want it to display in the browser in IE. Firefox (and presumably others) will of course prompt to download because they can't display it inline. It is the situation where the user chooses to save it that the blank screen results.
If it is possible, I'd like to display some content on this blank screen to provide the user with a message like "your file has been generated, click here to go back to the main screen" or somesuch. Is there a way I can do that?
I'm using an IIS extension written in C++, so solutions for ASP, PHP, etc will not be helpful unless they're generally applicable (though I wouldn't mind learning about solutions in those languages!). Thanks.
I think you practically answered your own question: setting content-disposition to inline does exactly that. One solution that comes to mind is browser detection: use inline disposition if the browser is IE, attachment otherwise.
BTW, as a user, I prefer sites which offer me a choice whether I want to download the file or view it inside the browser (when, for example, accessing a PDF file). In this case, I would consider having a link/button for downloading the file, and adding a second link/button for IE browsers to view it.

Resources