angular-cn/aio/e2e/app.po.ts
Peter Bacon Darwin 441e01c568 fix(aio): simplify GaService to avoid e2e test failures
The GaService and the E2E specs were unnecessarily complicated and had
arbitrary async timeouts to ensure that the interplay between the GA
library code and the rest of the app worked correctly. This resulted
in potential flaky tests if the timeouts were not adequate; this was
experienced when Travis upgraded to Chrome 62.

The new approach is to block loading of the Analytics library altogether
if there is a `__e2e__` flag set in the `SessionStorage` of the browser.
It doesn't appear to be enough just to set the flag directly on the
window. I think this is because the window gets cleaned when navigation
occurs (but I am not certain).

The downside of this is that we had to add a small piece of extra logic
to the GA snippet in index.html; and we also had to switch from using
`<script async ...>` to a programmatic approach to loading the GA library
which prevents the browser from eagerly fetching the library. This may
be mitigated by adding it to the HTTP/2 push configuration of the Firebase
hosting.

Re-enables the test that was disabled in https://github.com/angular/angular/pull/19784

Closes #19785
2017-10-20 09:40:52 -07:00

67 lines
2.4 KiB
TypeScript

import { browser, element, by, promise, ElementFinder, ExpectedConditions } from 'protractor';
const githubRegex = /https:\/\/github.com\/angular\/angular\//;
export class SitePage {
links = element.all(by.css('md-toolbar a'));
docsMenuLink = element(by.cssContainingText('aio-top-menu a', 'Docs'));
docViewer = element(by.css('aio-doc-viewer'));
codeExample = element.all(by.css('aio-doc-viewer pre > code'));
ghLink = this.docViewer
.all(by.css('a'))
.filter((a: ElementFinder) => a.getAttribute('href').then(href => githubRegex.test(href)))
.first();
static setWindowWidth(newWidth: number) {
const win = browser.driver.manage().window();
return win.getSize().then(oldSize => win.setSize(newWidth, oldSize.height));
}
getNavItem(pattern: RegExp) {
return element.all(by.css('aio-nav-item .vertical-menu-item'))
.filter(element => element.getText().then(text => pattern.test(text)))
.first();
}
getLink(path) { return element(by.css(`a[href="${path}"]`)); }
ga() { return browser.executeScript('return window["ga"].q') as promise.Promise<any[][]>; }
locationPath() { return browser.executeScript('return document.location.pathname') as promise.Promise<string>; }
navigateTo(pageUrl = '') {
return browser.get('/' + pageUrl)
// We need to tell the index.html not to load the real analytics library
// See the GA snippet in index.html
.then(() => browser.driver.executeScript('sessionStorage.setItem("__e2e__", true);'));
}
getDocViewerText() {
return this.docViewer.getText();
}
getInnerHtml(element) {
// `getInnerHtml` was removed from webDriver and this is the workaround.
// See https://github.com/angular/protractor/blob/master/CHANGELOG.md#breaking-changes
return browser.executeScript('return arguments[0].innerHTML;', element);
}
getScrollTop() {
return browser.executeScript('return window.pageYOffset');
}
scrollToBottom() {
return browser.executeScript('window.scrollTo(0, document.body.scrollHeight)');
}
enterSearch(query: string) {
const input = element(by.css('.search-container input[type=search]'));
input.clear();
input.sendKeys(query);
}
getSearchResults() {
const results = element.all(by.css('.search-results li'));
browser.wait(ExpectedConditions.presenceOf(results.first()), 8000);
return results;
}
}