2020-04-08 08:27:46 -04:00
|
|
|
import { browser, by, element, ElementArrayFinder, ElementFinder, logging } from 'protractor';
|
|
|
|
|
|
|
|
class Hero {
|
|
|
|
id: number;
|
|
|
|
name: string;
|
|
|
|
|
|
|
|
// Factory methods
|
|
|
|
|
|
|
|
// Hero from string formatted as '<id> <name>'.
|
|
|
|
static fromString(s: string): Hero {
|
|
|
|
return {
|
|
|
|
id: +s.substr(0, s.indexOf(' ')),
|
|
|
|
name: s.substr(s.indexOf(' ') + 1),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hero from hero list <li> element.
|
|
|
|
static async fromLi(li: ElementFinder): Promise<Hero> {
|
|
|
|
const stringsFromA = await li.all(by.css('a')).getText();
|
|
|
|
const strings = stringsFromA[0].split(' ');
|
|
|
|
return { id: +strings[0], name: strings[1] };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hero id and name from the given detail element.
|
|
|
|
static async fromDetail(detail: ElementFinder): Promise<Hero> {
|
|
|
|
// Get hero id from the first <div>
|
|
|
|
const id = await detail.all(by.css('div')).first().getText();
|
|
|
|
// Get name from the h2
|
|
|
|
const name = await detail.element(by.css('h2')).getText();
|
|
|
|
return {
|
|
|
|
id: +id.substr(id.indexOf(' ') + 1),
|
|
|
|
name: name.substr(0, name.lastIndexOf(' '))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('Universal', () => {
|
|
|
|
const expectedH1 = 'Tour of Heroes';
|
|
|
|
const expectedTitle = `${expectedH1}`;
|
|
|
|
const targetHero = { id: 15, name: 'Magneta' };
|
|
|
|
const targetHeroDashboardIndex = 3;
|
|
|
|
const nameSuffix = 'X';
|
|
|
|
const newHeroName = targetHero.name + nameSuffix;
|
|
|
|
|
|
|
|
afterEach(async () => {
|
|
|
|
// Assert that there are no errors emitted from the browser
|
|
|
|
const logs = await browser.manage().logs().get(logging.Type.BROWSER);
|
|
|
|
const severeLogs = logs.filter(entry => entry.level === logging.Level.SEVERE);
|
|
|
|
expect(severeLogs).toEqual([]);
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Initial page', () => {
|
|
|
|
beforeAll(() => browser.get(''));
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it(`has title '${expectedTitle}'`, async () => {
|
|
|
|
expect(await browser.getTitle()).toEqual(expectedTitle);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it(`has h1 '${expectedH1}'`, async () => {
|
|
|
|
await expectHeading(1, expectedH1);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const expectedViewNames = ['Dashboard', 'Heroes'];
|
2020-11-23 13:17:10 -05:00
|
|
|
it(`has views ${expectedViewNames}`, async () => {
|
|
|
|
const viewNames = await getPageElts().navElts.map(el => el.getText());
|
2020-04-08 08:27:46 -04:00
|
|
|
expect(viewNames).toEqual(expectedViewNames);
|
|
|
|
});
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it('has dashboard as the active view', async () => {
|
2020-04-08 08:27:46 -04:00
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.appDashboard.isPresent()).toBeTruthy();
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Dashboard tests', () => {
|
|
|
|
beforeAll(() => browser.get(''));
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it('has top heroes', async () => {
|
2020-04-08 08:27:46 -04:00
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.topHeroes.count()).toEqual(4);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`selects and routes to ${targetHero.name} details`, dashboardSelectTargetHero);
|
|
|
|
|
|
|
|
it(`updates hero name (${newHeroName}) in details view`, updateHeroNameInDetailView);
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it(`cancels and shows ${targetHero.name} in Dashboard`, async () => {
|
|
|
|
await element(by.buttonText('go back')).click();
|
|
|
|
await browser.waitForAngular(); // seems necessary to gets tests to pass for toh-pt6
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const targetHeroElt = getPageElts().topHeroes.get(targetHeroDashboardIndex);
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await targetHeroElt.getText()).toEqual(targetHero.name);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`selects and routes to ${targetHero.name} details`, dashboardSelectTargetHero);
|
|
|
|
|
|
|
|
it(`updates hero name (${newHeroName}) in details view`, updateHeroNameInDetailView);
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it(`saves and shows ${newHeroName} in Dashboard`, async () => {
|
|
|
|
await element(by.buttonText('save')).click();
|
|
|
|
await browser.waitForAngular(); // seems necessary to gets tests to pass for toh-pt6
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const targetHeroElt = getPageElts().topHeroes.get(targetHeroDashboardIndex);
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await targetHeroElt.getText()).toEqual(newHeroName);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Heroes tests', () => {
|
|
|
|
beforeAll(() => browser.get(''));
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it('can switch to Heroes view', async () => {
|
|
|
|
await getPageElts().appHeroesHref.click();
|
2020-04-08 08:27:46 -04:00
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.appHeroes.isPresent()).toBeTruthy();
|
|
|
|
expect(await page.allHeroes.count()).toEqual(10, 'number of heroes');
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('can route to hero details', async () => {
|
2020-11-23 13:17:10 -05:00
|
|
|
await getHeroLiEltById(targetHero.id).click();
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.heroDetail.isPresent()).toBeTruthy('shows hero detail');
|
2020-04-08 08:27:46 -04:00
|
|
|
const hero = await Hero.fromDetail(page.heroDetail);
|
|
|
|
expect(hero.id).toEqual(targetHero.id);
|
|
|
|
expect(hero.name).toEqual(targetHero.name.toUpperCase());
|
|
|
|
});
|
|
|
|
|
|
|
|
it(`updates hero name (${newHeroName}) in details view`, updateHeroNameInDetailView);
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
it(`shows ${newHeroName} in Heroes list`, async () => {
|
|
|
|
await element(by.buttonText('save')).click();
|
|
|
|
await browser.waitForAngular();
|
2020-04-08 08:27:46 -04:00
|
|
|
const expectedText = `${targetHero.id} ${newHeroName}`;
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await getHeroAEltById(targetHero.id).getText()).toEqual(expectedText);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`deletes ${newHeroName} from Heroes list`, async () => {
|
|
|
|
const heroesBefore = await toHeroArray(getPageElts().allHeroes);
|
|
|
|
const li = getHeroLiEltById(targetHero.id);
|
2020-11-23 13:17:10 -05:00
|
|
|
await li.element(by.buttonText('x')).click();
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.appHeroes.isPresent()).toBeTruthy();
|
|
|
|
expect(await page.allHeroes.count()).toEqual(9, 'number of heroes');
|
2020-04-08 08:27:46 -04:00
|
|
|
const heroesAfter = await toHeroArray(page.allHeroes);
|
|
|
|
// console.log(await Hero.fromLi(page.allHeroes[0]));
|
|
|
|
const expectedHeroes = heroesBefore.filter(h => h.name !== newHeroName);
|
|
|
|
expect(heroesAfter).toEqual(expectedHeroes);
|
2020-11-23 13:17:10 -05:00
|
|
|
// expect(await page.selectedHeroSubview.isPresent()).toBeFalsy();
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`adds back ${targetHero.name}`, async () => {
|
|
|
|
const updatedHeroName = 'Alice';
|
|
|
|
const heroesBefore = await toHeroArray(getPageElts().allHeroes);
|
|
|
|
const numHeroes = heroesBefore.length;
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
await element(by.css('input')).sendKeys(updatedHeroName);
|
|
|
|
await element(by.buttonText('add')).click();
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const page = getPageElts();
|
|
|
|
const heroesAfter = await toHeroArray(page.allHeroes);
|
|
|
|
expect(heroesAfter.length).toEqual(numHeroes + 1, 'number of heroes');
|
|
|
|
|
|
|
|
expect(heroesAfter.slice(0, numHeroes)).toEqual(heroesBefore, 'Old heroes are still there');
|
|
|
|
|
|
|
|
const maxId = heroesBefore[heroesBefore.length - 1].id;
|
|
|
|
expect(heroesAfter[numHeroes]).toEqual({id: maxId + 1, name: updatedHeroName});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays correctly styled buttons', async () => {
|
2020-11-23 13:17:10 -05:00
|
|
|
const buttons = await element.all(by.buttonText('x'));
|
|
|
|
|
|
|
|
for (const button of buttons) {
|
|
|
|
// Inherited styles from styles.css
|
2020-12-23 14:25:21 -05:00
|
|
|
expect(await button.getCssValue('font-family')).toBe('Arial, sans-serif');
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await button.getCssValue('border')).toContain('none');
|
|
|
|
expect(await button.getCssValue('padding')).toBe('5px 10px');
|
|
|
|
expect(await button.getCssValue('border-radius')).toBe('4px');
|
|
|
|
// Styles defined in heroes.component.css
|
|
|
|
expect(await button.getCssValue('left')).toBe('194px');
|
|
|
|
expect(await button.getCssValue('top')).toBe('-32px');
|
|
|
|
}
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const addButton = element(by.buttonText('add'));
|
|
|
|
// Inherited styles from styles.css
|
2020-12-23 14:25:21 -05:00
|
|
|
expect(await addButton.getCssValue('font-family')).toBe('Arial, sans-serif');
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await addButton.getCssValue('border')).toContain('none');
|
|
|
|
expect(await addButton.getCssValue('padding')).toBe('5px 10px');
|
|
|
|
expect(await addButton.getCssValue('border-radius')).toBe('4px');
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('Progressive hero search', () => {
|
|
|
|
beforeAll(() => browser.get(''));
|
|
|
|
|
|
|
|
it(`searches for 'Ma'`, async () => {
|
2020-11-23 13:17:10 -05:00
|
|
|
await getPageElts().searchBox.sendKeys('Ma');
|
|
|
|
await browser.sleep(1000);
|
2020-04-08 08:27:46 -04:00
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await getPageElts().searchResults.count()).toBe(4);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`continues search with 'g'`, async () => {
|
2020-11-23 13:17:10 -05:00
|
|
|
await getPageElts().searchBox.sendKeys('g');
|
|
|
|
await browser.sleep(1000);
|
|
|
|
expect(await getPageElts().searchResults.count()).toBe(2);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`continues search with 'e' and gets ${targetHero.name}`, async () => {
|
2020-11-23 13:17:10 -05:00
|
|
|
await getPageElts().searchBox.sendKeys('n');
|
|
|
|
await browser.sleep(1000);
|
2020-04-08 08:27:46 -04:00
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.searchResults.count()).toBe(1);
|
2020-04-08 08:27:46 -04:00
|
|
|
const hero = page.searchResults.get(0);
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await hero.getText()).toEqual(targetHero.name);
|
2020-04-08 08:27:46 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it(`navigates to ${targetHero.name} details view`, async () => {
|
|
|
|
const hero = getPageElts().searchResults.get(0);
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await hero.getText()).toEqual(targetHero.name);
|
|
|
|
await hero.click();
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.heroDetail.isPresent()).toBeTruthy('shows hero detail');
|
2020-04-08 08:27:46 -04:00
|
|
|
const hero2 = await Hero.fromDetail(page.heroDetail);
|
|
|
|
expect(hero2.id).toEqual(targetHero.id);
|
|
|
|
expect(hero2.name).toEqual(targetHero.name.toUpperCase());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// Helpers
|
2020-11-23 13:17:10 -05:00
|
|
|
async function addToHeroName(text: string): Promise<void> {
|
|
|
|
await element(by.css('input')).sendKeys(text);
|
2020-04-08 08:27:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
async function dashboardSelectTargetHero(): Promise<void> {
|
|
|
|
const targetHeroElt = getPageElts().topHeroes.get(targetHeroDashboardIndex);
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await targetHeroElt.getText()).toEqual(targetHero.name);
|
|
|
|
await targetHeroElt.click();
|
|
|
|
await browser.waitForAngular(); // seems necessary to gets tests to pass for toh-pt6
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const page = getPageElts();
|
2020-11-23 13:17:10 -05:00
|
|
|
expect(await page.heroDetail.isPresent()).toBeTruthy('shows hero detail');
|
2020-04-08 08:27:46 -04:00
|
|
|
const hero = await Hero.fromDetail(page.heroDetail);
|
|
|
|
expect(hero.id).toEqual(targetHero.id);
|
|
|
|
expect(hero.name).toEqual(targetHero.name.toUpperCase());
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:17:10 -05:00
|
|
|
async function expectHeading(hLevel: number, expectedText: string): Promise<void> {
|
2020-04-08 08:27:46 -04:00
|
|
|
const hTag = `h${hLevel}`;
|
2020-11-23 13:17:10 -05:00
|
|
|
const hText = await element(by.css(hTag)).getText();
|
2020-04-08 08:27:46 -04:00
|
|
|
expect(hText).toEqual(expectedText, hTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHeroAEltById(id: number): ElementFinder {
|
|
|
|
const spanForId = element(by.cssContainingText('li span.badge', id.toString()));
|
|
|
|
return spanForId.element(by.xpath('..'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getHeroLiEltById(id: number): ElementFinder {
|
|
|
|
const spanForId = element(by.cssContainingText('li span.badge', id.toString()));
|
|
|
|
return spanForId.element(by.xpath('../..'));
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPageElts() {
|
|
|
|
const navElts = element.all(by.css('app-root nav a'));
|
|
|
|
|
|
|
|
return {
|
|
|
|
navElts,
|
|
|
|
|
|
|
|
appDashboardHref: navElts.get(0),
|
|
|
|
appDashboard: element(by.css('app-root app-dashboard')),
|
|
|
|
topHeroes: element.all(by.css('app-root app-dashboard > div h4')),
|
|
|
|
|
|
|
|
appHeroesHref: navElts.get(1),
|
|
|
|
appHeroes: element(by.css('app-root app-heroes')),
|
|
|
|
allHeroes: element.all(by.css('app-root app-heroes li')),
|
|
|
|
selectedHeroSubview: element(by.css('app-root app-heroes > div:last-child')),
|
|
|
|
|
|
|
|
heroDetail: element(by.css('app-root app-hero-detail > div')),
|
|
|
|
|
|
|
|
searchBox: element(by.css('#search-box')),
|
|
|
|
searchResults: element.all(by.css('.search-result li'))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function toHeroArray(allHeroes: ElementArrayFinder): Promise<Hero[]> {
|
|
|
|
return await allHeroes.map(Hero.fromLi);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function updateHeroNameInDetailView(): Promise<void> {
|
|
|
|
// Assumes that the current view is the hero details view.
|
2020-11-23 13:17:10 -05:00
|
|
|
await addToHeroName(nameSuffix);
|
2020-04-08 08:27:46 -04:00
|
|
|
|
|
|
|
const page = getPageElts();
|
|
|
|
const hero = await Hero.fromDetail(page.heroDetail);
|
|
|
|
expect(hero.id).toEqual(targetHero.id);
|
|
|
|
expect(hero.name).toEqual(newHeroName.toUpperCase());
|
|
|
|
}
|
|
|
|
});
|