Is there a way to access clipboard contents? - automated-tests

I am testing a page that has an embed modal with a textbox with an embed code and a "Copy" button that should copy the contents of the textbox onto the clipboard so a user can paste it elsewhere. Is there a way to test clicking the "Copy" button and verifying that the clipboard contents match the contents of the textbox? Thanks!

TestCafe cannot automate a browser's built-in behavior, including the Copy & Paste functionality. It is expected that this functionality works correctly as it is tested by browser developers.
You can try to check that your script/button executes the copy command in the following way:
const overwriteCopyCommand = ClientFunction(() => {
document.execCommand = command => window.lastExecutedCommand = command;
});
const getLastExecutedCommand = ClientFunction(() => window.lastExecutedCommand);
await overwriteCopyCommand();
await t
.click('.copy-url-button')
.expect(getLastExecutedCommand()).eql('copy');
Unfortunately, according to JavaScript restrictions, I don't see a way how to check the copied text.
See additional workarounds in these threads:
Support 'ctrl+c' and 'ctrl+v' key combinations for copying/pasting selected text
Allow to use HTML5 Clipboard API in tests

Related

Force download PDF instead of open it in a new tab using Cypress

Here are my steps:
Click a button
A report is generated and then downloaded.
I am using Cypress. After clicking the button, the file opens in a new browser tab, and I can't validate the content. Is there any way to force download the file instead of displaying the content in a new browser tab in Cypress?
Removing the target attribute will prevent the browser from opening a separate new tab. maybe this can continue the download.
cy
.get('a')
.invoke('removeAttr', 'target')
Also doing a cypress request may help.
const element = doc.querySelector('[data-cy=orange-vcard]').getAttribute('href');
cy.request({
url: orangeVcardUrl,
encoding: 'base64'
})
.then((response) => {
expect(response.status).to.equal(200);
// check for the response body here
});
Ref: https://www.leonelngande.com/testing-file-download-links-with-cypress/

It is possible to attach files by clicking on capybara

I'm using capybara, cucumber and webDriver to perform file tests
I am doing a study and need to attach a file, however the file input does not exist in the dom, and it is only created when the button is clicked, currently my code is like this:
HTML
<a id="input-id" href="#" onClick="callInput">Attach</a>
Script
function callInput(){
const input = document.createElement("input");
input.style.display = "none";
input.type = "file";
input.click();
}
If it's possible the only way would be using the attach_file block mode
attach_file('/file/to/be/attached') do
click_link('Attach')
end
Since you're setting display to 'none' rather than hiding via setting it offscreen or other methods it's possible the make_visible option could make it work if the above doesn't
attach_file('/file/to/be/attached', make_visible: true) do
click_link('Attach')
end
If neither of those work then it's not possible for Capybara to work with the way you've implemented file upload on your page.
Note: odds of either working is pretty low because you're never actually attaching the input to the page so events won't get routed anywhere Capybara can detect.

How to update UI based on FileUpload control change

I have an FileUpload control on a page. I need to change some values based on the filename once a user selects a file. I'm trying to find out the best way to do this. The only option I can see is listening in JavaScript for a change event and then either..
a) forcing a post back and updating the form
b) updating things on the client side using JavaScript and some back end async calls.
Is there any other options and if not which of this is preferable?
Thanks
If you are using jquery, you can attach a function to the change of the file upload.
Consider the following example html:
<input id="myFile" type="file">
<p><label id="myLabel">No File</label></p>
And let's say we wanted to update the label with the name of the selected file. To do that, we'd use the following javascript:
$(document).ready(function () {
$("#myFile").change(function () {
$("#myLabel").html($(this).val());
});
});
Here's a fiddle in action: http://jsfiddle.net/ffkuL/1/
If you aren't using jquery, you can do something like this:
var upload = document.getElementById("myFile");
upload.onchange = function (e) {
var label = document.getElementById("myLabel");
label.innerHTML = this.value;
};
And here's a fiddle for that one: http://jsfiddle.net/8PYwK/
(Honestly, though, I find that it's far simpler in the long run to use jquery in the long run when dealing with ASP.NET controls.)
Obviously, the label changing in my samples are just examples. Following that pattern, though, you can make whatever changes you need to on the client side (rather than needing to post back).

How to use Google Apps Script into my Website on button click?

I have an Google apps script and I want to call this Google apps script on my web page?How can I achieve this?This is my Google apps Script code for creating a google doc on my drive.
function createAndSendDocument() {
var doc = DocumentApp.create('Hello World');
doc.appendParagraph('This document was created by my first Google Apps Script.');
doc.saveAndClose();
var url = doc.getUrl();
var emailAddress = Session.getActiveUser().getEmail();
GmailApp.sendEmail(emailAddress,
'Hello '+ url);
}
and I want to call this function when I click on a button on my web page.But I don't know how to call this function on my button click.And one more thing I also save this script and change this version number and deploy as a web app and get the URL of this Google apps Script.
UPDATE: (added incorrectly in an answer)
Actually i am using this line like this.
btnClick.addEventListener("click", function () {
Page.ClientScript.RegisterStartupScript(this.GetType(), "GoogleFunction", "createAndSendDocument()", true);
}
So please now tell me what's wrong in that?When i run my application then it's show me an error like "The Page is not defined".
Look also at this documentation and also this one, everything is clearly explained .
you can add the div and the code in your page as you needed. also you need to add the javascript codes inside your head if it exists.
hi you can use this code on button click after adding a script manager in the form
Page.ClientScript.RegisterStartupScript(this.GetType(),"GoogleFunction","createAndSendDocument()",true);

Precompiled views: can I still read the contents at runtime?

I have my views setup to pre-compile, and therefore, at runtime if I were to try and read the view file (e.g. "~\Views\User\Report.cshtml") I'd get the following dummy-text, as opposed to the contents of my view:
This is a marker file generated by the precompilation tool, and should not be deleted!
Problem is, I'd like to re-use the cshtml view, and rerender it another way at runtime, but I cannot due to the above restriction.
The scenario:
An admin can see a list of users in a /User/Report route. It outputs some HTML that has a list of all users, and their information in an HTML table. These admins frequently want to download this html file (styles and all) to email it as an attachment to someone else. They could, of course, go to File->Save in their browser, but I wanted to simplify that action by adding a link to the page "Download this report as HTML" that would simply return the same page's content, as a forced-downloaded HTML file (2012-07-11_UserReport.html).
So, what I tried to do was re-render the view by running the Report.cshtml file's contents through ASP.NET's File() method, like this:
var html = System.IO.File.ReadAllText(Server.MapPath(#"~\Views\User\Report.cshtml"));
var bytes = System.Text.Encoding.UTF8.GetBytes(html);
return File(bytes,"text/html",string.Format("{0}_UserReport.html",DateTime.Now.ToString("yyyy-MM-dd")));
But, like I mentioned earlier, the file comes back as the dummy-text, not the view, since I'm pre-compiling the views.
I understand that to get around the pre-compilition, I could simply copy the Report.cshtml file, and rename it to Report.uncompiled (adding it to the csproj as of course) and read the contents of it, that's an ok solution, but not good enough.
What I would really like to know is: Is there a way I can get at that pre-compiled content? I looked in the Assembly's embedded resources, and they are not there. Any ideas/suggestions?
Updated with current solution
So after searching around some more, and trying to use WebClient/WebRequest to just make a request to the route's URL and send the response back down to the user to download while at the same time trying to pass the user's .ASPXAUTH cookie (that made WebClient/WebRequest time out for some reason? I even tried to create a new ticket, same result) I ended up going with what I didn't want to do: duplicate the view file, and rename it so it's not precompiled.
The view file (Report.uncompiled) had to be modified a bit as it was, and then I ran it through RazorEngine's Razor.Parse method and got what I needed, but it just felt hackey. Would still like a way to access the view file (Report.cshtml) even after it's compiled.
var templateHtml = Razor.Parse(System.IO.File.ReadAllText(Server.MapPath(#"~\Views\User\Report.uncompiled")),model);
var bytes = System.Text.Encoding.UTF8.GetBytes(templateHtml);
return File(bytes, "text/html", string.Format("{0}_UserReport.html", DateTime.Now.ToString("yyyy-MM-dd")));
Would the WebClient class work?
using System.Net;
using (WebClient client = new WebClient ())
{
client.DownloadFile("http://yourwebsite.com/test.html", #"C:\directory.html");
// If you just want access to the html, see below
string html = client.DownloadString("http://yourwebsite.com/test.html");
}
Just have this fire whenever your user clicks a button and then it will save the current content of the page wherever? You could probably also have a directory selector and feed whatever they select into that second parameter.
It essentially does the same thing as the browser save as, if that's what you want.

Resources