我正在研究将TestCafe用作我的测试自动化框架,并且在使用AUT上的Rendr App执行功能方面遇到了一些障碍。 使用Cypress.io,Protractor和Puppeteer等,我可以运行相同的命令...所以我不太确定我在TestCafe哪里出了问题。
基本上,我要执行的是:
window.App.get('currentUser').set('login_state', 'someLoginState');
柏树
cy.window().then((win) => {
win.App.get('currentUser').set('login_state', 'someState');
});
量角器
function changeUserState() {
App.get('currentUser').set('login_state', 'someState');
}
browser.executeScript(changeUserState);
木偶
function changeUserState() {
window.App.get('currentUser').set('login_state', 'someState');
}
await page.evaluate(changeUserState);
对于TestCafe,我尝试使用:
const changeUserState = ClientFunction((desiredState) => {
return App.get('currentUser').set('login_state', desiredState);
});
fixture `User states`
.page(url)
.afterEach( async t => {
await t
logout();
});
test('Change a users log in state', async t => {
await loginForm.loginViaUrl(userEmail, userPassword);
await changeUserState('SomeState');
await checkUserState('SomeState'); // Just an example of what I would do next
}
运行此命令时,会引发ReferenceError: App is not defined
错误。
(我也尝试过使用'window.App.get ...'进行上述选择:TypeError: Cannot read property 'get' of undefined
-在调用ClientFunction之前添加等待不会影响结果)< / em>
更新
根据评论,在访问客户端功能时,不应使用t.eval(...)
选项。
答案 0 :(得分:3)
我认为问题与未定义App变量有关。因此,您应该等到它可用。您可以使用具有固定等待时间的wait操作,也可以使用以下选择器等待元素出现在页面上:
await Selector('element that tells us that App exists')
UPD :检查了您在your Github issue中提供的测试示例后,我们为您找到了解决方案。有必要等到App
变量出现。在changeUserState
函数调用之前添加以下代码:
// first case
const getPageUrl = ClientFunction(() => location.toString());
await t.expect(getPageUrl()).eql('https://www.change.org/', { timeout: 5000 });
// second case
const isAppExists = ClientFunction(() => !!window.App);
await t.expect(isAppExists()).ok({ timeout: 5000 });
来自GitHub comment的UPD:
import { Selector, ClientFunction } from 'testcafe';
fixture`Change User State`.page('www.change.org');
const isAppExists = ClientFunction(() => !!window.App);
const changeUserState = ClientFunction((desiredState) => {
const initialState = App.get('currentUser').get('login_state');
App.get('currentUser').set('login_state', desiredState);
const currentState = App.get('currentUser').get('login_state');
return { initialState, currentState };
});
test
('Start from homepage', async t => {
await t
.maximizeWindow()
.navigateTo('/login_or_join')
.typeText(Selector('#content input[type="email"]'), '*****')
.typeText(Selector('#content input[type="password"]'), '*****')
.click(Selector('#content input[type="submit"]'))
.expect(isAppExists()).ok(); // wait for App
console.log(await changeUserState('guest'));
});
结果:
Running tests in:
- Chrome 74.0.3729 / Windows 10.0.0
Change User State
{ initialState: 'authenticated', currentState: 'guest' }
√ Start from homepage
1 passed (15s)