Using JavaScript to get the text of an element using Laravel Dusk - laravel

I'm doing automated testing using Laravel Dusk, when I do this:
$test = $browser->script('$(".page-sidebar-menu").text();');
dd($test);
It returns array of null, but if run $(".page-sidebar-menu").text(); in a browser, it returns all text inside that class.
Where I go wrong in here? Please help if you know.

Okay it's wrong of me to asked this, I not include return inside script
it should be like this
$test = $browser->script('return $(".page-sidebar-menu").text();');
dd($test);

Related

why scrapy won't load any of my pipelines?

ok, so Im using Scrapy for some basic web scraping and its working fine on scraping part! when get some output using feed export something like -o output.csv wont do anything, it will make an empty file but nothing else.
after a period of confusion I couldn't make it work so i've decided to use a pipeline to write some custom method of exporting. but now the problem is even though the application is working fine... its just not load the pipelines. not any single one of them is not running and there is no error.
this is my settings.py where I put the option to load them:
ITEM_PIPELINES = {
'fbcrawl.pipelines.CsvExporterPipeline': 300
}
and this is my CsvExporterPipeline class inside pipelines.py:
class CsvExporterPipeline(object):
def process_item(self, item, spider):
print('\n' * 2)
print(item)
print('\n' * 2)
return item
and its not gonna run neither of these 3 prints wont run at all.
I want to know how can I have my pipelines loaded and working?
UPDATE: i forgot to mention that im trying to run this code... so the spider is mentioned here:
https://github.com/rugantio/fbcrawl
Are you sure BOT_NAME in settings.py is set at fbcrawl ?
And what is the code of your spider ?

Laravel Dusk: element not interactable when used vue-fragment

I struggle to use Dusk with this package. It removes the "you should return one root element" limitation.
Now I want to write Laravel Dusk tests, but I get the error:
1) Tests\Browser\HomepageTest::it_shows_the_correct_value
Facebook\WebDriver\Exception\ElementNotInteractableException: element not interactable
(Session info: chrome=81.0.4044.113)
If I don't use fragment, everything works... but unfortunately, I have to use it.
I created a repository to test all the Laravel Dusk tests. https://github.com/pmochine/fragment-dusk
Any idea how I can do the test without calling the $browser->script() function?
You need to change this line
$browser->assertSeeIn('.dialog', 'Option1');
in your test this_test_fails_even_though_we_try_to_use_script to this
$text = $browser->script("return document.querySelector('.dialog').textContent")[0];
$this->assertEquals('Result: Option1', trim($text));
And the reason laravel dusk can't find the contents of the fragment it's because, Fragment tag would not read as a node by the DOM. You can read more about it here https://blog.logrocket.com/fragments-in-vue-js/

Laravel - OutputStyle from jobs

I'm running into issues with allowing a Laravel job to interact with the console output.
At the moment I am passing in the OutputStyle from a Command to the Job constructor and assigning it.
I have seen the InteractsWithIO trait but if I use that by itself without assigning the OutputStyle from the command then it says it is null.
Call to a member function title() on null
I have also tried setting $this->output from the container using
$this->output = resolve(OutputStyle::class);
This fails with a
Target [Symfony\Component\Console\Input\InputInterface] is not instantiable while building [Illuminate\Console\OutputStyle].
I've also ran into issues with PHPUnit tests that run through this job. The output from the class is displayed in the test output.
.......................Processing element 1 for "Section"
.......
What's the best way to handle outputting to the console within Laravel that also works with PHPUnit?
Putting the following code in a Service Provider works:
$this->app->bind('console.output', function () {
return new OutputStyle(
new StringInput(''),
new StreamOutput(fopen('php://stdout', 'w'))
);
});
I am then able to say, in my Job,
$this->output = resolve('console.output');
Which gives access to all the methods such as title, section, and table.

View::make in Phpunit

I've a function that returns a View::make($string). I want to test that this function did indeed return an instance of View object. $string points to a file that does exist.
When I try to run this function within Phpunit it doesn't seem to finish. How can I test in Phpunit that a View object was created?
Laravel has helper methods specifically designed for testing views.
Some of them include:
$response = $this->get('/path/to-your-route');
$response->assertViewIs($value);
$response->assertViewHas($key, $value = null);
$response->assertViewHasAll(array $data);
$response->assertViewMissing($key);
More info can be found here: https://laravel.com/docs/5.5/http-tests#available-assertions
If you need to assert that something is an instance of something else, you can try the following:
$this->assertInstanceOf($expected, $actual);
When you provide invalid string the view object will not be created and will throw an exception. Not sure what you have in your function that prevents the exception, but the way to go around this issue, is to include this line in the failing test:
$this->expectException(InvalidArgumentException::class);
The issue stemmed down from usage of var_dump as I wanted to see the object in question. As nothing was presented in output, I assumed that had to do with View::make rather than outputting the object to the console.

Codeception API testing: stuck at passing json payload to REST service

I found this cool tool called codeception for testing in PHP. I am liking it a lot. I started writing API test cases. But I am stuck at POSTING json payload to a REST service. How can I perform this?
I have a REST end point called /order, which accepts a JSON payload. The service is build on Laravel4, so I accept the payload in Laravel4 using Input::json()->all().
I have tried something like this
$filename = __DIR__.'/createOrder.json';
$I->haveHttpHeader('Content-Type', 'application/json');
**$I->sendPOST('order', null, array($filename));**
$I->seeResponseCodeIs(200);
$I->seeResponseIsJson();
But it gives me 500 internal server error, as my service accepts json payload and not in the form of file.
Anyone has worked on something like this before?
Thanks in advance.
I know this is an old question but for others who stumble on this, try:
$I->haveHttpHeader('Content-Type','application/json');
it definitely should work.
I got it working by doing this
$exampleData = [
'name' => 'adam',
'age' => 99
];
$json = json_encode($exampleData);
$I->sendPOST('/endpoint', $json);
For more details see this Github issues
# Sameer, I have tried and faced similar issue. I then went with a work around for this.
$filename = __DIR__.'/createOrder.json';
$I->sendPOST('order', getPostParams($filename));
Now create a new folder called "helpers" inside your suite and add "<suiteName>Helper.php" to it.
Add the following code there
function getPostParams($filename){
if(!file_exists($filename)){
print "MISSING FILE ".$filename."\n";
return;
}
$jsonData= json_decode(file_get_contents($filename));
if(!$jsonData){
print "INVALID JSON CONTENT ".$filename."\n";
return;
}
return (array)($jsonData);
}
Go to _bootstrap.php inside "tests" folder and add this
require_once('<suiteName>/helpers/<suiteName>Helper.php');
Now do a build and run the test suite. you should be able to get pass the error :)

Resources