Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does TestCafe require useRole call in every test/beforeEach

I've been evaluating TestCafe for an app that requires user authentication. The documentation isn't very clear and I've had trouble getting a straight answer on how we should be using useRole.

Our application requires user authentication, right now we only test a single user so we have no need to switch user sessions.

I've defined a Role and it authenticates correctly. But I've noticed the following:

  1. I need to call useRole first in every test in order to use the authenticated session
  2. Every time useRole is called (first in every test) TestCafe navigates the browser back to the original login URL (or whatever preserveUrl saves post-login)

Are either of these statements wrong? I can't imagine how this works in a real environment, that's an insane amount of redirects.

Item 2 seems correct, a devexpress github contributor replied "Currently, TestCafe can't use a Role without reloading or triggering page navigation" so if I have to call useRole in every test that's literally doubling the HTTP navigation load.

like image 470
helion3 Avatar asked Feb 28 '19 18:02

helion3


People also ask

What is test controller in TestCafe?

The test controller object exposes the test API's methods. The test controller is passed to each function that can run server-side test code (like test, beforeEach or afterEach). Use the test controller to call test actions, handle browser dialogs, use the wait function, or execute assertions.

Is Cypress faster than TestCafe?

Cypress is a front end automated testing application created for the modern web. Cypress is built on a new architecture and runs in the same run-loop as the application being tested. As a result Cypress provides better, faster, and more reliable testing for anything that runs in a browser.

Does TestCafe use selenium?

Unlike most testing solutions, TestCafe is not built on Selenium. This allows us to implement features you cannot find in Selenium-based tools (for example, testing on mobile devices, user roles, automatic waiting, etc.). TestCafe uses a URL-rewriting proxy which allows it to work without the WebDriver.


1 Answers

The purpose of useRole is to authenticate to the app only once (per user): this means you will see the login page in the first test, and all other tests will start directly on the App page with the user being already authenticated.

The problem is that every test runs in a sandbox. The sandbox is per test and not per fixture. This means that when a test starts to execute, it starts in a brand new sandbox with no cookies and no local storage.

The only way to re-apply cookies and local storage is to call useRole. This is why useRole must be called at the beginning of each test.

useRole is a huge time saver. When I started to work with TestCafe (more than one year ago) useRole did not exist and each test would start by feeding the login page.

useRole is even more useful when you need to switch, inside a test, between different users.

And then to finish, yes, useRole reloads the App page because each test starts in a sandbox with no page history.

What you are looking for is a feature that does not exist: do not reload page between test. If you do not want to reload the page each time, do all you tests in a single test method.

like image 89
hdorgeval Avatar answered Oct 03 '22 21:10

hdorgeval