concept page object in category angular

This is an excerpt from Manning's book Testing Angular Applications.
Before you can create a Protractor test suite, we’ll need to cover the basics. This chapter will demonstrate how Protractor works and how to write your first Protractor test. You’ll create a simple test that interacts with web elements on the screen. We’ll also show you how to organize test code with page objects. When refactoring your test suite, using page objects helps reduce code duplication and creates maintainable code.
Listing 8.14 Contact list page object—e2e/po/contact-list.po.ts
import { browser, by, element, ElementFinder } from 'protractor'; export class ContactListPageObject { plusButton: ElementFinder; constructor() { this.plusButton = element(by.id('add-contact')); #1 } clickPlusButton() { this.plusButton.click(); return new NewContactPageObject(); #2 } navigateTo() { browser.get('/#/'); } } #1 Creates reusable element finder objects #2 When clicking on the plus button, because the page navigates from the contact-list, calls the constructor to return a page object for creating a new contactListing 8.15 New contact page object—e2e/po/new-contact.po.ts
import { browser, by, element, ElementFinder } from 'protractor'; export class NewContactPageObject { inputName: ElementFinder; inputEmail: ElementFinder; inputPhone: ElementFinder; constructor() { this.inputName = element(by.id('contact-name')); this.inputEmail = element(by.id('contact-email')); this.inputPhone = element(by.css('input[type="tel"]')); } setContactInfo(name: string, email: string, phoneNumber: string) { this.inputName.sendKeys(name); #1 if (email) { #2 this.inputEmail.sendKeys(email); } if (phoneNumber) { #2 this.inputPhone.sendKeys(phoneNumber); } } clickCreateButton() { this.element(by.buttonText('Create')).click(); return new ContactListPageObject(); } getName() { #3 return this.inputName.getAttribute('value'); } } getPhone() { return this.inputPhone.getAttribute('value'); } getEmail() { return this.inputEmail.getAttribute('value'); } #1 Finds the element finder object, then sends the keys #2 Sends keys for optional fields #3 Implicitly returns a WebDriver promise for a stringNow that you’ve seen how to make page objects, you can refactor your create contact test as shown in listing 8.16. First, the test creates the
contactListobject and navigates to the contact list page. The nextit()block clicks the plus button and verifies that the current URL is the create contact page. On the create contact page, the test fills out the name input field and email input field. After the fields are filled, the test verifies that the input values match. Finally, the test clicks the Create button and returns to the contact list page.Listing 8.16 Refactor creating a new contact—e2e/page-object.e2e-spec.ts
import { ContactListPageObject } from './po/contact-list.po.ts'; import { NewContactPageObject } from './po/new-contact.po.ts', , Contact } describe('contact list', () => { let contactList: ContactListPageObject; let newContact: NewContactPageObject; beforeAll(() => { contactList = new ContactListPageObject(); }); describe('add a new contact', () => { beforeAll(() => { contactList.navigateTo(); }); it('should click the + button', () => { newContact = contactList.clickPlusButton(); expect(browser.getCurrentUrl()) .toBe(browser.baseUrl + '/#/add'); }); it('should fill out form for a new contact', () => { newContact.setContactInfo( 'Mr. Newton', 'mr.newton@example.com', null); expect(newContact.getName()).toBe('Mr. Newton'); expect(newContact.getEmail()) .toBe('mr.newton@example.com'); expect(newContact.getPhone()).toBe(''); }); it('should click the create button', () => { contactList = newContact.clickCreateButton(); expect(browser.getCurrentUrl()) .toBe(browser.baseUrl + '/#/'); }); }); });Instead of importing in Protractor’s
browser,by, andelement, the test imports the page objects and uses only methods from the page objects for navigation and validation.