In my opinion you should re-think what you want to test and how. You can test if something has being triggered from inside the component (unit tests) like if I call this function, then this property change the value.
export class Page {
listOfSomething: string[] = [];
addStuff: (item: string) => this.listOfSomething.push(item);
}
Here you can test that the listOfSomething
changes over time.
To know if a button do that from the button
in your template then you might have this situation
<p *ngFor="let item of listOfSomething">{{ item }}<p>
<button (click)="addStuff('stuff')">add stuff</button>
In this case you want to see that the number of elements on the screen changes if you click the button. Basically you are checking addStuff
and listOfSomething
indirectly, but still checking them.
--
Over all you need to split your tests into unit tests and e2e tests.
Jasmine is more for unit tests. you might find a way but it's not worth the time.
Below you can see the different approach you need to have for a login page (e2e).
import { browser, by, element } from 'protractor';
export const testData = {
userName: 1231232,
password: 'pass1234'
};
export class LoginPage {
//////////////////////////////////////////////////////////////////////////////////////////
// navigate //////
navigateTo() {
return browser.get('/login');
}
quickLogin() {
this.navigateTo();
this.setMobileNumber(testData.userName);
this.setPassword(testData.password);
this.doLogin();
}
//////////////////////////////////////////////////////////////////////////////////////////
// selectors /////
getLoginButton() {
return element(by.buttonText('Log in'));
}
//////////////////////////////////////////////////////////////////////////////////////////
// getText ///////
getSelectedMobileNumberPrefix() {
return element(by.deepCss('section form .mat-select .value-output')).getText();
}
getErrorMessage() {
return element(by.deepCss('snack-bar-container simple-snack-bar span')).getText();
}
//////////////////////////////////////////////////////////////////////////////////////////
// sendKeys //////
setMobileNumber(phoneNumber: number) {
return element(by.css('[formControlName=phoneNumber]')).sendKeys(phoneNumber);
}
setPassword(password: string) {
return element(by.css('[formControlName=password]')).sendKeys(password);
}
//////////////////////////////////////////////////////////////////////////////////////////
// click /////////
doLogin() {
return this.getLoginButton().click();
}
}
import { browser, protractor } from 'protractor';
import { DashboardPage } from '../dashboard/dashboard.po';
import { LoginPage } from './login.po';
const password = {
correct: 'pass1234',
wrong: 'xxx',
};
const testData = {
maleUser: 1231232,
femaleUser: 1231231,
};
describe('Login Journey', () => {
const EC = protractor.ExpectedConditions;
let dashboardPage: DashboardPage;
let loginPage: LoginPage;
beforeEach(() => {
dashboardPage = new DashboardPage();
loginPage = new LoginPage();
});
afterEach(() => {
browser.executeScript('window.localStorage.clear();');
});
it('Checking init page', () => {
loginPage.navigateTo();
expect(loginPage.getSelectedMobileNumberPrefix()).toContain('+60');
expect(loginPage.getLoginButton().isEnabled()).toBeFalsy();
loginPage.setMobileNumber(testData.maleUser);
loginPage.setPassword(password.correct);
expect(loginPage.getLoginButton().isEnabled()).toBeTruthy();
expect(loginPage.getSelectedMobileNumberPrefix()).toContain('+60');
});
it('I should be able to login', () => {
loginPage.navigateTo();
loginPage.setMobileNumber(testData.maleUser);
loginPage.setPassword(password.correct);
loginPage.getLoginButton().click();
browser.waitForAngular();
expect(dashboardPage.getTitle()).toContain('Dashboard');
});
it('I should NOT be able to login with incorrect credentials', () => {
loginPage.navigateTo();
loginPage.setMobileNumber(testData.maleUser);
loginPage.setPassword(password.wrong);
loginPage.getLoginButton().click();
browser.waitForAngular();
expect(loginPage.getErrorMessage()).toContain('The email address or the password you inputted is incorrect.');
});
});
ngIf
s andngFor
s, does it? – Petrology