Skip to content

Commit

Permalink
Add unit tests for setValue/setValueImmediate (#507)
Browse files Browse the repository at this point in the history
* Add unit tests for setValue/setValueImmediate

* Add error for weird values

* Change error message for bad arguments to setValue
  • Loading branch information
imurchie authored Aug 17, 2017
1 parent 2438208 commit 2815129
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ before_install:
# code from http://stackoverflow.com/a/24600210/375688
- sed -i '' 's/[email protected]:/https:\/\/github.com\//' /Users/travis/build/appium/appium-xcuitest-driver/.gitmodules
- git submodule update --init --recursive
before_script:
- node --version
- npm install
script:
- npm run lint && npm run mocha -- -t 480000 --recursive build/test/$TEST -g @skip-ci -i
- if [ -n "$COVERALLS" ]; then npm run coverage; fi
Expand Down
11 changes: 7 additions & 4 deletions lib/commands/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,15 @@ commands.setValue = async function (value, el) {
// ['some text']
// ['s', 'o', 'm', 'e', ' ', 't', 'e', 'x', 't']
// 'some text'
if (_.isString(value)) {
// plain string, so make it into an array of characters
value = value.split('');
} else if (_.isArray(value)) {
if (_.isNull(value) || _.isUndefined(value) || _.isPlainObject(value)) {
throw new Error(`Only strings and arrays of strings are supported as input arguments. Received: '${JSON.stringify(value)}'`);
}
if (_.isArray(value)) {
// make sure that all the strings inside are a single character long
value = _.flatMap(value, (v) => (_.isString(v) ? v : JSON.stringify(v)).split(''));
} else {
// make it into an array of characters
value = (value || '').toString().split('');
}

if (!hasSpecialKeys(value)) {
Expand Down
2 changes: 2 additions & 0 deletions test/functional/basic/element-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ describe('XCUITestDriver - element(s)', function () {
});

describe('interactions', function () {
this.retries(2);

describe('text fields', () => {
let text1 = 'bunchoftext';
let text2 = 'differenttext';
Expand Down
3 changes: 2 additions & 1 deletion test/functional/basic/touch-id-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ chai.use(chaiAsPromised);

const MOCHA_RETRIES = process.env.TRAVIS ? 3 : 1;

if (!process.env.REAL_DEVICE) {
// touch id tests need to be on sims and need accessibility turned on
if (!process.env.REAL_DEVICE && !process.env.TRAVIS) {
describe('touchID() ', function () {
this.timeout(MOCHA_TIMEOUT * 2);
this.retries(MOCHA_RETRIES);
Expand Down
70 changes: 70 additions & 0 deletions test/unit/commands/element-specs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import sinon from 'sinon';
import XCUITestDriver from '../../..';
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import { withMocks } from 'appium-test-support';


chai.should();
chai.use(chaiAsPromised);

describe('element commands', function () {
let driver = new XCUITestDriver();
let proxySpy = sinon.stub(driver, 'proxyCommand');

afterEach(function () {
proxySpy.reset();
});

describe('setValueImmediate', withMocks({driver}, (mocks) => {
it('should call setValue', async function () {
mocks.driver
.expects('setValue')
.once().withExactArgs('hello', 2).returns();
await driver.setValueImmediate('hello', 2);
mocks.driver.verify();
});
}));

describe('setValue', function () {
const elementId = 2;
const expectedEndpoint = `/element/${elementId}/value`;
const expectedMethod = 'POST';

describe('success', function () {
afterEach(function () {
proxySpy.calledOnce.should.be.true;
proxySpy.firstCall.args[0].should.eql(expectedEndpoint);
proxySpy.firstCall.args[1].should.eql(expectedMethod);
});

it('should proxy string as array of characters', async function () {
await driver.setValue('hello', elementId);
proxySpy.firstCall.args[2].should.eql({value: ['h', 'e', 'l', 'l', 'o']});
});
it('should proxy integer as array of characters', async function () {
await driver.setValue(1234, elementId);
proxySpy.firstCall.args[2].should.eql({value: ['1', '2', '3', '4']});
});
it('should proxy string array as array of characters', async function () {
await driver.setValue(['hel', 'lo'], elementId);
proxySpy.firstCall.args[2].should.eql({value: ['h', 'e', 'l', 'l', 'o']});
});
it('should proxy integer array as array of characters', async function () {
await driver.setValue([1234], elementId);
proxySpy.firstCall.args[2].should.eql({value: ['1', '2', '3', '4']});
});
});

describe('failure', function () {
it('should throw invalid argument exception for null', async function () {
await driver.setValue(null, elementId)
.should.eventually.be.rejectedWith(/Only strings and arrays of strings are supported as input arguments. Received: 'null'/);
});
it('should throw invalid argument exception for object', async function () {
await driver.setValue({hi: 'there'}, elementId)
.should.eventually.be.rejectedWith(/Only strings and arrays of strings are supported as input arguments. Received: '{"hi":"there"}'/);
});
});
});
});

0 comments on commit 2815129

Please sign in to comment.