Skip to content
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

Role doesn't work when page navigation doesn't trigger page reloading #2195

Closed
LavrovArtem opened this issue Mar 6, 2018 · 3 comments
Closed
Assignees
Labels
AREA: client STATE: Auto-locked An issue has been automatically locked by the Lock bot. STATE: Need improvement A minor issue that can't be treated as a bug. SYSTEM: automations
Milestone

Comments

@LavrovArtem
Copy link
Contributor

Are you requesting a feature or reporting a bug?

bug

What is the current behavior?

Role doesn't work after first-time initialization. Cookie, localStorage and sessionStorage should be restored when a preserved page is loaded but the page changes only the hash and it isn't reloaded after navigating.

What is the expected behavior?

Page must be reloaded after the useRole function call.

How would you reproduce the current behavior (if this is a bug)?

Node server:

const http = require('http');

http
    .createServer((req, res) => {
        if (req.url === '/') {
            res.writeHead(200, { 'content-type': 'text/html' });
            res.end(`
                <h1 id="header"></h1>
                <a id="anchor" href="#login">log in</a>
                <input id="button" type="button" value="log in">

                <script>
                    var onHashChange = function () {
                        var newHash = location.hash;

                        if (newHash === '') {
                            if (localStorage.getItem('isLoggedIn')) {
                                header.textContent = 'Authorized';
                                header.style.display = 'block';
                                anchor.style.display = 'none';
                                button.style.display = 'none';
                            }
                            else {
                                header.textContent = 'Unauthorized';
                                anchor.style.display = 'block';
                                button.style.display = 'none';
                            }
                        } 
                        else if (newHash === '#login') {
                            if (localStorage.getItem('isLoggedIn'))
                                return location.hash = '';

                            header.style.display = 'none';
                            anchor.style.display = 'none';
                            button.style.display = 'block';

                            button.addEventListener('click', function() {
                                localStorage.setItem('isLoggedIn', 'true');
                                location.hash = '';
                            });
                        }
                    };

                    onHashChange();

                    window.addEventListener('hashchange', onHashChange);
                </script>
            `);
        }
        else
            res.end();
    })
    .listen(4100);

Provide the test code and the tested page URL (if applicable)

Test code

import { Role, ClientFunction, Selector } from 'testcafe';

fixture `Test authentication`
    .page `http://localhost:4100/`;

const role = Role(`http://localhost:4100/#login`, async t => await t.click('input'), { preserveUrl: true });

test('first login', async t => {
    await t
        .wait(3000)
        .useRole(role)
        .expect(Selector('h1').innerText).eql('Authorized');
});

test('second login', async t => {
    await t
        .wait(3000)
        .useRole(role)
        .expect(Selector('h1').innerText).eql('Authorized');
});

Workaround

import { Role, ClientFunction, Selector } from 'testcafe';

fixture `Test authentication`
    .page `http://localhost:4100/`;

const role = Role(`http://localhost:4100/#login`, async t => await t.click('input'), { preserveUrl: true });
const reloadPage = new ClientFunction(() => location.reload(true));
const fixedUseRole = async (t, role) => {
	await t.useRole(role);
	await reloadPage();
};

test('first login', async t => {
    await t.wait(3000)
    await fixedUseRole(t, role);
    await t.expect(Selector('h1').innerText).eql('Authorized');
});

test('second login', async t => {
    await t.wait(3000)
    await fixedUseRole(t, role);
    await t.expect(Selector('h1').innerText).eql('Authorized');
});

Specify your

  • testcafe version: 0.19.0
@LavrovArtem LavrovArtem added TYPE: bug The described behavior is considered as wrong (bug). AREA: client labels Mar 6, 2018
Rebolon pushed a commit to Rebolon/php-sf-flex-webpack-encore-vuejs that referenced this issue Mar 8, 2018
@miherlosev miherlosev added this to the Planned milestone May 7, 2018
@lennytakahashi
Copy link

Thanks a lot for the workaround. That really helps to ease the pain. (testcafe version: 0.20.4)

@uvizhe
Copy link

uvizhe commented Jul 31, 2018

I'm using hash based navigation and my workaround is:

await t
  .useRole(role)
  .navigateTo('/')
  .wait(500)
  ...

testcafe 0.20.5

@AndreyBelym AndreyBelym modified the milestones: Planned, Sprint #16 Aug 14, 2018
@AndreyBelym AndreyBelym modified the milestones: Sprint #17, Sprint #18 Sep 17, 2018
@AndreyBelym AndreyBelym modified the milestones: Sprint #18, Planned Oct 15, 2018
@AndreyBelym AndreyBelym modified the milestones: Planned, Sprint #23 Nov 28, 2018
@AndreyBelym AndreyBelym changed the title Role doesn't work after first-time initialization Role doesn't work with hash-based login URLs Dec 12, 2018
@AndreyBelym AndreyBelym modified the milestones: Sprint #23, Planned Dec 12, 2018
@AndreyBelym AndreyBelym added the STATE: Need improvement A minor issue that can't be treated as a bug. label Dec 12, 2018
@AndreyBelym AndreyBelym removed the TYPE: bug The described behavior is considered as wrong (bug). label Dec 12, 2018
@AndreyBelym AndreyBelym changed the title Role doesn't work with hash-based login URLs Role doesn't work when page navigation doesn't trigger page reloading Dec 26, 2018
@miherlosev miherlosev modified the milestones: Planned, Sprint #29 Mar 13, 2019
@miherlosev miherlosev self-assigned this Mar 15, 2019
@AndreyBelym AndreyBelym modified the milestones: Sprint #29, Sprint #30 Mar 18, 2019
@AndreyBelym AndreyBelym modified the milestones: Sprint #30, Sprint #31 Apr 2, 2019
@AndreyBelym AndreyBelym modified the milestones: Sprint #31, Sprint #32 Apr 17, 2019
@lock
Copy link

lock bot commented May 9, 2019

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.

@lock lock bot added the STATE: Auto-locked An issue has been automatically locked by the Lock bot. label May 9, 2019
@lock lock bot locked as resolved and limited conversation to collaborators May 9, 2019
kirovboris pushed a commit to kirovboris/testcafe-phoenix that referenced this issue Dec 18, 2019
cameronjfergus added a commit to cameronjfergus/php-sf-flex-webpack-encore-vuejs that referenced this issue Mar 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
AREA: client STATE: Auto-locked An issue has been automatically locked by the Lock bot. STATE: Need improvement A minor issue that can't be treated as a bug. SYSTEM: automations
Projects
None yet
Development

No branches or pull requests

5 participants