Laravel 5: Testing – Cheatsheet: Laravel Dusk testing element interactions

Cheatsheet: Laravel Dusk testing element interaction methods

Laravel Dusk: list of page elements interaction methods.

Dynamically assign, or remove values from page elements.

[one_third]
clickLink
value
text
attribute
keys
type
append
clear
select
[/one_third][one_third]
radio
check
uncheck
attach
press
pressAndWaitFor
script
drag
dragUp
[/one_third][one_third_last]
dragDown
dragLeft
dragRight
dragOffset
acceptDialog
typeInDialog
dismissDialog
[/one_third_last]


Methods listed above in dark color have not been tested (yet)

Important – Dialog Methods

This is not about modal dialogs, e.g. Bootstrap Modal Dialog.
This method tests these little pesky, page locking JavaScript
popups triggered by JS code alert(‘some stuff here’);


value

text

attribute

keys

type

append

clear

select

radio

check

uncheck

attach

press (click) button

pressAndWaitFor

script

 

 

Laravel 5: Testing – Cheatsheet: Laravel Dusk testing assertions

Cheatsheet: Laravel Dusk testing assertions

Laravel Dusk: list of assertions.


[one_third]
assertTitle
assertTitleContains
assertUrlIs
assertPathIs
assertPathBeginsWith
assertPathIsNot
assertFragmentIs
assertFragmentBeginsWith
assertFragmentIsNot
assertRouteIs
assertQueryStringHas
assertQueryStringMissing
assertHasCookie
assertCookieMissing
[/one_third][one_third]
assertCookieValue
assertPlainCookieValue
assertSourceHas
assertSourceMissing
assertSee
assertDontSee
assertSeeIn
assertDontSeeIn
assertSeeLink
assertDontSeeLink
assertInputValue
assertInputValueIsNot
assertChecked
assertNotChecked
[/one_third][one_third_last]
assertRadioSelected
assertRadioNotSelected
assertSelected
assertNotSelected
assertSelectHasOptions
assertSelectMissingOptions
assertSelectHasOption
assertSelectMissingOption
assertValue
assertVisible
assertPresent
assertMissing
assertDialogOpened
[/one_third_last]


Title

assertTitle

assertTitleContains

Url

assertUrlIs

Path (relative)

assertPathIs

assertPathBeginsWith

assertPathIsNot

Url fragment

What is url fragment?

assertFragmentIs

assertFragmentBeginsWith

assertFragmentIsNot

Route

assertRouteIs

Query: url params (GET key/val pairs) testing

assertQueryStringHas

assertQueryStringMissing

Cookie

assertHasCookie

assertCookieMissing

assertCookieValue

assertPlainCookieValue

HTML Source Code

assertSourceHas

assertSourceMissing

Text on page

assertSee

assertDontSee

Text on page inside of selectors

A few words about selectors.

Selectors seem to be HTML tags – see here, but also elements identified by ID, CLASS
They are case insensitive, your tag could be ‘H1’, but in assertion you can ask for ‘h1’.
Allows for text to be inside of other elements, like: a, label, input etc. – sample:


assertSeeIn

assertDontSeeIn

Link

Input

assertInputValue

assertInputValueIsNot

Checkbox

assertChecked

assertNotChecked

Radio

assertRadioSelected

assertRadioNotSelected

Drop-down (<select>)

assertSelected

assertNotSelected

assertSelectHasOptions

assertSelectMissingOptions

assertSelectHasOption

assertSelectMissingOption

Selector (form field etc.- all that have attr value) current value

assertValue

Visibility

assertVisible

seem to add functionality to all ‘see in code’ methods described above

assertPresent

assertMissing

Dialog Open

assertDialogOpened

 

Laravel 5: Testing – Laravel Dusk – browser testing – general info

Testing – Laravel Dusk – browser testing – general info

Laravel Dusk is enriched replacement for ‘Integrated Package’.

Laravel Dusk requires extra setup. More: read here.
Cheatsheets
Most used Testing assertions are described here:
  1. PHPunit testing
  2. Dusk browser testing (selenium-like)

Rest of them can be found in this directory: vendor\laravel\dusk\src\Concerns\

General
Generating Dusk test files

Running Dusk tests

You can run @group PHPUnit annotation:

… but you cannot run –testsuite:

Fortunately, you do not have to use php artisan dusk at all.
Just use standard phpunit commands for dusk using testsuites and groups and other annotations, e.g.:

or

For testsuite, you have to add to phpunit.xml:

 

Now you have access to all commands standard PHPUnit runs, like:

  • –filter <pattern> : Filter which tests to run.
  • –testsuite <pattern> : Filter which testsuite to run.
  • –group … : Only runs tests from the specified group(s).
  • –exclude-group … : Exclude tests from the specified group(s).
  • –list-groups : List available test groups.
  • –list-suites : List available test suites.
  • –test-suffix … : Only search for test in files with specified suffix(es)

 

More PHPUnit CLI commands – here.

Some samples
Creating Browsers

Browser is a set of instructions testing some functionality as user would by opening browser and interacting with it.

Lets take this ‘browser’ for example:

So, as you see in comments above, you just construct – step by step scenario, exactly resembling actions a normal user would do.
If this is not intuitive … I do not know what is.

Lets see another example, where actions had to be split among multiple ‘browsers’.

Great stuff.


Next page: detailed info about all available tools.

 

Laravel 5: Testing – Cheatsheet: Laravel specific PHPUnit testing assertion

Cheatsheet – Laravel Specific PHPUnit Testing Assertion

All PHPUnit docs page with all methods and annotations – see here

[one_third]
assertSuccessful
assertStatus
assertRedirect
assertHeader
assertHeaderMissing
assertPlainCookie
assertCookie
assertCookieExpired
assertCookieMissing
assertSee
assertSeeInOrder
[/one_third][one_third]
assertDontSee
assertSeeText
assertDontSeeText
assertSeeTextInOrder
assertJson
assertExactJson
assertJsonFragment
assertJsonMissing
assertJsonMissingExact
assertJsonStructure
assertJsonCount
[/one_third][one_third_last]
assertJsonValidationErrors
assertViewIs
assertViewHas
assertViewHasAll
assertViewMissing
assertSessionHas
assertSessionHasAll
assertSessionHasErrors
assertSessionHasErrorsIn
assertSessionMissing
[/one_third_last]


LARAVEL SPECIFIC TESTING HELPER METHODS

Check HTTP code
assertSuccessful

assertStatus

Redirects
assertRedirect

Headers
assertHeader

assertHeader

Cookies
assertPlainCookie

assertCookie

assertCookieExpired

assertCookieMissing

Check response page code – including HTML tags
assertSee

assertSeeInOrder

assertDontSee

Check response page code – text only
assertSeeText

assertDontSeeText

assertSeeTextInOrder

JSON
assertJson

assertExactJson

assertJsonFragment

assertJsonMissing

assertJsonMissingExact

assertJsonStructure

assertJsonCount

assertJsonValidationErrors

decodeResponseJson

Views
assertViewIs

assertViewHas

assertViewHasAll

assertViewMissing

Session
assertSessionHas

assertSessionHasAll

assertSessionHasErrors

assertSessionHasErrorsIn

assertSessionMissing

Authentication Assertions
assertCredentials

assertInvalidCredentials

 

 

 

HTTP tests – see Laravel docs.

Laravel 5: Testing – Laravel specific & PHPUnit testing – general info

How2s: Laravel Specific PHPUnit Testing Assertion

Laravel provides a very fluent API for making HTTP requests to your application and examining the output.
Cheatsheets
Most used Testing assertions are described here:
  1. PHPunit testing
  2. Dusk browser testing (selenium-like)

Rest of them can be found in this directory: vendor\laravel\dusk\src\Concerns\


HTTP assertions may be accessed on the response that is returned from one of HTTP methods:  POST, GET, PUT, PATCH, and DELETE.
You can specify which method you want to use:

Or use Laravel provided shortcut, like:

Above you use, if you test html page and try to make sure some stuff is present.
To test  using server actions, before they actually will get pass view(), skip get():

Also non-http assertion e.g. provided by Laravel traits may be used:

All PHPUnit docs page with all methods and annotations – see here
Please note:

Laravel allows to use all PHPunit provided assertions (and annotations). Just go to PHPunit docs, grab assertion you need and use it (as already mentioned above ) something like this:

Laravel 5: Testing – General: test types, running tests, environment etc.

Testing – test types, running tests, environment etc.

Every developer test his app.
But large organizations, where people come and go, felt they need some more formalized testing.
Testing, where code tests some other code.
But the bottom line is like in this old saying: “Who polices the Police!”.
In other words, test are only as good as a coder, who writes them.
Of course in OOP environment, is makes sense to have some formalized, software driven tests, to see if something done in one place does not wreck something else, somewhere else.

Full PHPUnit documentation – see external docs here.
There is internal PDF version for PHPUnit v.6 – see here.
Testing types in Laravel 5.4

In pre 5.4 there were no folders inside of /tests, now we have some.

∴ \tests main folder

 \Browser sub-folder

Easy-to-use browser output testing API.
In other words, check if what should appear in browser, really appears.

This is a Laravel 5.4 Dusk only feature.

What is Dusk.
It is enriched replacement for ‘Integrated Package’.
It requires extra setup.
More: read here.

Here is how to create Dusk test file:

 \Feature sub-folder

Feature tests may test a larger portion of your code, including how several objects interact with each other or even a full HTTP request to a JSON endpoint.
In other words, you make HTTP requests to your application and examine the output.

Here is how to make scaffolding for this kind of test:

 \Unit sub-folder

These are standard phpunit-like tests.
Unit tests focus on a very small, isolated portion of your code (e.g. on a single method).

Here is how to make scaffolding for this kind of test:

Environment
From docs:
When running tests via phpunit, Laravel will automatically set the configuration environment to testing because of the environment variables defined in the phpunit.xml file.
Laravel also automatically configures the session and cache to the array driver while testing, meaning no session or cache data will be persisted while testing.
HOWEVER

It is good for to make testing database (sometimes) and put this below QUEUE_DRIVER in phpunit.xml:

Then db_local should be defined in /config/database.php in ‘connections‘ array.

Running tests

Running tests via command in console:

Command ‘phpunit’ returns ‘No tests executed!’

If you see  No tests executed!, when you run phpunit, you may want to install phpunit as ‘global’ and add it’s path to Windows ‘environment variable path’ – for more, see this: link.

Otherwise run: vendor\bin\phpunit.
If you use out-of-the-box phpunit, which comes with Laravel, this is the only command that would work, as phpunir is not ‘global’.

Also, this may happen, when your tests were made using phpunite lower than v.6
You are probably extending class PHPUnit_Framework_TestCase. As of v.6 that class is called: TestCase. This will also get you: No tests executed! response. See this link.

For Dusk use this command:

Both commands accepts any argument that is also accepted by the phpunit command.
  • –filter <pattern> : Filter which tests to run.
  • –testsuite <pattern> : Filter which testsuite to run.
  • –group … : Only runs tests from the specified group(s).
  • –exclude-group … : Exclude tests from the specified group(s).
  • –list-groups : List available test groups.
  • –list-suites : List available test suites.
  • –test-suffix … : Only search for test in files with specified suffix(es)

More PHPUnit CLI commands – here.

 

Lang translation in controller, model …

Lang translation in controller, model …

How to get language translation inside of controller, model etc.

 

Cookies and View

Cookies and View

See: this thread

way #1: queue (seems best)

way #2: use CookieJar

way #3: use response

 

Get redirect back link

Get redirect back link

Why would you want this?

E.g. you are in ‘edit’ page you got to from listing.
You want to have redirect link back exactly to page in listing all with proper page number and sorting.
You could use:

… but above would also pickup all JS activity, you may not want, like Toastr notifications for example.

If you need a link to do back and not just a redirect back e.g. in controller, here are options:

And solutions that work with cloud/cluster hosting:

Laravel 5: Configuration: database based configuration

Laravel 5: Configuration: database based configuration


Laravel offers great, file based configuration, but it is cumbersome to adjust from GUI.

GUI friendly database based configuration.
Database tables
I use 3 tables to accomplish database based config.
  1. configurations
    This table holds main configuration key/value pairs, plus some extra stuff.
  2. configuration_groups
    This table is just in case, if you get to have a lot of configuration data and want to group them in some GUI.
  3. configurables
    If you have many user types, with different access levels, you may use this to link particular configuration key/value to user. Since this is a poly-pivot table, you can easily clone multiple groups at any time of your app development. Of course this is meant for GUI use, if you want to allow editing of db based config values.
Migration Files
configurations:

configuration_groups:

configurables:

Language Files

Columns: ‘name’, ‘description’ do not hold actual names, or description, but location to language localized translations.

Here are my language files.

configuration

configuration_groups

IMPORTANT:

If you group your files in subfolders, dot notation would not work. You have to use slashes, e.g.:

Service Provider code

I used existing service provider: AppServiceProvider, to fire my database based config values.
See method: dbConfig();

I am using cache, to make it speedy.
My cache never expires.
Reason for this forever cache is that I want it to hold, until it is changed, e.g. from GUI.
It will be (should be) overwritten, when anything is changed va GUI.