-
Notifications
You must be signed in to change notification settings - Fork 671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Disable page caching in browsers for login pages #3780
Comments
I see the same error and think that it does not relate to the role mechanism (at least in this example). <script='./script.js'></script> Moreover, the script section is defined after the |
@AlexKamaev , I don't believe that's the issue. I fixed the script tag The problem is during the role the data in hammerhead|storages-sandbox-temp in localStorage is not being transferred to hammerhead|storage-wrapper|<UUID>|dbschwartz.github.io correctly as it does without a role. Without a role the data in hammerhead|storage-wrapper|<UUID>|dbschwartz.github.io is [["storageBucket"],["[{\"extra\":\"adfasdfasdf\",\"testObj\":{\"abcdef\":\"12345\",\"ghijkl\":\"123123\"},\"link\":\"https://devexpress.github.io/testcafe/example/\"}]"]] but with a role it's That is why when a role is used, the test fails for the JavaScript error of accessing an undefined localStorage key. The link also no longer exists in localStorage.html because it's depending on localStorage to create it. I will post screenshots as soon as I am able. |
I was able to reproduce the issue. I need some additional time to research it in detail. |
Thanks @AlexKamaev, I really appreciate it. |
I researched your issue and found that it's related to the Network Cache. You can see the detailed information in this question on StackOverflow. |
Thank you so much for your help @AlexKamaev! This works! The only caveat I might add is that the problem is also happening in Firefox, so cacheing would have to be disabled there too and perhaps IE, Edge and Safari. I find it odd that it's cacheing issue because localStorage is supposed to happen on the browser itself, rather than through Network Requests. Maybe since TestCafe runs all its commands through the hammerhead server is the reason behind the cacheing issue? Thanks so much for finding a workaround, I hope there is a patch soon so that we can run our tests in all browsers again. |
You are right, the cause of the issue is in the core mechanisms.
We will check this behavior in other browsers as well. |
@dbschwartz import { RequestHook } from 'testcafe';
class CustomRequestHook extends RequestHook {
_onConfigureResponse (event) {
super._onConfigureResponse(event);
event.setHeader('cache-control', 'no-store');
}
async onRequest (event) {
}
async onResponse (event) {
}
}
const hook = new CustomRequestHook(/.*/);
fixture `...`
.requestHooks(hook) Here I override the internal |
Hello, I was having the same problem, the two workarounds didn't work for my tests that used Role. However, when I removed the I suspect that the behaviour - Local Storage is being lost while using Role - might be related to the use of When I used both together the Token behavior in Local Storage varies between:
Then, as a workaround, what worked for me was: Role: export const adminRole = Role(
loginPageUrl,
async t => {
// Perform login
await t
.typeText(userNameInput, USER)
.typeText(userPasswordInput, PASSWORD)
.click(loginPageButton);
// Just for test: Verify if Login with userRole was performed
const loginToken = await getLocalStorage('Token');
console.log('IN ROLE');
console.log(loginToken); // <-- Unpredictable behavior when used with `.page (...)` in fixture: null or valid value only at this point in the rest of the tests is always null
},
{ preserveUrl: true },
);
// Workaround to keep Local Storage between useRole tests;
class CustomRequestHook extends RequestHook {
_onConfigureResponse(event) {
super._onConfigureResponse(event);
event.setHeader('cache-control', 'no-store');
}
async onRequest(event) {
event;
}
async onResponse(event) {
event;
}
}
export const roleWorkaroundHook = new CustomRequestHook(/.*/); Fixture and test: fixture`Visit x page`
.requestHooks(roleWorkaroundHook)
// .page(xPageUrl) <-- Uncomment to verify abnormal behaviour
.beforeEach(async t => {
await t.useRole(adminRole);
// Just for test
console.log('Fixture');
console.log(await getLocalStorage('Token'));
});
test('X render with logged user', async t => {
// Just for test
console.log('test1 - before.js');
console.log(await getLocalStorage('Token'));
await t
.navigateTo(xPageUrl)
.hover(...)
.rightClick(...)
.click(...)
.expect(getCurrentPageUrl())
.contains(xPageUrl, { timeout: 5000 });
// Just for test
console.log('test1 - after.js');
console.log(await getLocalStorage('Token'));
});
test('X does not render with unlogged user', async t => {
// Just for test
console.log('test2 - before.js');
console.log(await getLocalStorage('Token'));
await useAnonymousRole(); // <-- Need reload the page to real logout
await t
.navigateTo(xPageUrl)
.expect(getCurrentPageUrl())
.contains(loginPageUrl, { timeout: 5000 });
// Just for test
console.log('test2 - after.js');
console.log(await getLocalStorage('Token'));
}); Auxiliary Functions: export const getLocalStorage = ClientFunction(key => localStorage.getItem(key));
export const getCurrentPageUrl = ClientFunction(() =>
window.location.href.toString(),
); |
@AllanNobre |
@AlexKamaev |
Feel free to open a new issue if you will have some project (or public URL) to reproduce your scenario issues. |
This thread has been automatically locked since it is closed and there has not been any recent activity. Please open a new issue for related bugs or feature requests. We recommend you ask TestCafe API, usage and configuration inquiries on StackOverflow. |
* initial * fix linting * small refactoring * fix lint * fix review issues * add lost option name * try to fix tests
What is your Test Scenario?
When using a role, when localStorage is set programmatically (via a button click) and is then accessed on a new page, it becomes completely lost. This happens in Test Cafe versions 1.0.0 onward. NOTE: the role is not finished yet when this is happening.
What is the Current behavior?
A localStorage key becomes null after going to a new page in the middle of using a role
What is the Expected behavior?
The localStorage key should not become null in the middle of using a role
What is your web application and your TestCafe test code?
This is my demo repo https://github.com/dbschwartz/test-cafe-local-storage
It runs against a website I built to demonstrate it.
Your website URL (or attach your complete example):
The test code can be viewed in my demo: https://github.com/dbschwartz/test-cafe-local-storage/blob/master/test.js
Your complete test code (or attach your test files):
Your complete test report:
Screenshots:
Steps to Reproduce:
Your Environment details:
The text was updated successfully, but these errors were encountered: