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

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/

Related

how to open new tab in inertiaJs and vue 3

I want to open a file on a new tab but my inertia props are lost,
my approach on opening new tab is this
window.location.href = "/view/"+props.document.type+"/"+props.document.id, "_blank"
but the my inertia props are lost. It only returns a blank html page
Update: there was a missing return in OP's code.
If you open a new tab, you will have your SPA nuked. The idea is to persist it with something like cookies or alike. Give a try to that answer for a possible solution: https://stackoverflow.com/a/66872372/8816585
VueJS by default has no way to persist any state out of the box (it's client side code).

How can i open a spreadsheet in a new tab with a button?

I want to create a button in a googlesheet/google Apps Script that opens an existing google sheet in a new tab when u click on it. I will insert the button in a google sheet, so i basically just need a code that i link to the button.
The code below does not work somehow?
Can you help me?
function openURL(){
var button = CardService.newTextButton()
.setText("This button opens a link in an overlay window")
.setOpenLink(CardService.newOpenLink()
.setUrl("google sheet")
.setOpenAs(CardService.OpenAs.OVERLAY)
.setOnClose(CardService.OnClose.RELOAD_ADD_ON));
};
In order to create such button and functionality, you will have to:
Create the code that will be executed upon clicking it. Go to Tools>Script Editor. Paste the following code (replacing, of course, the url for the appropriate one):
function openMySpreadsheet() {
var url = 'YOUR_SHEET_URL';
var htmlTemplate = HtmlService.createTemplateFromFile('js');
htmlTemplate.url = url;
SpreadsheetApp.getUi().showModalDialog(htmlTemplate.evaluate().setHeight(10).setWidth(100), 'Opening Sheet...');
}
Still from the Google Apps Script IDE, go to File>New>HTML File. Set the file name to be js and paste the following code into it:
<script>
window.open('<?=url?>', '_blank', 'width=800, height=600');
google.script.host.close();
</script>
Go back to your Sheets document and insert an image using Insert>Image>Image over cells and select any image you would like to have as your button.
Select the newly created image and click on the 3 dots that appear on the top-right corner of it. Click on "Assign script", and put the function name to be run (in this case, openMySpreadsheet would work).
From now on, each time you click on the button, a short-lived dialog will be shown up (that is necessary in order to open a link on a new tab) and the link will be automatically open.

Is there a way to access clipboard contents?

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

Open or save the excel file attachment from the browser using ajax request in Ext.js

I am generating the excel file in server side and writing it to the
outpuststream with the following response headers.
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition","attachment;filename="+fileName);
In my client side : An ajax call to the servlet as shown :
Ext.Ajax.request({
url: 'GenerateReport',
method: 'GET',
params: {
'start_date': sd.getValue(),
'end_date':ed.getValue()
}
});
Browser shows following Response Headers :
My Issue : The Open and Save dialog prompt is not opening.What may be the problem.Please help me resolve this.Any help is appreciated.Thanks.
Try to open new window with appropriate URL instead of AJAX request.
Thanks #liya. I have resolved it by using iframe instead of ajax call.Its getting
downloaded directly.Here it is :
Ext.core.DomHelper.append(document.body, {
tag : 'iframe',
id : 'downloadIframe',
frameBorder : 0,
width : 0,
height : 0,
css : 'display:none;visibility:hidden;height:0px;',
src : 'GenerateReport'
});
But Is their anyway,to open a dialog box before downloading,to prompt
for 'OPEN' or 'SAVE' the file instead of saving it directly which I am
doing.
To be sure the browser would prompt Open or Save, you have to send binary content type. Otherwise the browser tries to open the downloaded file with an associated application. Some browsers (older IE i.e.) might still infer the associated application from the filename extension.
response.setContentType("application/octet-stream");

How to extract the file from the filepicker?

I just use Filepicker to upload the picture. After I uploaded the picture I can get the url in the console.
Template.uploadform.events
'change #upload_widget': (evt) ->
if console?
console.log JSON.stringify(evt.fpfile)
Now I need to show that images to my web application by those url. I am just a new bie on meteor. I am using jade-handlebars and coffeescript in my application.
Pleas help me out how can I show the images by using that url in my application ???
Thank You in advance.!!!
Set a Session variable you can use to hold the url:
Session.setDefault('uploadedImageUrl', placeholderImageUrl)
Once the image has been uploaded and you have it, set the session variable to the new url: Session.set('uploadedImageUrl', fpImageUrl)
Make a template helper to reference the session variable in your template: Template.uploadform.uploadedImageUrl = -> Session.get('uploadedImageUrl')
In your template, show the image like so: img(src="{{uploadedImageUrl}}")
Once the image has been uploaded, your placeholder image will be replaced with the new uploaded image.

Resources