I'm using TestCafe and i can't implement this behavior:
- Log-in to my website using
Rolemechanism - Check for conditions:
- Url change OR
- An error message appears
I know how to get current url using ClientFunction and check for element visibility with Selector but i can't figure out how to mix the two.
Is it possible ?
Base on NoriSte's answer (thanks!), a working sample adapted from my use case. I did not find it neither elegant nor extendable (what to do if you want ORing more conditions).
AuthPage.js
import { Selector, t, Role, ClientFunction } from "testcafe"
const url = 'http://localhost:8081'
const loginUrl = `${url}/login`;
const logoutUrl = `${url}/logout`;
const loginInput = Selector('[data-qa="login input"]');
const passwordInput = Selector('[data-qa="password input"]');
const submitButton = Selector('[data-qa="submit button"]')
const loginError = Selector('[data-qa="login error"]');
const getLocation = ClientFunction(() => document.location.href.toString());
const login = (username, password) => async tt =>
await tt
.typeText(loginInput, username)
.pressKey('tab')
.typeText(passwordInput, password)
.click(submitButton);
export default class AuthPage {
constructor () {
this.roles = {
// 'almighty god': Role(loginUrl, login('filou', 'filoucfou')),
// 'bad mutafuka': Role(loginUrl, login('badbadbad', 'password'))
// Can't use Role, don't know why ?!
'almighty god': login('gooood', 'password'),
'bad mutafuka': login('badbadbad', 'password')
}
}
async loginWith (roleName) {
const oldUrl = await getLocation();
// await t
// .useRole(this.roles[roleName]);
// Does weird thing, not what i would expect
await this.roles[roleName](t)
if (await loginError.with({ timeout: 5000, visibilityCheck: true }).exists) {
console.log('login error:', oldUrl)
return {url: oldUrl, login: false};
}
await t
.wait(10000);
const newUrl = await getLocation()
return {url: newUrl, login: oldUrl !== newUrl};
}
}
test.js
import LoginPage from 'AuthPage'
fixture `sinoc-fixture`
.page `http://localhost:8081`;
const loginPage = new LoginPage()
test('Login with wrong credentials should fail', async (t) => {
const result = await loginPage.loginWith('bad mutafuka')
await t
.expect(result.login === false).ok()
});
test('Login with good credentials should succeed', async (t) => {
const result = await loginPage.loginWith('almighty god')
await t
.expect(result.login === true)
.ok()
.expect(result.url)
.contains('/dashboard');
});