Skip to content

Commit

Permalink
added matchers withAncestor and withDescendant
Browse files Browse the repository at this point in the history
  • Loading branch information
talkol committed Oct 11, 2016
1 parent f94aeee commit 639f335
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2 deletions.
4 changes: 4 additions & 0 deletions detox/ios/Detox/GREYMatchers+Detox.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

+ (id<GREYMatcher>)detoxMatcherForBoth:(id<GREYMatcher>)firstMatcher and:(id<GREYMatcher>)secondMatcher;

+ (id<GREYMatcher>)detoxMatcherForBoth:(id<GREYMatcher>)firstMatcher andAncestorMatcher:(id<GREYMatcher>)ancestorMatcher;

+ (id<GREYMatcher>)detoxMatcherForBoth:(id<GREYMatcher>)firstMatcher andDescendantMatcher:(id<GREYMatcher>)descendantMatcher;

+ (id<GREYMatcher>)detoxMatcherForNot:(id<GREYMatcher>)matcher;

+ (id<GREYMatcher>)detoxMatcherForClass:(NSString *)aClassName;
Expand Down
10 changes: 10 additions & 0 deletions detox/ios/Detox/GREYMatchers+Detox.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ @implementation GREYMatchers (Detox)
return grey_allOf(firstMatcher, secondMatcher, nil);
}

+ (id<GREYMatcher>)detoxMatcherForBoth:(id<GREYMatcher>)firstMatcher andAncestorMatcher:(id<GREYMatcher>)ancestorMatcher
{
return grey_allOf(firstMatcher, grey_ancestor(ancestorMatcher), nil);
}

+ (id<GREYMatcher>)detoxMatcherForBoth:(id<GREYMatcher>)firstMatcher andDescendantMatcher:(id<GREYMatcher>)descendantMatcher
{
return grey_allOf(firstMatcher, grey_descendant(descendantMatcher), nil);
}

+ (id<GREYMatcher>)detoxMatcherForNot:(id<GREYMatcher>)matcher
{
return grey_not(matcher);
Expand Down
18 changes: 17 additions & 1 deletion detox/src/ios/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,20 @@ detox.invoke.execute(_getInteraction2);

//// classes

class Matcher {}
class Matcher {
withAncestor(matcher) {
if (!matcher instanceof Matcher) throw new Error(`Matcher withAncestor argument must be a valid Matcher, got ${typeof matcher}`);
const _originalMatcherCall = this._call;
this._call = invoke.call(invoke.IOS.Class('GREYMatchers'), 'detoxMatcherForBoth:andAncestorMatcher:', _originalMatcherCall, matcher._call);
return this;
}
withDescendant(matcher) {
if (!matcher instanceof Matcher) throw new Error(`Matcher withDescendant argument must be a valid Matcher, got ${typeof matcher}`);
const _originalMatcherCall = this._call;
this._call = invoke.call(invoke.IOS.Class('GREYMatchers'), 'detoxMatcherForBoth:andDescendantMatcher:', _originalMatcherCall, matcher._call);
return this;
}
}

class LabelMatcher extends Matcher {
constructor(value) {
Expand Down Expand Up @@ -87,6 +100,7 @@ class TextMatcher extends Matcher {
}
}

// TODO: maybe refactor this and move into a member of Matcher class
class ExtendedScrollMatcher extends Matcher {
constructor(matcher) {
super();
Expand All @@ -95,6 +109,7 @@ class ExtendedScrollMatcher extends Matcher {
}
}

// TODO: maybe refactor this and move into a member of Matcher class
class CombineBothMatcher extends Matcher {
constructor(firstMatcher, secondMatcher) {
super();
Expand All @@ -104,6 +119,7 @@ class CombineBothMatcher extends Matcher {
}
}

// TODO: maybe refactor this and move into a member of Matcher class
class NotMatcher extends Matcher {
constructor(matcher) {
super();
Expand Down
20 changes: 19 additions & 1 deletion detox/test/e2e/b-matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,28 @@ describe('Matchers', function () {
expect(element(by.label('ID Working!!!'))).toBeVisible();
});

it('should match elements by type', function () {
it('should match elements by type (native class)', function () {
expect(element(by.type('RCTImageView'))).toBeVisible();
element(by.type('RCTImageView')).tap();
expect(element(by.type('RCTImageView'))).toBeNotVisible();
});

it('should match elements with ancenstor (parent)', function () {
expect(element(by.id('Grandson883').withAncestor(by.id('Son883')))).toExist();
expect(element(by.id('Son883').withAncestor(by.id('Grandson883')))).toNotExist();
expect(element(by.id('Grandson883').withAncestor(by.id('Father883')))).toExist();
expect(element(by.id('Father883').withAncestor(by.id('Grandson883')))).toNotExist();
expect(element(by.id('Grandson883').withAncestor(by.id('Grandfather883')))).toExist();
expect(element(by.id('Grandfather883').withAncestor(by.id('Grandson883')))).toNotExist();
});

it('should match elements with descendant (child)', function () {
expect(element(by.id('Son883').withDescendant(by.id('Grandson883')))).toExist();
expect(element(by.id('Grandson883').withDescendant(by.id('Son883')))).toNotExist();
expect(element(by.id('Father883').withDescendant(by.id('Grandson883')))).toExist();
expect(element(by.id('Grandson883').withDescendant(by.id('Father883')))).toNotExist();
expect(element(by.id('Grandfather883').withDescendant(by.id('Grandson883')))).toExist();
expect(element(by.id('Grandson883').withDescendant(by.id('Grandfather883')))).toNotExist();
});

});
8 changes: 8 additions & 0 deletions detox/test/src/Screens/MatchersScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ export default class MatchersScreen extends Component {
</TouchableOpacity> : null
}

<View testID='Grandfather883' style={{padding: 8, backgroundColor: 'red'}}>
<View testID='Father883' style={{padding: 8, backgroundColor: 'green'}}>
<View testID='Son883' style={{padding: 8, backgroundColor: 'blue'}}>
<View testID='Grandson883' style={{padding: 8, backgroundColor: 'purple'}} />
</View>
</View>
</View>

</View>
);
}
Expand Down

0 comments on commit 639f335

Please # to comment.