Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
chore(test): move interaction test off of the control flow (#5019)
Browse files Browse the repository at this point in the history
  • Loading branch information
cnishina authored Nov 9, 2018
1 parent 7b77acf commit 7172e48
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 81 deletions.
2 changes: 1 addition & 1 deletion scripts/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var passingTests = [
'node built/cli.js spec/plugins/browserGetSyncedConf.js',
'node built/cli.js spec/plugins/browserGetUnsyncedConf.js',
'node built/cli.js spec/plugins/waitForAngularConf.js',
// 'node built/cli.js spec/interactionConf.js',
'node built/cli.js spec/interactionConf.js',
// 'node built/cli.js spec/directConnectConf.js',
'node built/cli.js spec/restartBrowserBetweenTestsConf.js',
// 'node built/cli.js spec/driverProviderLocalConf.js',
Expand Down
163 changes: 83 additions & 80 deletions spec/interaction/interaction_spec.js
Original file line number Diff line number Diff line change
@@ -1,134 +1,137 @@
describe('Browser', function() {
class Person {

var newBrowser;
constructor(name, browser) {
this.name = name;
this.browser = browser;
this.$ = browser.$;
this.element = browser.element;
}

afterEach(function(done) {
async openApp() {
await this.browser.get('index.html#/interaction');
};

async login() {
await this.element(by.model('userInput')).sendKeys(this.name);
await this.$('#sendUser').click();
};

async clearMessages() {
await this.$('#clearMessages').click();
};

async sendMessage(msg) {
await this.element(by.model('message')).sendKeys(msg);
await this.$('#sendMessage').click();
};

getMessages() {
return this.element.all(by.repeater('msg in messages track by $index'));
};
};

describe('Browser', () => {

let newBrowser;

afterEach(async() => {
// Calling quit will remove the browser.
// You can choose to not quit the browser, and protractor will quit all of
// them for you when it exits (i.e. if you need a static number of browsers
// throughout all of your tests). However, I'm forking browsers in my tests
// and don't want to pile up my browser count.
if (newBrowser) {
newBrowser.quit().then(() => {
done();
});
} else {
done();
await newBrowser.quit();
}
});

it('should be able to fork', function() {
browser.get('index.html');
newBrowser = browser.forkNewDriverInstance();
it('should be able to fork', async() => {
await browser.get('index.html');
newBrowser = await browser.forkNewDriverInstance().ready;
expect(newBrowser).not.toEqual(browser);
expect(newBrowser.driver).not.toEqual(browser.driver);
expect(newBrowser.driver.getCurrentUrl()).toEqual('data:,');
expect(await newBrowser.driver.getCurrentUrl()).toEqual('data:,');
});

it('should be able to navigate to same url on fork', function() {
browser.get('index.html');
newBrowser = browser.forkNewDriverInstance(true);
expect(newBrowser.driver.getCurrentUrl()).
toMatch('index.html#/form');
it('should be able to navigate to same url on fork', async() => {
await browser.get('index.html');
newBrowser = await browser.forkNewDriverInstance(true).ready;
expect(await newBrowser.driver.getCurrentUrl()).toMatch('index.html#/form');
});

it('should be able to copy mock modules on fork', function() {
var mockModule = function() {
var newModule = angular.module('mockModule', []);
it('should be able to copy mock modules on fork', async() => {
const mockModule = () => {
const newModule = angular.module('mockModule', []);
newModule.value('version', '2');
};

browser.addMockModule('mockModule', mockModule);
browser.get('index.html');
await browser.get('index.html');

newBrowser = browser.forkNewDriverInstance(true, true);
expect(newBrowser.element(by.css('[app-version]')).getText()).toEqual('2');
newBrowser = await browser.forkNewDriverInstance(true, true).ready;
expect(await newBrowser.element(by.css('[app-version]')).getText())
.toEqual('2');
});


describe('Multiple browsers', function() {
describe('Multiple browsers', () => {

var Person = function(name, browser) {
var $ = browser.$;
var element = browser.element;

this.openApp = function() {
browser.get('index.html#/interaction');
};

this.login = function() {
element(by.model('userInput')).sendKeys(name);
$('#sendUser').click();
};

this.clearMessages = function() {
$('#clearMessages').click();
};

this.sendMessage = function(msg) {
element(by.model('message')).sendKeys(msg);
$('#sendMessage').click();
};

this.getMessages = function() {
return element.all(by.repeater('msg in messages track by $index'));
};
};


var p0, p1;
let p0, p1;

beforeEach(function() {
beforeEach(async() => {
// default browser.
p0 = new Person('p0', browser);
p0.openApp();
p0.login();
p0.clearMessages();
await p0.openApp();
await p0.login();
await p0.clearMessages();

// Any additional browsers can be instantiated via browser.forkNewDriverInstance().
newBrowser = browser.forkNewDriverInstance(true);
newBrowser = await browser.forkNewDriverInstance(true).ready;
p1 = new Person('p1', newBrowser);
p1.openApp();
p1.login();
await p1.openApp();
await p1.login();
});

it('should be able to interact', function() {
expect(p0.getMessages().count()).toEqual(0);
it('should be able to interact', async() => {
expect(await p0.getMessages().count()).toEqual(0);

p0.sendMessage('p0');
browser.sleep(100); // The app polls every 100ms for updates.
expect(p0.getMessages().count()).toEqual(1);
expect(p1.getMessages().count()).toEqual(1);
await p0.sendMessage('p0');
await browser.sleep(100); // The app polls every 100ms for updates.
expect(await p0.getMessages().count()).toEqual(1);
expect(await p1.getMessages().count()).toEqual(1);

p1.sendMessage('p1');
browser.sleep(100); // The app polls every 100ms for updates.
expect(p0.getMessages().count()).toEqual(2);
expect(p1.getMessages().count()).toEqual(2);
await p1.sendMessage('p1');
await browser.sleep(100); // The app polls every 100ms for updates.
expect(await p0.getMessages().count()).toEqual(2);
expect(await p1.getMessages().count()).toEqual(2);
});

it('should perform actions in sync', function() {
var ACTIONS = 10;
expect(p0.getMessages().count()).toEqual(0);
it('should perform actions in sync', async() => {
const ACTIONS = 10;
expect(await p0.getMessages().count()).toEqual(0);

var expectedMessages = [];
var i;
let expectedMessages = [];
let i;
for (i = 0; i < ACTIONS; ++i) {
p0.sendMessage(i);
await p0.sendMessage(i);
expectedMessages.push('p0: ' + i);
}
for (i = 0; i < ACTIONS; ++i) {
p1.sendMessage(i);
await p1.sendMessage(i);
expectedMessages.push('p1: ' + i);
}
for (i = 0; i < ACTIONS; ++i) {
p0.sendMessage(i);
p1.sendMessage(i);
await p0.sendMessage(i);
await p1.sendMessage(i);
expectedMessages.push('p0: ' + i);
expectedMessages.push('p1: ' + i);
}

browser.sleep(100); // The app polls every 100ms for updates.
expect(p0.getMessages().getText()).toEqual(expectedMessages);
expect(p1.getMessages().getText()).toEqual(expectedMessages);
await browser.sleep(100); // The app polls every 100ms for updates.
expect(await p0.getMessages().getText()).toEqual(expectedMessages);
expect(await p1.getMessages().getText()).toEqual(expectedMessages);
});
});
});
1 change: 1 addition & 0 deletions spec/interactionConf.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var env = require('./environment.js');
// Test having two browsers interacting with each other.
exports.config = {
seleniumAddress: env.seleniumAddress,
SELENIUM_PROMISE_MANAGER: false,

framework: 'jasmine',

Expand Down

0 comments on commit 7172e48

Please sign in to comment.