Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added optional percentage parameter to swipe action #162

Merged
merged 3 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 25 additions & 8 deletions detox/src/ios/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ScrollEdgeAction extends Action {
}

class SwipeAction extends Action {
constructor(direction, speed) {
constructor(direction, speed, percentage) {
super();
if (typeof direction !== 'string') throw new Error(`SwipeAction ctor 1st argument must be a string, got ${typeof direction}`);
if (typeof speed !== 'string') throw new Error(`SwipeAction ctor 2nd argument must be a string, got ${typeof speed}`);
Expand All @@ -130,12 +130,29 @@ class SwipeAction extends Action {
case 'down': direction = 4; break;
default: throw new Error(`SwipeAction direction must be a 'left'/'right'/'up'/'down', got ${direction}`);
}
if (speed == 'fast') {
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForSwipeFastInDirection:', invoke.IOS.NSInteger(direction));
} else if (speed == 'slow') {
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForSwipeSlowInDirection:', invoke.IOS.NSInteger(direction));
if (percentage) {
let x, y;
switch (direction) {
case 1: x = percentage, y = 0.0; break;
case 2: x = percentage, y = 0.0; break;
case 3: y = percentage, x = 0.0; break;
case 4: y = percentage, x = 0.0; break;
}
if (speed == 'fast') {
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForSwipeFastInDirection:xOriginStartPercentage:yOriginStartPercentage:', invoke.IOS.NSInteger(direction), invoke.IOS.CGFloat(x), invoke.IOS.CGFloat(y));
} else if (speed == 'slow') {
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForSwipeSlowInDirection:xOriginStartPercentage:yOriginStartPercentage:', invoke.IOS.NSInteger(direction), invoke.IOS.CGFloat(x), invoke.IOS.CGFloat(y));
} else {
throw new Error(`SwipeAction speed must be a 'fast'/'slow', got ${speed}`);
}
} else {
throw new Error(`SwipeAction speed must be a 'fast'/'slow', got ${speed}`);
if (speed == 'fast') {
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForSwipeFastInDirection:', invoke.IOS.NSInteger(direction));
} else if (speed == 'slow') {
this._call = invoke.call(invoke.IOS.Class('GREYActions'), 'actionForSwipeSlowInDirection:', invoke.IOS.NSInteger(direction));
} else {
throw new Error(`SwipeAction speed must be a 'fast'/'slow', got ${speed}`);
}
}
}
}
Expand Down Expand Up @@ -262,10 +279,10 @@ class Element {
this._selectElementWithMatcher(this._originalMatcher._extendToDescendantScrollViews());
return await new ActionInteraction(this, new ScrollEdgeAction(edge)).execute();
}
async swipe(direction, speed = 'fast') {
async swipe(direction, speed = 'fast', percentage = 0) {
// override the user's element selection with an extended matcher that avoids RN issues with RCTScrollView
this._selectElementWithMatcher(this._originalMatcher._avoidProblematicReactNativeElements());
return await new ActionInteraction(this, new SwipeAction(direction, speed)).execute();
return await new ActionInteraction(this, new SwipeAction(direction, speed, percentage)).execute();
}
}

Expand Down
5 changes: 5 additions & 0 deletions detox/src/ios/expect.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,10 @@ describe('expect', async () => {
await e.element(e.by.id('ScrollView799')).swipe('up', 'slow');
await e.element(e.by.id('ScrollView799')).swipe('left', 'fast');
await e.element(e.by.id('ScrollView799')).swipe('right', 'slow');
await e.element(e.by.id('ScrollView799')).swipe('down', 'fast', 0.9);
await e.element(e.by.id('ScrollView799')).swipe('up', 'slow', 0.9);
await e.element(e.by.id('ScrollView799')).swipe('left', 'fast', 0.9);
await e.element(e.by.id('ScrollView799')).swipe('right', 'slow', 0.9);
await e.element(e.by.id('ScrollView799')).atIndex(1);
});

Expand All @@ -131,6 +135,7 @@ describe('expect', async () => {
await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('noDirection', 0));
await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('noDirection', 'fast'));
await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('down', 'NotFastNorSlow'));
await expectToThrow(() => e.element(e.by.id('ScrollView799')).swipe('down', 'NotFastNorSlow', 0.9));
await expectToThrow(() => e.element(e.by.id('ScrollView799')).atIndex('NaN'));
});

Expand Down