Skip to content

Instantly share code, notes, and snippets.

@bingomanatee
Created February 14, 2013 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bingomanatee/4954746 to your computer and use it in GitHub Desktop.
Save bingomanatee/4954746 to your computer and use it in GitHub Desktop.
When a test fails, it is impossible to end the selenium connection.
var webdriverjs = require("webdriverjs");
var chai = require('chai');
var _ = require('underscore');
if (_.isFunction(chai.should)) {
chai.should();
}
var util = require('util');
var moment = require('moment');
var DATE_FORMAT = 'YYYY-MM-DD';
var todayDateString = moment().format(DATE_FORMAT);
console.log('today: %s', todayDateString);
var _DEBUG = false;
var startDate = '2013_02_01'; // formatted for timeMachine URL
var barDate = '2013_01_06'; // the date to check bar graph at - formatted for URL
/* ************* SELECTORS ****************** */
var barGraphJanSixValueSelector = '#cashFlow #cashFlowBarSVG .bars dd.d' + barDate;
var allAccountsCheckbox = '#cashFlow #cashFlowAccountSelector input.all';
var barGraphJanSixExpenseAmount = '$950.00';
var barGraphJanSixExpenseAmountWithoutFoo = '$350.00';
var barGraphJanSixIncomeAmountWithoutFoo = '$0.00';
var fooAccountCheckbox = '#cashFlow #cashFlowAccountSelector .accountsMenu input.uaid1';
var accountButtonSelector = '#cashFlow #cashFlowAccountSelector .accountsButton';
var accountSelectorDoneButton = '#cashFlow #cashFlowAccountSelector button.done';
var accountSelectorCancelButton = '#cashFlow #cashFlowAccountSelector button.cancel';
var accountsMenu = '#cashFlow #cashFlowAccountSelector .accountsMenu';
var ieIncomeButton = '#cashFlow #cashFlowIncomeExpense button.income';
var ieExpenseButton = '#cashFlow #cashFlowIncomeExpense button.expense';
var randomTaskURL = 'http://localhost:3031/page/login/app#/accounts/details?ua=1';
describe('cashFlow screen', function () {
var client;
beforeEach(function(done){
client = webdriverjs.remote()//({logLevel: 'silent'});
client//.testMode()
.init()
.url('http://localhost:3031/initMock/cashFlowAPI')
.url('http://localhost:3031/timeMachine/' + startDate)
.url('http://localhost:3031/page/login/app#/cashFlow')
.pause(2000, done);
});
afterEach(function(done){
console.log('doing afterEach');
var ta = setTimeout(function(){
client.end(done);
}, 2000);
client.url('http://localhost:3031/resetMock')
.pause(500)
.url('http://localhost:3031/timeMachine/reset')
.pause(500)
.end(function(){
clearTimeout(ta);
done();
});
});
it('should be able to interact with income and expense buttons of cash flow', function (done) {
function _do_test_page_headline() {
client.getText('#cashFlow #cashFlowHeader h2', function (text) {
if (_DEBUG) console.log('title text', util.inspect(text));
text.value.should.eql('Cash Flow');
_do_test_before_income_button_press();
});
}
function _do_test_before_income_button_press() {
client.getAttribute(ieExpenseButton, 'class', function (result) {
result.should.match(/active/);
client.getAttribute(ieIncomeButton, 'class', function (result) {
result.should.not.match(/active/);
client.getText(barGraphJanSixValueSelector, function (text) {
text.value.should.eql(barGraphJanSixExpenseAmount);
_do_test_after_income_button_click();
});
});
});
}
function _do_test_after_income_button_click() {
client.click(ieIncomeButton)
.pause(500)
.getText(barGraphJanSixValueSelector, function (text) {
text.value.should.eql('$200.00');
client.getAttribute(ieIncomeButton, 'class', function (result) {
result.should.match(/active/);
done();
});
})
}
_do_test_page_headline();
});
it.only('should be able to interact with account selection buttons of cash flow page', function (done) {
function _do_test_page_headline() {
client.getText('#cashFlow #cashFlowHeader h2', function (text) {
if (_DEBUG) console.log('text', text.value);
text.value.should.eql('Cash Flow');
_do_test_account_selector();
});
}
function _do_test_account_selector() {
client.getText(accountButtonSelector, function (text) {
if (_DEBUG) console.log("text:", util.inspect(text));
text.value.should.eql('All');
_do_open_accounts_menu_and_inspect_account_checkbox_state();
})
}
function _do_open_accounts_menu_and_inspect_account_checkbox_state() {
client
// .tests.visible(accountsMenu, false, '#account menu is not visible')
.isVisible(accountsMenu, function(isVisible){
console.log('isVisible am: ', isVisible);
isVisible.should.eql(false, 'accounts menu visible');
client.click(accountButtonSelector)
.tests.visible(accountsMenu, true, '#account menu is visible')
.isSelected(allAccountsCheckbox, function (allAccountCheckboxCheckedState) {
allAccountCheckboxCheckedState.should.eql(false); // should be true - testing test breakage
client.isSelected(fooAccountCheckbox, //user account ID 1
function (accountFooCheckboxIsChecked) {
accountFooCheckboxIsChecked.should.eql(true);
_do_click_on_all_account_checkbox();
});
});
})
}
function _do_click_on_all_account_checkbox(){
client.click(fooAccountCheckbox);
client.isSelected(fooAccountCheckbox, function (accountFooCheckboxIsChecked) {
accountFooCheckboxIsChecked.should.eql(false);
client.isSelected(allAccountsCheckbox, function (allAccountCheckboxCheckedState) {
allAccountCheckboxCheckedState.should.eql(false);
_do_done_and_reopen();
});
})
}
function _do_done_and_reopen(){
client.click(accountSelectorDoneButton)
.pause(500)
.click(accountButtonSelector)
.pause(500)
.isSelected(fooAccountCheckbox, function (foo_sel3) {
foo_sel3.should.eql(false); // the fact that we clicked on cancel should mean that the previous changes are not retained
done();
});
}
_do_test_page_headline();
});
it('should reflect account selection and income expense selection in the balances of the bar graph', function (done) {
function _do_test_page_headline() {
client.getText('#cashFlow #cashFlowHeader h2', function (text) {
if (_DEBUG) console.log('text', text.value);
text.value.should.eql('Cash Flow');
_do_check_account_button_label_and_initial_balance();
});
}
function _do_check_account_button_label_and_initial_balance() {
client.getText(accountButtonSelector, function (text) {
if (_DEBUG) console.log("text:", util.inspect(text));
text.value.should.eql('All');
client.getText(barGraphJanSixValueSelector, function(text){
text.value.should.eql(barGraphJanSixExpenseAmount);
client.tests.visible(accountsMenu, false, '#account menu is not visible')
_do_open_menu_and_check_all_checkbox_state();
});
})
}
function _do_open_menu_and_check_all_checkbox_state() {
client.click(accountButtonSelector)
.tests.visible(accountsMenu, true, '#account menu is visible')
.isSelected(allAccountsCheckbox, function (allAccountCheckboxCheckedState) {
allAccountCheckboxCheckedState.should.eql(true);
client.isSelected(fooAccountCheckbox, //user account ID 1
function (accountFooCheckboxIsChecked) {
accountFooCheckboxIsChecked.should.eql(true);
client.getText(barGraphJanSixValueSelector, function (text) {
// you have checked off an account but haven't hit done so the balance should stay the same
text.value.should.eql(barGraphJanSixExpenseAmount);
_do_deselect_foo_checkbox();
});
}); // end (foo account selected check)
}); // end (all account selected check)
}
function _do_deselect_foo_checkbox(){
client.click(fooAccountCheckbox)
.isSelected(allAccountsCheckbox, function (allAccountCheckboxCheckedState) {
allAccountCheckboxCheckedState.should.eql(false);
client.isSelected(fooAccountCheckbox, //user account ID 1
function (accountFooCheckboxIsChecked) {
accountFooCheckboxIsChecked.should.eql(false);
_do_click_on_done_button_then_reopen_and_examine();
});
});
}
function _do_click_on_done_button_then_reopen_and_examine(){
client.click(accountSelectorDoneButton)
.click(accountButtonSelector) // reopen account menu
.isSelected(fooAccountCheckbox, function (accountFooCheckboxIsChecked) {
accountFooCheckboxIsChecked.should.eql(false);
client.isSelected(allAccountsCheckbox, function (allAccountCheckboxCheckedState) {
allAccountCheckboxCheckedState.should.eql(false);
client.getText(barGraphJanSixValueSelector, function (text) {
// the value should change
text.value.should.eql(barGraphJanSixExpenseAmountWithoutFoo);
client.click(ieIncomeButton)
.getText(barGraphJanSixValueSelector, function (text) {
text.value.should.eql(barGraphJanSixIncomeAmountWithoutFoo);
done();
});
});
});
});
}
_do_test_page_headline();
});
it('should be able to flush the users alterations when the user hits cancel after selecting/ deselecting accounts', function (done) {
function _do_test_page_headline() {
client.getText('#cashFlow #cashFlowHeader h2', function (text) {
if (_DEBUG) console.log('text', text.value);
text.value.should.eql('Cash Flow');
_do_test_account_selector();
});
}
function _do_test_account_selector() {
client.getText(accountButtonSelector, function (text) {
if (_DEBUG) console.log("text:", util.inspect(text));
text.value.should.eql('All');
_do_click_on_all_button();
})
}
function _do_click_on_all_button() {
client
.tests.visible(accountsMenu, false, '#account menu is visible')
.click(accountButtonSelector)
.tests.visible(accountsMenu, true, '#account menu is not visible')
.isSelected(allAccountsCheckbox, function (all_sel) {
all_sel.should.eql(true);
_do_click_on_account_buttons()
})
}
function _do_click_on_account_buttons() {
client.isSelected(fooAccountCheckbox, function (foo_sel) {
foo_sel.should.eql(true);
client.click(fooAccountCheckbox);
client.isSelected(fooAccountCheckbox, function (foo_sel2) {
foo_sel2.should.eql(false);
client.isSelected(allAccountsCheckbox, function (all_sel) {
all_sel.should.eql(false);
_do_cancel_and_reopen();
})
})
})
}
function _do_cancel_and_reopen(){
client.click(accountSelectorCancelButton)
.pause(500)
.click(accountButtonSelector)
.pause(500)
.isSelected(fooAccountCheckbox, function (foo_sel3) {
foo_sel3.should.eql(true); // the fact that we clicked on cancel should mean that the previous changes are not retained
done();
});
}
_do_test_page_headline();
});
it('should be able to restore previous account selection when returning to cashFlow', function (done) {
function _do_click_on_account_buttons() {
client.click(accountButtonSelector)
.pause(500)
.isSelected(fooAccountCheckbox, function (foo_sel) {
foo_sel.should.eql(true);
client.click(fooAccountCheckbox)
.pause(500)
.isSelected(fooAccountCheckbox, function (foo_sel2) {
foo_sel2.should.eql(false);
client.isSelected(allAccountsCheckbox, function (all_sel) {
all_sel.should.eql(false);
_deselect_foo();
})
})
})
}
function _deselect_foo(){
client.click(accountSelectorDoneButton)
.pause(500)
.click(accountButtonSelector)
.pause(500)
.isSelected(fooAccountCheckbox, function (foo_sel3) {
foo_sel3.should.eql(false); // the fact that we clicked on cancel should mean that the previous changes are not retained
_navigate_to_accountDetails();
});
}
function _navigate_to_accountDetails() {
client.url(randomTaskURL)
.pause(2000, _do_test_accountDetails_accountName);
}
function _do_test_accountDetails_accountName() {
client.getText('#accountDetails .moduleFrame .detailsSection .content .details .name', function (text) {
if (_DEBUG) console.log('text', text.value);
text.value.should.eql('foo');
_return_to_cashFlow();
});
}
function _return_to_cashFlow() {
client.url('http://localhost:3031/page/login/app#/cashFlow')
.pause(2000, _verify_selected_accounts);
}
function _verify_selected_accounts() {
client.getText(accountButtonSelector, function (text) {
text.value.should.eql('2 Accounts');
client.isSelected(fooAccountCheckbox, function (foo_sel) {
foo_sel.should.eql(false);
client.isSelected(allAccountsCheckbox, function (all_sel) {
all_sel.should.eql(false);
done();
})
})
})
}
_do_click_on_account_buttons();
});
});
@bingomanatee
Copy link
Author

When a test fails, the fact that it throws an error makes it impossible to end the client and cleanly end the mocha test; I always seem to be stuck in a "Pause" mode and end up waiting for mocha to time out.

The "After" clause in the test DOES execute, but it doesn't seem to be able to close the client and execute done.

Here is the test output. Note the "only" test (line 108) is the one being executed and I have forced it to fail.

Dave-Edelharts-MacBook-of-Awesomeness:tests dedelhart$ mocha seleniumTests/cashFlow.js -t 100000 -R spec
today: 2013-02-14

cashFlow screen

◦ should be able to interact with account selection buttons of cash flow page:

Selenium 2.0/webdriver protocol bindings implementation with helper commands in nodejs by Camilo Tapia.
For a complete list of commands, visit http://code.google.com/p/selenium/wiki/JsonWireProtocol.
Not all commands are implemented yet. visit https://github.com/Camme/webdriverjs for more info on webdriverjs.

[09:59:15]: COMMAND POST "/wd/hub/session"
[09:59:15]: DATA {"desiredCapabilities":{"browserName":"firefox","version":"","javascriptEnabled":true,"platform":"ANY"},"sessionId":null}
[09:59:16]: SET SESSION ID "9d981c4c-2d72-4fd2-a05e-14412f604425"
[09:59:16]: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/url"
[09:59:16]: DATA {"url":"http://localhost:3031/initMock/cashFlowAPI"}
[09:59:17]: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/url"
[09:59:17]: DATA {"url":"http://localhost:3031/timeMachine/2013_02_01"}
[09:59:17]: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/url"
[09:59:17]: DATA {"url":"http://localhost:3031/page/login/app#/cashFlow"}
09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element"
09:59:19: DATA {"using":"css selector","value":"#cashFlow #cashFlowHeader h2"}
09:59:19: RESULT {"ELEMENT":"0"}
09:59:19: COMMAND GET "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element/0/text"

09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element"
09:59:19: DATA {"using":"css selector","value":"#cashFlow #cashFlowAccountSelector .accountsButton"}
09:59:19: RESULT {"ELEMENT":"1"}
09:59:19: COMMAND GET "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element/1/text"

09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element"
09:59:19: DATA {"using":"css selector","value":"#cashFlow #cashFlowAccountSelector .accountsMenu"}
09:59:19: RESULT {"ELEMENT":"2"}
09:59:19: COMMAND GET "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element/2/displayed"

isVisible am: false
09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element"
09:59:19: DATA {"using":"css selector","value":"#cashFlow #cashFlowAccountSelector .accountsButton"}
09:59:19: RESULT {"ELEMENT":"1"}
09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element/1/click"
09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element"
09:59:19: DATA {"using":"css selector","value":"#cashFlow #cashFlowAccountSelector .accountsMenu"}
09:59:19: RESULT {"ELEMENT":"2"}
09:59:19: COMMAND GET "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element/2/displayed"
09:59:19: RESULT true
✔ #account menu is visible
09:59:19: COMMAND POST "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element"
09:59:19: DATA {"using":"css selector","value":"#cashFlow #cashFlowAccountSelector input.all"}
09:59:19: RESULT {"ELEMENT":"3"}
09:59:19: COMMAND GET "/wd/hub/session/9d981c4c-2d72-4fd2-a05e-14412f604425/element/3/selected"
09:59:19: RESULT true
1) should be able to interact with account selection buttons of cash flow page
doing afterEach

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment