Skip to content

Instantly share code, notes, and snippets.

@jeffsheets
Created April 8, 2015 18:13
Show Gist options
  • Save jeffsheets/2f2b14ab0b90525f888b to your computer and use it in GitHub Desktop.
Save jeffsheets/2f2b14ab0b90525f888b to your computer and use it in GitHub Desktop.
Protractor spec showing how to test for text selection inside a form input field
/**
* Initially written as a test for IE/Chrome for a bug in ui-mask.
* https://github.com/angular-ui/ui-utils/issues/302
*
* The test verifies that all of the text is selected in an input field when the field is tabbed into
*/
describe('field selection test', function () {
beforeEach (function () {
getRoute('#/app/events');
});
describe('tabbing into a field', function () {
it('should highlight all the text when tabbed into a field', function() {
//First - enter some known data in the endDate field (which is the 2nd field in the form)
var endField = element(by.model('event.endDate'));
endField.clear();
endField.sendKeys('10/30/2999' + protractor.Key.TAB);
//Second - send a tab from the first field on the form
var startField = element(by.model('event.startDate'));
startField.sendKeys(protractor.Key.TAB);
//This gets the length of the selection of the endDate field
//Basically if it is 10 then we know the entire field was selected, which is what we want.
var selectionLength = browser.executeScript('' +
'return document.getElementById("endDate").selectionEnd;' +
'');
expect(selectionLength).toBe(10);
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment