Skip to content

Commit cea94c6

Browse files
authored
feat: Add basic support for interactions (#85)
1 parent 78ad7cc commit cea94c6

81 files changed

Lines changed: 6329 additions & 1939 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
File renamed without changes.

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
tsconfig.json

docs/api/matchers.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
sidebar_position: 2
2+
sidebar_position: 3
33
---
44

55
# Matchers
@@ -10,13 +10,14 @@ This custom matcher will try to find and compare the baseline screenshot by usin
1010

1111
#### Example
1212

13-
```js {7}
13+
```js title="__tests__/App.owl.tsx"
1414
import { takeScreenshot } from 'react-native-owl';
1515

1616
describe('App.tsx', () => {
1717
it('takes a screenshot of the first screen', async () => {
1818
const screen = await takeScreenshot('homescreen');
1919

20+
// highlight-next-line
2021
expect(screen).toMatchBaseline();
2122
});
2223
});

docs/api/methods.md

Lines changed: 211 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ Grabs a screenshot from the simulator and stores it under `latest` screenshots(i
1010

1111
#### Example
1212

13-
```js {5}
13+
```js title="__tests__/App.owl.tsx"
1414
import { takeScreenshot } from 'react-native-owl';
1515

1616
describe('App.tsx', () => {
1717
it('takes a screenshot of the first screen', async () => {
18+
// highlight-next-line
1819
const screen = await takeScreenshot('homescreen');
1920

2021
expect(screen).toMatchBaseline();
@@ -25,3 +26,212 @@ describe('App.tsx', () => {
2526
The first time this test is run, or when run with the `--update` flag, the screenshot will be stored at `./owl/baseline/ios/homescreen.png` (or `/android/` is testing on Android).
2627

2728
On subsequent test runs, the screenshot will be stored at `./owl/current/ios/homescreen.png`.
29+
30+
### toExist(testID: string)
31+
32+
Waits for an element to exist where its `testID` prop matches the methods `testID` argument.
33+
34+
If there is no matching element, it will retry for 10 seconds before throwing an Error.
35+
36+
#### Example
37+
38+
```js title="App.tsx"
39+
...
40+
const [isLoaded, setIsLoaded] = React.useState(false);
41+
42+
React.useEffect(() => {
43+
setTimeout(() => {
44+
setIsLoaded(true);
45+
}, 1000);
46+
}, []);
47+
48+
<>
49+
{isLoaded && (
50+
<TextInput testId="testInput" />
51+
)}
52+
</>
53+
...
54+
```
55+
56+
```js title="__tests__/App.owl.tsx"
57+
import { toExist, takeScreenshot } from 'react-native-owl';
58+
59+
describe('App.tsx', () => {
60+
it('waits for an element, then takes a screenshot', async () => {
61+
// highlight-next-line
62+
await toExist('textInput');
63+
64+
const screen = await takeScreenshot('textInputExists');
65+
66+
expect(screen).toMatchBaseline();
67+
});
68+
});
69+
```
70+
71+
### press(testID: string)
72+
73+
Calls the `onPress` prop callback of the element where its `testID` prop matches the methods `testID` argument.
74+
75+
If there is no matching element, it will retry for 10 seconds before throwing an Error.
76+
77+
The element must have a `onPress` prop, or an Error will be thrown.
78+
79+
#### Example
80+
81+
```js title="App.tsx"
82+
...
83+
<Button title="Press Me" testID="button" onPress={() => {
84+
// Will be called by the react-native-owl `press` method
85+
}} />
86+
...
87+
```
88+
89+
```js title="__tests__/App.owl.tsx"
90+
import { press, takeScreenshot } from 'react-native-owl';
91+
92+
describe('App.tsx', () => {
93+
it('presses a button with testID = `button` then takes a screenshot', async () => {
94+
// highlight-next-line
95+
await press('button');
96+
97+
const screen = await takeScreenshot('afterButtonPress');
98+
99+
expect(screen).toMatchBaseline();
100+
});
101+
});
102+
```
103+
104+
### longPress(testID: string)
105+
106+
Calls the `onLongPress` prop callback of the element where its `testID` prop matches the methods `testID` argument.
107+
108+
If there is no matching element, it will retry for 10 seconds before throwing an Error.
109+
110+
The element must have a `onLongPress` prop, or an Error will be thrown.
111+
112+
#### Example
113+
114+
```js title="App.tsx"
115+
...
116+
<Button title="Long press Me" testID="button" onLongPress={() => {
117+
// Will be called by the react-native-owl `longPress` method
118+
}} />
119+
...
120+
```
121+
122+
```js title="__tests__/App.owl.tsx"
123+
import { longPress, takeScreenshot } from 'react-native-owl';
124+
125+
describe('App.tsx', () => {
126+
it('long presses a button with testID = `button` then takes a screenshot', async () => {
127+
// highlight-next-line
128+
await longPress('button');
129+
130+
const screen = await takeScreenshot('afterButtonLongPress');
131+
132+
expect(screen).toMatchBaseline();
133+
});
134+
});
135+
```
136+
137+
### changeText(testID: string, text: string)
138+
139+
Calls the `onChangeText` prop callback of the element where its `testID` prop matches the methods `testID` argument.
140+
141+
If there is no matching element, it will retry for 10 seconds before throwing an Error.
142+
143+
The element must have a `onChangeText` prop, or an Error will be thrown.
144+
145+
#### Example
146+
147+
```js title="App.tsx"
148+
...
149+
<TextInput
150+
testID="email"
151+
placeholder="Enter email address"
152+
onChangeText={setEmail}
153+
value={email}
154+
/>
155+
...
156+
```
157+
158+
```js title="__tests__/App.owl.tsx"
159+
import { changeText, takeScreenshot } from 'react-native-owl';
160+
161+
describe('App.tsx', () => {
162+
it('changes text in a TextInput with testID = `email` then takes a screenshot', async () => {
163+
// highlight-next-line
164+
await changeText('email', 'john.smith@example.com');
165+
166+
const screen = await takeScreenshot('afterChangeText');
167+
168+
expect(screen).toMatchBaseline();
169+
});
170+
});
171+
```
172+
173+
### scrollTo(testID: string, position: {x?: number, y?: number})
174+
175+
Calls the `scrollTo` method of the element where its `testID` prop matches the methods `testID` argument.
176+
177+
If there is no matching element, it will retry for 10 seconds before throwing an Error.
178+
179+
The element must have a `scrollTo` method (i.e `ScrollView`), or an Error will be thrown.
180+
181+
#### Example
182+
183+
```js title="App.tsx"
184+
...
185+
<ScrollView testID="scrollView">
186+
...
187+
</ScrollView>
188+
...
189+
```
190+
191+
```js title="__tests__/App.owl.tsx"
192+
import { scrollTo, takeScreenshot } from 'react-native-owl';
193+
194+
describe('App.tsx', () => {
195+
it('scroll down the screen a bit then takes a screenshot', async () => {
196+
// highlight-next-line
197+
await scrollTo('scrollView', {y: 50});
198+
199+
const screen = await takeScreenshot('afterScrollTo');
200+
201+
expect(screen).toMatchBaseline();
202+
});
203+
});
204+
```
205+
206+
### scrollToEnd(testID: string)
207+
208+
Calls the `scrollToEnd` method of the element where its `testID` prop matches the methods `testID` argument.
209+
210+
If there is no matching element, it will retry for 10 seconds before throwing an Error.
211+
212+
The element must have a `scrollToEnd` method (i.e `ScrollView`), or an Error will be thrown.
213+
214+
#### Example
215+
216+
```js title="App.tsx"
217+
...
218+
<ScrollView testID="scrollView">
219+
...
220+
</ScrollView>
221+
...
222+
```
223+
224+
```js title="__tests__/App.owl.tsx"
225+
import { scrollToEnd, takeScreenshot } from 'react-native-owl';
226+
227+
describe('App.tsx', () => {
228+
it('scroll down the screen a bit then takes a screenshot', async () => {
229+
// highlight-next-line
230+
await scrollToEnd('scrollView');
231+
232+
const screen = await takeScreenshot('afterScrollToEnd');
233+
234+
expect(screen).toMatchBaseline();
235+
});
236+
});
237+
```

docs/cli/testing-the-app.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ The following will be included the the output of failed tests:
108108

109109
```
110110
...
111-
[OWL] Generating Report
112-
[OWL] Report was built at /Users/username/Code/FormidableLabs/react-native-owl/example/.owl/report/index.html
111+
[OWL - CLI] Generating Report
112+
[OWL - CLI] Report was built at /Users/username/Code/FormidableLabs/react-native-owl/example/.owl/report/index.html
113113
...
114114
```

docs/introduction/getting-started.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Use the [takeScreenshot](/docs/api/methods#takescreenshotname-string) and [.toMa
3939
#### Example
4040

4141
```js
42-
import { takeScreenshot } from 'react-native-owl';
42+
import { press, takeScreenshot } from 'react-native-owl';
4343

4444
describe('App.tsx', () => {
4545
it('takes a screenshot of the first screen', async () => {
@@ -48,6 +48,16 @@ describe('App.tsx', () => {
4848
expect(screen).toMatchBaseline();
4949
});
5050
});
51+
52+
describe('App.tsx', () => {
53+
it('presses a button, then takes a screenshot', async () => {
54+
await press('button')
55+
56+
const screen = await takeScreenshot('afterButtonPress');
57+
58+
expect(screen).toMatchBaseline();
59+
});
60+
});
5161
```
5262

5363
### Building the app
@@ -71,8 +81,6 @@ yarn owl build --platform ios
7181
</TabItem>
7282
</Tabs>
7383

74-
### Work-In-Progress
75-
7684
:::info
7785

7886
You will need to manually start the correct simulator before the tests are run.

docs/introduction/work-in-progress.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,4 @@ title: Work In Progress
77

88
### Future functionality
99

10-
- We plan to integrate [Detox](https://wix.github.io/Detox/) into the library, and add methods to our [API](/docs/api/methods) to allow tests to interact with the app. For example, this could allow the particular button to be tapped, or screen to be scrolled, before taking and comparing a screenshot as part of a test.
11-
1210
- We will automate the launching to the relevant simulator, if not already running, when running tests.
206 KB
Loading
-202 KB
Binary file not shown.
213 KB
Loading

0 commit comments

Comments
 (0)