concept ExpectedConditions in category angular

This is an excerpt from Manning's book Testing Angular Applications.
In listing 8.9, the test enters a valid name and an invalid email address, baduser.com. After the test clicks the Create button, it should check if the modal alert window is visible and dismiss the message. After dismissing the modal alert message, it’s a good idea to verify that the modal alert did disappear. To test this, you can use another Protractor API called
ExpectedConditions. TheExpectedConditionsAPI combined with thebrowser.waitmethod allows the test to wait for some condition to occur on the web application within a set period of time. In this example, you’re waiting for the web element to not be present within five seconds. Finally, because the test should fail to create a new contact, the test also should check to see if the Contacts app route URL is still on the/#/addroute.Listing 8.9 Test that doesn’t create a new contact—e2e/invalid-contact.e2e-spec.ts
import { browser, by, element, ExpectedConditions as EC } from 'protractor'; describe('adding a new contact with an invalid email', () => { beforeEach(() => { browser.get('/#/add'); element(by.id('contact-name')).sendKeys('Bad Email'); }); it('shouldn’t create a new contact with baduser.com', () => { let email = element(by.id('contact-email')); email.sendKeys('baduser.com'); element(by.buttonText('Create')).click(); let invalidEmailModal = element(by.tagName( 'app-invalid-email-modal')); expect(invalidEmailModal.isPresent()).toBe(true); #1 let modalButton = invalidEmailModal.element( by.tagName('button')); #2 modalButton.click(); browser.wait(EC.not( EC.presenceOf(invalidEmailModal)), 5000); #3 expect(invalidEmailModal.isPresent()).toBe(false); #4 expect(browser.getCurrentUrl()).toEqual( browser.baseUrl + '/#/add'); }); }); #1 Checks to see that the modal is present #2 Finds the button from the modal web element #3 Waits five seconds for the invalid email modal to disappear #4 Checks to see that the modal is no longer present
Knowing how and when to enable and disable waiting for Angular, even on pages that are part of an Angular app, is an important part of writing tests using Protractor. But turning off waiting for Angular can have side effects. For example, your tests won’t know when Angular is done updating the page, so you might have to use other synchronization methods. One such method is
ExpectedConditions.
Listing 9.2 Using
ExpectedConditionsinstead ofwaitForAngularit('should be able to login', () => { let EC = browser.ExpectedConditions; browser.waitForAngularEnabled(false); browser.get('/assets/login.html'); element(by.css('input.user')).sendKeys('username'); element(by.css('input.password')).sendKeys('password'); element(by.id('login')).click(); const list = element(by.css('app-contact-list')); const listReady = EC.not( EC.textToBePresentInElement(list, 'Loading contacts')); #1 browser.wait(listReady, 5000, 'Wait for list to load'); #2 expect(list.getText()).toContain('Jeff Pipe'); }); #1 Builds the expected condition #2 Waits up to five seconds for 'Loading contacts' to go away