Testcafe LocalStorage的问题

问题描述 投票:3回答:1

大家!我是TestCafe的新手,我需要一些我想要实现的帮助。

我有一个React网站,我在其中放置了Facebook登录。通常,当您进入页面并单击Login with facebook时,将打开一个弹出窗口并正常输入您的凭据。之后,您将被重定向到页面,并且令牌将保存在localStorage变量中,以便稍后进行咨询。

但是,当我运行测试登录过程时,Testcafe而不是打开弹出窗口,在同一页面上打开Facebook表单,永远不会重定向到页面。

此外,我尝试使用dummy token(以及ClientFunction)在localstorage上设置一些Roles,我的网站永远无法达到该令牌,因为testcafe似乎将此变量放在名为hammerhead的键上

所以,我的问题是,如何在测试中或手动输入此令牌,以便我的网站可以读取它并使用它来制作一些功能?

这就是我到目前为止所拥有的。

/* global test, fixture */
import { WelcomePage } from './pages/welcome-page'
import {ClientFunction, Role} from 'testcafe';

const welcomePage = new WelcomePage()

const setLocalStorageItem = ClientFunction((prop, value) => {
  localStorage.setItem(prop, value);
});

const facebookAccUser = Role(`https//mypage.net/`, async t => {
  await setLocalStorageItem('token', 'my-token');
}, { preserveUrl: true });

fixture`Check certain elements`.page(`https//mypage.net/`)
test('Check element is there', async (t) => {
  await t
    .navigateTo(`https//mypage.net/`)
    .wait(4000)
    .useRole(facebookAccUser)
    .expect(cetainElementIfLoggedIn)
    .eql(certainValue)
    .wait(10000)
})

任何帮助将受到高度赞赏

谢谢你的时间。

javascript reactjs facebook local-storage testcafe
1个回答
3
投票

目前,TestCafe不支持多个浏览器窗口。因此,无法通过Facebook弹出窗体登录。但是,有一种解决方法。请参考以下主题https://github.com/DevExpress/testcafe-hammerhead/issues/1428

我的工作测试看起来像这样:

import { Selector, ClientFunction } from 'testcafe';

const patchAuth = ClientFunction(() => {
    window['op' + 'en'] = function (url) {
        var iframe = document.createElement('iframe');

        iframe.style.position = 'fixed';
        iframe.style.left = '200px';
        iframe.style.top = '150px';
        iframe.style.width = '400px';
        iframe.style.height = '300px';
        iframe.style['z-index'] = '99999999';
        iframe.src = url;
        iframe.id = 'auth-iframe';

        document.body.appendChild(iframe);
    };
});

fixture `fixture`
    .page `https://www.soudfa.com/signup`;

test('test', async t => {
    await patchAuth();

    await t
        .click('button.facebook')
        .switchToIframe('#auth-iframe')
        .typeText('#email', '****')
        .typeText('#pass', '****')
        .click('#u_0_0')
        .wait(30e3);
});

请记住,需要在x-frame-options模块中使用testcafe-hammerhead进行操作。

另外,我想提一下Testing in Multiple browser windows是我们的优先任务之一,这是TestCafe Roadmap的一部分

© www.soinside.com 2019 - 2024. All rights reserved.