How can I test for a file download in Laravel Dusk? - laravel-dusk

In Laravel Dusk, I want to click a button on a web page that runs Javascript that triggers a download of a PDF file. How can I run a "click" on a button and then assert that a file is getting downloaded?

Please check this link: https://laracasts.com/discuss/channels/testing/testing-file-download-with-dusk
Summary:
$response = $this->actingAs($user)->get(route('export.report'));
$response->assertStatus(200);
$response->assertHeader('content-disposition', 'attachment; filename="report.csv"');
$file = $response->getFile();
$filename = $file->getPathname();

Related

How to test url of new tab after clicking a link in laravel dusk

I'm testing my website using Laravel Dusk and I want to test a link that open a new tab to check if it goes to the correct URL. My test looks like this;
$this->browse(function (Browser $browser) {
$browser->loginAs(User::find(1))
->visit('/test')
->waitForLocation('/test')
->click('#test-link');
$window = collect($browser->driver->getWindowHandles())->last();
$browser->driver->switchTo()->window($window);
$url = $browser->driver->getCurrentURL();
Assert::assertTrue($url == 'http://foobar.com');
});
The problem here is that the URL that I'm getting is just about:blank. At first, I thought it's because the new tab is still loading but I tried to add a pause(5000) before getting current URL but it still returns about:blank. Why does the new tab doesn't proceed with the URL of the link?

Feature files called on after scenario hooks would not print to html report

I am following the demo for after scenario hooks that supposedly enable us to print to html file by calling a feature file (https://github.com/intuit/karate/commit/4e4583390f28891665033607ace88272f4ab6aff)
Below is my config.
karate-config.js
karate.configure('afterScenario', read('classpath:hooks.js'));
hooks.js
function(){
var response = karate.get('response');
karate.log(response);
if(response.errors!=null)
karate.call('classpath:features/Utils/AfterScenario.feature', { respond: response });
}
AfterScenario.feature
#ignore
Feature: To print response after scenario hook
Scenario:
* print 'RESPONSE ==> ', respond
However the * print command will print it to console but would not log it to html report as it is intended to be. Any help will be greatly appreciated. Thanks!
No, hooks don't print to the HTML file when the entry point is a JS file.

Is there a way to simulate a link/button click in mink?

I'm using a headless browser (phantomjs) in conjunction with Mink to do some functional testing on my Website.
Now in this setting, files can not be downloaded regularly e.g: by clicking a link. So I have to extract the url from the link or the button, and download the file manually.
As I just stated sometimes there is no link () for the download, but a button in a Form (e.g: Inputting data for a report in the form, and receiving the report file on submission).
So what I need to do is simulate clicking the link or button and extract the Data for the Request that would have been sent, and use that data to download the file manually.
Is there a way to do this?
Note: I'm using guzzle to actually download the file.
Mmmm... I don't know if you solved this and only as an alternative to typical mink methods. As Phantomjs is a javascript based browser engine, did you tried with javascript?
You could try something like this:
public function getElementHref($element)
{
/* #var FeatureContext $this */
$function = "(function(){
//Javascript method to get the href.
})()";
try {
return $this->featureContext->getSession()->evaluateScript($function);
} catch (Exception $e) {
throw new Exception('Element not found');
}
}
You can find a method to do this in javascript here: How to get anchor text/href on click using jQuery?
Then use the URL returned with file_get_contents (depending on the file type) and that's it.

CKEditor + KCEditor upload error

I spent the whole day but was unable to find a solution.
I installed CKEditor and KCFinder, everything works fine except for one: when I want to upload a image from the "Browse Server" window file dosen't upload and result is "Unknow error".
When I upload from simple tag "Upload" the file is uploading but is not created a thumb and when I browse the server i can't see the thums of images, only icons of pic and name.
upload:
'uploadURL' => "http://domain.com/uploads/",
'uploadDir' => "../../../uploads/",
config:
CKEDITOR.config.filebrowserBrowseUrl = '<?=$GLOBALS['_st_']['urlTree']?>libraries/kcfinder/browse.php?opener=ckeditor&type=files';
CKEDITOR.config.filebrowserImageBrowseUrl = '<?=$GLOBALS['_st_']['urlTree']?>libraries/kcfinder/browse.php?opener=ckeditor&type=images';
CKEDITOR.config.filebrowserFlashBrowseUrl = '<?=$GLOBALS['_st_']['urlTree']?>libraries/kcfinder/browse.php?opener=ckeditor&type=flash';
CKEDITOR.config.filebrowserUploadUrl = '<?=$GLOBALS['_st_']['urlTree']?>libraries/kcfinder/upload.php?opener=ckeditor&type=files';
CKEDITOR.config.filebrowserImageUploadUrl = '<?=$GLOBALS['_st_']['urlTree']?>libraries/kcfinder/upload.php?opener=ckeditor&type=images';
CKEDITOR.config.filebrowserFlashUploadUrl = '<?=$GLOBALS['_st_']['urlTree']?>libraries/kcfinder/upload.php?opener=ckeditor&type=flash';
when: $GLOBALS['_st_']['urlTree'] is dinamic something like '../' in one or more steps

View PDF in Yii

I have PDF stored in a folder, now i want to view it via link on my form page. How will i do it using Yii framework.
echo CHtml::link(
'pdf',
Yii::app()->createUrl('/uploads/Tutorial.pdf') ,
array('class'=>'button','target'=>'_blank'));
The error i am receiving mentioned below, i have also tried by including uploads in allow of controller.
Error
Error 404
Unable to resolve the request "uploads/Tutorial.pdf".
I am using mPDF to generate PDF but i don't know how to view an already generated PDF using Yii.
You need several more things to make this work. You may have already done some of this and not pasted it but here goes...
I'm assuming you've set up your site to use clean URLs with .htaccess by following this tutorial. Now you can add:
in config/main.php, urlMananger section you need a route to a controller class and action method. This tells Yii which controller to use when you go to the URL /uploads/Tutorial.pdf
'urlManager'=>array(
'urlFormat'=>'path',
'showScriptName'=>false,
'rules'=>array(
'uploads/<filename:[a-zA-Z]+\.pdf>' => 'upload/viewPdf', // actionViewPdf Method in UploadController class
// ... all your other rules
),
),
And a controller class, and a method with the same name as in your route above (prepended with action):
class UploadController extends Controller
{
// Put the usual Yii access control stuff here or whatever...
public function actionViewPdf()
{
$filename = $_GET['filename'] . '.pdf';
$filepath = '/path/to/your/pdfs/' . $filename;
if(file_exists($filepath))
{
// Set up PDF headers
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filepath));
header('Accept-Ranges: bytes');
// Render the file
readfile($filepath);
}
else
{
// PDF doesn't exist so throw an error or something
}
}
}
Then you should be able to use
echo CHtml::link(
'pdf',
Yii::app()->createUrl('/uploads/viewPdf', array('filename' => 'Tutorial')) ,
array('class'=>'button','target'=>'_blank'));
Hope that's helpful.
Well.... are you sure that the uploads folder is available on the server and it is in a location that is link friendly? Yii does not pass everything through it's index.php file, if a file already exists then it just used it like it is. That is why you can access the files in your images or css folder without having a controller for it.
If you are using the normal Yii structure then you should have your "uploads" folder next to your protected folder.
If your uploads folder is not in a web accessible location, use what JamesG told you to. The only change I would to is to the pattern, it only matches letters, you might have other chars in the name too.
Do not add ".pdf" to your link:
Yii::app()->createUrl('/uploads/Tutorial')
in PHP you can print your pdf like this (must be in the actionTutorial):
$mPDF = Yii::app()->ePdf->mpdf();
$mPDF->WriteHTML($this->render('tutorial', array(), true));
$mPDF->Output();
The tutorial is a simply html page that should be rendered into your pdf.
Here you can find the doku to the mpdf Yii extension