Bug 36607 - Use test builder in cypress tests
Summary: Use test builder in cypress tests
Status: In Discussion
Alias: None
Product: Koha
Classification: Unclassified
Component: Test Suite (show other bugs)
Version: Main
Hardware: All All
: P5 - low enhancement
Assignee: Matt Blenkinsop
QA Contact: Testopia
URL: https://github.com/HKS3/koha-devel/tr...
Keywords:
Depends on:
Blocks:
 
Reported: 2024-04-16 09:55 UTC by Matt Blenkinsop
Modified: 2024-05-06 20:07 UTC (History)
6 users (show)

See Also:
Change sponsored?: ---
Patch complexity: ---
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:
Circulation function:


Attachments
Bug 36607: Add cypress commands to build and delete objects (4.01 KB, patch)
2024-04-16 10:00 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 36607: [DO NOT PUSH] Test for the commands (3.28 KB, patch)
2024-04-16 10:00 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 36607: Add cypress commands to build and delete objects (4.66 KB, patch)
2024-04-16 10:03 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
Bug 36607: [DO NOT PUSH] Test for the commands (3.28 KB, patch)
2024-04-16 10:03 UTC, Matt Blenkinsop
Details | Diff | Splinter Review
added teardown (5.58 KB, patch)
2024-05-06 06:02 UTC, Mark Hofstetter
Details | Diff | Splinter Review
Bug 36607: [discuss] setup and teardown patron (2.46 KB, patch)
2024-05-06 20:06 UTC, Mark Hofstetter
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Blenkinsop 2024-04-16 09:55:49 UTC
At hackfest it was discussed that it would be useful to be able to use the perl TestBuilder module in Cypress along with the new Cypress mysql. This bug is for a proof of concept to discuss
Comment 1 Matt Blenkinsop 2024-04-16 10:00:57 UTC
Created attachment 164899 [details] [review]
Bug 36607: Add cypress commands to build and delete objects

This patch is a proof of concept for using test builder in cypress. The commands take in a Koha class and any data values and pass these to a perl script to run test builder

To delete values we can either pass values to identify the data to delete or simply tell the command how many rows to delete from the end of the table
Comment 2 Matt Blenkinsop 2024-04-16 10:00:59 UTC
Created attachment 164900 [details] [review]
Bug 36607: [DO NOT PUSH] Test for the commands

cypress run --spec t/cypress/integration/DataTest_spec.ts
Comment 3 Matt Blenkinsop 2024-04-16 10:03:26 UTC
Created attachment 164901 [details] [review]
Bug 36607: Add cypress commands to build and delete objects

This patch is a proof of concept for using test builder in cypress. The commands take in a Koha class and any data values and pass these to a perl script to run test builder

To delete values we can either pass values to identify the data to delete or simply tell the command how many rows to delete from the end of the table
Comment 4 Matt Blenkinsop 2024-04-16 10:03:28 UTC
Created attachment 164902 [details] [review]
Bug 36607: [DO NOT PUSH] Test for the commands

cypress run --spec t/cypress/integration/DataTest_spec.ts
Comment 5 Matt Blenkinsop 2024-04-16 10:06:18 UTC
The patch above allows us to use an object builder directly in the test - the alternative would be to just write a perl data generation script and call that directly at the start and end of each test to generate/tidy up data.

Ideally the command could also return the newly created value but that is a future step as we can retrieve it with cypress mysql for now

Let me know any concerns/feedback
Comment 6 Jonathan Druart 2024-04-16 11:12:28 UTC
(In reply to Matt Blenkinsop from comment #5)
> The patch above allows us to use an object builder directly in the test -
> the alternative would be to just write a perl data generation script and
> call that directly at the start and end of each test to generate/tidy up
> data.
> 
> Ideally the command could also return the newly created value but that is a
> future step as we can retrieve it with cypress mysql for now
> 
> Let me know any concerns/feedback

We need to cleanup behind us. So for instance if you create a patron you need to remove it AND what has been auto-generated: library + patron's category.

So yes, we should retrieve the id of the resource we have created. The "delete the last X entries" approach is not robust enough IMO.

See t/db_dependent/selenium/regressions.t:    push @cleanup, $patron, $patron->category, $patron->library;
Comment 7 Mark Hofstetter 2024-05-06 06:02:34 UTC
Created attachment 166180 [details] [review]
added teardown
Comment 8 Mark Hofstetter 2024-05-06 06:13:52 UTC
hi,

I think Jonathan is right, ideally tests should be idempotent ie you have to clean up after testing. I added a "teardown" option in both 

t/cypress/support/e2e.js
t/cypress/support/cypress_builder.pl

additionally I think there was an error in 
t/lib/TestBuilder.pm

because it used hashes and not objects when trying to delete objects

to run 

yarn cypress run --config video=false,screenshotOnRunFailure=false --spec t/cypress/integration/DataTestSetupTeardown_spec.ts

which creates a patron and then removes exactly this patron + the library/branch created implicitly. If this approach seems worthwhile it needs fleshing out for each object.

the point of this approach is that you may not simply create an object randomly but you have to use a identifier, in the case of a patron the cardnumber. 

with this identifier you "keep state" during the test and in the end you issue a teardown command which not only deletes the explictly created object, but also all dependent object.
Comment 9 Mark Hofstetter 2024-05-06 20:06:37 UTC
Created attachment 166237 [details] [review]
Bug 36607: [discuss] setup and teardown patron

please checkout/push to https://github.com/HKS3/koha-devel/tree/bug-36607


Current status: In Discussion