In this post, I bring up another important missing element in my tests. It is the proper management of the URLs of the application that I am automating tests of.
Poprzedni post w tej tematyce znajdziesz tutaj: Test refactoring - App Actions vs Page Object Model
Do you like my blog?
Subscribe to mail list, to be first which will be notified about new posts and informations:
The first item which qualifies for refactoring today is loading the appropriate subpage of the application. This operation is performed on each test which blurs its logic and purpose. I have in mind precisely this line:
Certainly not only me have noticed that it makes no sense and should be solved in a better, cleaner way. Of course, Cypress provides tools that will allow us to clean up this disorder. Like most tools, frameworks or test libraries, we have access to functions that allow us to run the code before or after the test. Cypress’s authors call them hooks. We have 4 functions of this type to choose from:
Applying it to the application tests which I am trying to automate I know that before each test I have to navigate to a specific page Looks like an excellent case for the beforeEach() function. The implementation will not be too complicated:
You can easily see that such a simple change significantly improved the quality of tests. It has removed an unnecessary step that is needed in the test case study and highlighted the need to navigate to the desired subpage before the test can be performed. During the test execution we can notice that the BEFORE EACH block is appropriately separated in the Cypress window:
The next problem is the spread of URLs in function arguments. Attempting to change the URL to test the application on another, for example non-local environment, would end up with a headache by changing it in every place, where that address is used. I will use two mechanisms to fix this. The first is to set: baseUrl, and the next is setting environment variables. Both refer to the storage of variables in the configuration file. Setting baseUrl consists in adding the appropriate entry to the file configuration which will modify the default behavior of the cy.visit() function and cy.request(). Going to the implementation, I modify the cypress.json file as follow:
Then I can change the cypress/integration/LoginTests.AppActions.spec.js tests to the form:
It turns out, however, that the test is not exactly the same as before the changes. On the URL ASSERT line you notice this type of log: “expected http://localhost:4100/ to include /”. The test only checked that the URL had the following string “/” in it characters. This is an unacceptable situation and should be corrected. Fortunately, there is an easy way to do this, we can easily retrieve the value of baseUrl parameter saved in the configuration using the function: Cypress.config(‘arg’), the implementation looks like this:
This fixes one of the problems of this function. Unfortunately, there is another serious defect in it. It only checks that the URL contains the specified string. This is a wrong approach, which was noticed by one person in the comments, for which thanks. The correct approach is to check that the string is equal to the one in which we expect. Instead of ‘contain’, a ‘equal’ should have been used. Why? In the current situation, if the application, after executing the test logic, would wrongly redirect us to the page: http://localhost:4100/you_should_not_be_here, the test would pass which is incorrect. A few lines of code, and they managed to contain as many as two defects - quite good score. I also change the name of the function to correctly reflect the current operation and the correct implementation looks like this:
Of course, we should now be careful not to pass the full URL address to the function, however, this approach seems consistent with the native behavior of Cypress, which authors propose. Besides, we can always add the appropriate if block to the function, which will solve this problem. Currently, it is not a reason for further changes so I am not changing this one function in conjunction with this note. I can see another small option to improve this block of code with is putting the full URL into a separate variable. The function looks like this now and I think it doesn’t need any more changes:
I am also applying the changes described above to tests using Page Object Pattern by putting the URL into a separate variable and modifying the assertions:
The app’s backend is at a different address, so it should be too put in configuration. I am adding a new entry to the cypress.json file:
Then I extract the URL into a separate variable in the file cypress/support/api/apiCommands.js Using the same approach as in baseUrl I am replacing only the name of the parameter I am looking for in the file configuration. Use of the function will look like this Cypress.config(‘apiUrl’) and implemented as follows:
I also notice one defect, the arguments passed to the function were not used. It turns out that the tests use the hardcoded once. Correct version:
Here I also apply changes to the cypress/requests/User.js file:
As above, there is also a defect in this function. This is a logical error - I do not delete the user before creating the new one. The reason why I am doing this is discussed in the post: Login tests with Cypress in the section Implementation of first test. The correct version of the create() function:
As you can see, Cypress gives us the ability to easily manage and read configuration. Of course, that’s not all it offers developers. The first handy feature that comes to mind is overwriting configuration, which will certainly come in handy when changing it in the CI process and will definitely be described in the future. You can find all the changes in my repo on the branch, here:
It turns out that I accidentally introduced one more defect, which makes it impossible running tests: cypress/integration/LoginTests.AppActions.spec.js. The solution to the problem is the following code:
Still hungry for knowledge? Take it for free!
16 things which you have to know to efficiently automatize tests and library list which I use in every my test project, click here