Verifying presence of alias or comparing text in alias to string in 'if' statement #28290
Unanswered
Ixluxi
asked this question in
Questions and Help
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi,
I'm creating an API automation suite.
I have API tests which don't require logging in, and ones that do.
I have written a utility method called 'callAPI' which all my tests will use - it has a bunch of error checking and automatically augments calls with relevant headers/authorization token, depending on what the caller passes into the method.
I also have a 'logIn' method which creates an alias storing an authorization token.
In the scenario where the automation engineer calls callAPI to do an API request that requires logging in, I want to be able to detect that they haven't logged in and throw an error message advising as such.
Approach 1:
If the alias 'accessToken' doesn't exist, throw the error.
Problem:
It appears that the use of cy.state('aliases') has been deprecated.
Approach 2:
Create the alias in the 'before' hook with the default value 'accessToken not set'...
Then in callAPI, check if the alias's value is equal to 'accessToken not set' and throw the error.
Problems:
First 'if' statement displays "This comparison appears to be unintentional because the types 'Chainable<JQuery>' and 'string' have no overlap."
Second 'if' statement displays "This comparison appears to be unintentional because the types 'Chainable' and 'string' have no overlap."
I've perhaps mistakenly assumed that it should be trivial to compare the text within an alias in an 'if' statement, but apparently this is not a thing...?
Approach 3:
The intention here would be to do the comparison within the 'then'....
Problem:
"data.text is not a function"
Approach 4:
Within my 'if' statement, assert that the alias's text equals 'accessToken not set'.
Problem:
If this assertion fails, Cypress throws its own error, defeating the object of throwing my custom error.
My code:
Unless I'm missing the obvious (wouldn't surprise me), is there any way to address this problem? Or should I just rely on the API informing the automation engineer that they need to log in?
I'm using TypeScript if that matters.
Thank you for any help you may be able to offer.
Beta Was this translation helpful? Give feedback.
All reactions