React Native(Expo) + Appiumでe2eテスト環境を構築する（断念）

[前回](https://qiita.com/harada4atsushi/items/7ca0e1b6d3fb0dd2c8b9)、DetoxというReact Native用のe2eテストライブラリを入れてみたけども、実際動かしてみるとMatcher周りがかなり貧弱で実運用ではちょっと使い物になりそうになかった。

そこで別のe2eテストの候補としてAppiumというものがあったので試してみることにした。結論、動くところまで出来なかったが、断念するところまでのログを残しておく。

## まずはExpoでブランクアプリを生成

```
$ npm install -g expo-cli
$ expo init ExpoAppium
$ cd ExpoAppium
$ expo install
$ expo start
```

前回やってるのであっさり起動出来た。

![Simulator Screen Shot - iPhone 12 - 2023-06-27 at 21.08.31.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/47677/f676aa9a-ca66-f675-1fe3-949d181a66fd.png)

続いて、今回はbare workflowのアプリへの適用を考えているので、ejectしておく。

```
$ expo eject
$ npm run ios
```

ここも問題なく動いた。

![Simulator Screen Shot - iPhone 12 - 2023-06-27 at 21.05.06.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/47677/175f6d10-d1e3-d27c-cb72-80f934815674.png)

## jestを導入

この辺を参考に。
https://github.com/expo/expo/tree/main/packages/jest-expo

```
$ npm install jest-expo --save-dev
``` 

```__tests__/Exmaple-test.js
it('works', () => {
  expect(1).toBe(1);
});
```

```
$ npm test                                   

> expoappium@1.0.0 test
> jest

watchman warning:  Recrawled this watch 15 times, most recently because:
MustScanSubDirs UserDroppedTo resolve, please review the information on
https://facebook.github.io/watchman/docs/troubleshooting.html#recrawl
To clear this warning, run:
`watchman watch-del '/Users/aharada/source/ExpoAppium' ; watchman watch-project '/Users/aharada/source/ExpoAppium'`

 PASS  __tests__/Exmaple-test.js
  ✓ works (1 ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.317 s
Ran all test suites.
```

よしテストは通ったぞ。


## Appiumを導入

`wd`というライブラリを使っている例が比較的多いが、以前に触ったときちゃんと動かなかったので、`webdriverio`を使う。

この辺を参考に進めてみる。
https://www.waldo.com/blog/how-to-test-react-native-apps-with-appium

必要なライブラリをインストールし、設定を実行する。
あれ`jest`に対応していない？まあいいとりあえず進める。

```
$ npm install appium webdriverio @wdio/cli --save-dev
$ npx wdio config

===============================
🤖 WDIO Configuration Wizard 🧙
===============================

? A project named "expoappium" was detected at "/Users/aharada/source/ExpoAppium", correct? Yes
? Where should your tests be launched? local - for e2e testing of web and mobile applications
? Where is your automation backend located? On my local machine
? Would you like to setup Appium for mobile testing? Yes
? Which framework do you want to use? Mocha (https://mochajs.org/)
? Do you want to use a compiler? TypeScript (https://www.typescriptlang.org/)
? Do you want WebdriverIO to autogenerate some test files? Yes
? Where should these files be located? /Users/aharada/source/ExpoAppium/test/specs/**/*.ts
? Do you want to use page objects (https://martinfowler.com/bliki/PageObject.html)? No
? Which reporter do you want to use? spec
? Do you want to add a plugin to your test setup?
? Do you want to add a service to your test setup? appium
? Do you want me to run `npm install` Yes


Setting up TypeScript...
✔ Success!

Installing wdio packages:
- @wdio/local-runner@latest
- @wdio/mocha-framework@latest
- @wdio/spec-reporter@latest
- @wdio/appium-service@latest
- ts-node
- typescript
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'execa@7.1.1',
npm WARN EBADENGINE   required: { node: '^14.18.0 || ^16.14.0 || >=18.0.0' },
npm WARN EBADENGINE   current: { node: 'v16.13.2', npm: '8.1.2' }
npm WARN EBADENGINE }
npm WARN EBADENGINE Unsupported engine {
npm WARN EBADENGINE   package: 'lru-cache@9.1.2',
npm WARN EBADENGINE   required: { node: '14 || >=16.14' },
npm WARN EBADENGINE   current: { node: 'v16.13.2', npm: '8.1.2' }
npm WARN EBADENGINE }

added 69 packages, and audited 2724 packages in 10s

125 packages are looking for funding
  run `npm fund` for details

171 vulnerabilities (1 low, 134 moderate, 20 high, 16 critical)

To address issues that do not require attention, run:
  npm audit fix

To address all issues (including breaking changes), run:
  npm audit fix --force

Run `npm audit` for details.
✔ Success!

Creating a WebdriverIO config file...
✔ Success!

Autogenerating test files...
✔ Success!

Adding "wdio" script to package.json.
✔ Success!

🤖 Successfully setup project at /Users/aharada/source/ExpoAppium 🎉

Join our Discord Community Server and instantly find answers to your issues or queries. Or just join and say hi 👋!
  🔗 https://discord.webdriver.io

Visit the project on GitHub to report bugs 🐛 or raise feature requests 💡:
  🔗 https://github.com/webdriverio/webdriverio

To run your tests, execute:
$ cd /Users/aharada/source/ExpoAppium
$ npm run wdio

? Continue with Appium setup using appium-installer (https://github.com/AppiumTestDistribution/appium-installer)? No
Ok! You can learn more about setting up mobile environments in the Appium docs at https://appium.io/docs/en/2.0/quickstart/
```

今回はiOSアプリをテストしたいので、以下のように編集する。

```wdio.conf.ts
    capabilities: [{
        platformName: 'iOS',
        browserName: 'Safari',
        'appium:deviceName': 'iPhone Simulator',
        'appium:platformVersion': '16.2',
        'appium:automationName': 'XCUITest'
    }],
```

テスト実行してみるもエラー。なんか`xcodebuild`が失敗してるっぽい。

```
$ npx wdio wdio.conf.ts                                                                        

Execution of 1 workers started at 2023-06-27T12:36:26.429Z

2023-06-27T12:36:26.447Z INFO @wdio/cli:launcher: Run onPrepare hook
2023-06-27T12:36:27.027Z INFO @wdio/cli:launcher: Run onWorkerStart hook
2023-06-27T12:36:27.027Z INFO @wdio/local-runner: Start worker 0-0 with arg: wdio.conf.ts
[0-0] 2023-06-27T12:36:27.508Z INFO @wdio/local-runner: Run worker command: run
[0-0] RUNNING in Safari - file:///test/specs/example.e2e.ts
[0-0] 2023-06-27T12:36:27.704Z INFO webdriver: Initiate new session using the WebDriver protocol
[0-0] 2023-06-27T12:36:27.749Z INFO webdriver: [POST] http://127.0.0.1:4723/session
[0-0] 2023-06-27T12:36:27.749Z INFO webdriver: DATA {
[0-0]   capabilities: {
[0-0]     alwaysMatch: {
[0-0]       platformName: 'iOS',
[0-0]       browserName: 'Safari',
[0-0]       'appium:deviceName': 'iPhone Simulator',
[0-0]       'appium:platformVersion': '16.2',
[0-0]       'appium:automationName': 'XCUITest'
[0-0]     },
[0-0]     firstMatch: [ {} ]
[0-0]   },
[0-0]   desiredCapabilities: {
[0-0]     platformName: 'iOS',
[0-0]     browserName: 'Safari',
[0-0]     'appium:deviceName': 'iPhone Simulator',
[0-0]     'appium:platformVersion': '16.2',
[0-0]     'appium:automationName': 'XCUITest'
[0-0]   }
[0-0] }
[0-0] 2023-06-27T12:37:12.141Z WARN webdriver: Request failed with status 500 due to An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
[0-0] xcodebuild error message:
[0-0]
[0-0] 2023-06-27T12:37:12.142Z INFO webdriver: Retrying 1/3
[0-0] 2023-06-27T12:37:12.142Z INFO webdriver: [POST] http://127.0.0.1:4723/session
[0-0] 2023-06-27T12:37:12.142Z INFO webdriver: DATA {
[0-0]   capabilities: {
[0-0]     alwaysMatch: {
[0-0]       platformName: 'iOS',
[0-0]       browserName: 'Safari',
[0-0]       'appium:deviceName': 'iPhone Simulator',
[0-0]       'appium:platformVersion': '16.2',
[0-0]       'appium:automationName': 'XCUITest'
[0-0]     },
[0-0]     firstMatch: [ {} ]
[0-0]   },
[0-0]   desiredCapabilities: {
[0-0]     platformName: 'iOS',
[0-0]     browserName: 'Safari',
[0-0]     'appium:deviceName': 'iPhone Simulator',
[0-0]     'appium:platformVersion': '16.2',
[0-0]     'appium:automationName': 'XCUITest'
[0-0]   }
[0-0] }
[0-0] 2023-06-27T12:37:36.897Z WARN webdriver: Request failed with status 500 due to An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
[0-0] xcodebuild error message:
[0-0]
[0-0] 2023-06-27T12:37:36.897Z INFO webdriver: Retrying 2/3
[0-0] 2023-06-27T12:37:36.897Z INFO webdriver: [POST] http://127.0.0.1:4723/session
[0-0] 2023-06-27T12:37:36.897Z INFO webdriver: DATA {
[0-0]   capabilities: {
[0-0]     alwaysMatch: {
[0-0]       platformName: 'iOS',
[0-0]       browserName: 'Safari',
[0-0]       'appium:deviceName': 'iPhone Simulator',
[0-0]       'appium:platformVersion': '16.2',
[0-0]       'appium:automationName': 'XCUITest'
[0-0]     },
[0-0]     firstMatch: [ {} ]
[0-0]   },
[0-0]   desiredCapabilities: {
[0-0]     platformName: 'iOS',
[0-0]     browserName: 'Safari',
[0-0]     'appium:deviceName': 'iPhone Simulator',
[0-0]     'appium:platformVersion': '16.2',
[0-0]     'appium:automationName': 'XCUITest'
[0-0]   }
[0-0] }
[0-0] 2023-06-27T12:37:57.889Z WARN webdriver: Request failed with status 500 due to An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
[0-0] xcodebuild error message:
[0-0]
[0-0] 2023-06-27T12:37:57.889Z INFO webdriver: Retrying 3/3
[0-0] 2023-06-27T12:37:57.889Z INFO webdriver: [POST] http://127.0.0.1:4723/session
[0-0] 2023-06-27T12:37:57.889Z INFO webdriver: DATA {
[0-0]   capabilities: {
[0-0]     alwaysMatch: {
[0-0]       platformName: 'iOS',
[0-0]       browserName: 'Safari',
[0-0]       'appium:deviceName': 'iPhone Simulator',
[0-0]       'appium:platformVersion': '16.2',
[0-0]       'appium:automationName': 'XCUITest'
[0-0]     },
[0-0]     firstMatch: [ {} ]
[0-0]   },
[0-0]   desiredCapabilities: {
[0-0]     platformName: 'iOS',
[0-0]     browserName: 'Safari',
[0-0]     'appium:deviceName': 'iPhone Simulator',
[0-0]     'appium:platformVersion': '16.2',
[0-0]     'appium:automationName': 'XCUITest'
[0-0]   }
[0-0] }
[0-0] 2023-06-27T12:38:26.818Z ERROR webdriver: Request failed with status 500 due to unknown error: An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
[0-0] xcodebuild error message:
[0-0]
[0-0] 2023-06-27T12:38:26.818Z ERROR webdriver: unknown error: An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
[0-0] xcodebuild error message:
[0-0]
[0-0]     at getErrorFromResponseBody (file:///Users/aharada/source/ExpoAppium/node_modules/webdriver/build/utils.js:194:12)
[0-0]     at NodeJSRequest._request (file:///Users/aharada/source/ExpoAppium/node_modules/webdriver/build/request/index.js:164:23)
[0-0]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[0-0]     at async startWebDriverSession (file:///Users/aharada/source/ExpoAppium/node_modules/webdriver/build/utils.js:63:20)
[0-0]     at async Function.newSession (file:///Users/aharada/source/ExpoAppium/node_modules/webdriver/build/index.js:26:45)
[0-0]     at async remote (file:///Users/aharada/source/ExpoAppium/node_modules/webdriverio/build/index.js:48:22)
[0-0]     at async Runner._startSession (file:///Users/aharada/source/ExpoAppium/node_modules/@wdio/runner/build/index.js:221:29)
[0-0]     at async Runner._initSession (file:///Users/aharada/source/ExpoAppium/node_modules/@wdio/runner/build/index.js:187:25)
[0-0]     at async Runner.run (file:///Users/aharada/source/ExpoAppium/node_modules/@wdio/runner/build/index.js:77:19)
[0-0] 2023-06-27T12:38:26.820Z ERROR @wdio/runner: Error: Failed to create session.
[0-0] An unknown server-side error occurred while processing the command. Original error: Unable to launch WebDriverAgent because of xcodebuild failure: xcodebuild failed with code 65
[0-0] xcodebuild error message:
[0-0]
[0-0]     at startWebDriverSession (file:///Users/aharada/source/ExpoAppium/node_modules/webdriver/build/utils.js:68:15)
[0-0]     at processTicksAndRejections (node:internal/process/task_queues:96:5)
[0-0]     at async Function.newSession (file:///Users/aharada/source/ExpoAppium/node_modules/webdriver/build/index.js:26:45)
[0-0]     at async remote (file:///Users/aharada/source/ExpoAppium/node_modules/webdriverio/build/index.js:48:22)
[0-0]     at async Runner._startSession (file:///Users/aharada/source/ExpoAppium/node_modules/@wdio/runner/build/index.js:221:29)
[0-0]     at async Runner._initSession (file:///Users/aharada/source/ExpoAppium/node_modules/@wdio/runner/build/index.js:187:25)
[0-0]     at async Runner.run (file:///Users/aharada/source/ExpoAppium/node_modules/@wdio/runner/build/index.js:77:19)
[0-0] FAILED in Safari - file:///test/specs/example.e2e.ts
2023-06-27T12:38:26.936Z INFO @wdio/cli:launcher: Run onWorkerEnd hook
2023-06-27T12:38:26.937Z INFO @wdio/cli:launcher: Run onComplete hook

Spec Files:	 0 passed, 1 failed, 1 total (100% completed) in 00:02:00
```

けっこういろんな人が同じエラーにハマっているらしく情報がいろいろあった。
- https://www.youtube.com/watch?v=4E_Dcu6Ifc0&ab_channel=JahmalRichard
- https://discuss.appium.io/t/unable-to-launch-webdriveragent-because-of-xcodebuild-failure-xcodebuild-failed-with-code-65/36126/20

だがしかし、npmでインストールしたパッケージの内容を直接書き換えるような対応方法しかなく、なんだかなーと思っていたら、`appium2`にすれば解決するよ！という説が浮上。

`appium2`をインストール。

```
# npm uninstall appium
$ npm install appium@next --save-dev
```

テストを実行。`XCUITest`のdriverがインストールされてないっぽい。

```
$ npx wdio wdio.conf.ts

[0-0] 2023-06-27T13:27:11.085Z ERROR webdriver: unknown error: An unknown server-side error occurred while processing the command. Original error: Could not find a driver for automationName 'XCUITest' and platformName 'iOS'. Have you installed a driver that supports those capabilities? Run 'appium driver list --installed' to see. (Lower-level error: Could not find installed driver to support given caps)
```

[公式ドキュメント](https://github.com/appium/appium)にしたがってdriverをインストール。

```
$ npx appium driver install --source=npm appium-xcuitest-driver
```

起動してみるとテストが通った。

```
$npx wdio wdio.conf.ts

Execution of 1 workers started at 2023-06-27T13:28:29.113Z

2023-06-27T13:28:29.129Z INFO @wdio/cli:launcher: Run onPrepare hook
2023-06-27T13:28:30.823Z INFO @wdio/cli:launcher: Run onWorkerStart hook
2023-06-27T13:28:30.824Z INFO @wdio/local-runner: Start worker 0-0 with arg: wdio.conf.ts
[0-0] 2023-06-27T13:28:31.285Z INFO @wdio/local-runner: Run worker command: run
[0-0] RUNNING in Safari - file:///test/specs/example.e2e.ts
[0-0] 2023-06-27T13:28:31.482Z INFO webdriver: Initiate new session using the WebDriver protocol
[0-0] 2023-06-27T13:28:31.530Z INFO webdriver: [POST] http://127.0.0.1:4723/session
[0-0] 2023-06-27T13:28:31.530Z INFO webdriver: DATA {
[0-0]   capabilities: {
[0-0]     alwaysMatch: {
[0-0]       platformName: 'iOS',
[0-0]       browserName: 'Safari',
[0-0]       'appium:deviceName': 'iPhone Simulator',
[0-0]       'appium:platformVersion': '16.2',
[0-0]       'appium:automationName': 'XCUITest'
[0-0]     },
[0-0]     firstMatch: [ {} ]
[0-0]   },
[0-0]   desiredCapabilities: {
[0-0]     platformName: 'iOS',
[0-0]     browserName: 'Safari',
[0-0]     'appium:deviceName': 'iPhone Simulator',
[0-0]     'appium:platformVersion': '16.2',
[0-0]     'appium:automationName': 'XCUITest'
[0-0]   }
[0-0] }
[0-0] 2023-06-27T13:29:09.689Z INFO webdriver: COMMAND navigateTo("https://the-internet.herokuapp.com/login")
[0-0] 2023-06-27T13:29:09.689Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/url
[0-0] 2023-06-27T13:29:09.689Z INFO webdriver: DATA { url: 'https://the-internet.herokuapp.com/login' }
[0-0] 2023-06-27T13:29:13.245Z INFO webdriver: RESULT null
[0-0] 2023-06-27T13:29:13.248Z INFO webdriver: COMMAND findElement("css selector", "#username")
[0-0] 2023-06-27T13:29:13.249Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element
[0-0] 2023-06-27T13:29:13.250Z INFO webdriver: DATA { using: 'css selector', value: '#username' }
[0-0] 2023-06-27T13:29:13.274Z INFO webdriver: RESULT {
[0-0]   'element-6066-11e4-a52e-4f735466cecf': ':wdc:1687872553269',
[0-0]   ELEMENT: ':wdc:1687872553269'
[0-0] }
[0-0] 2023-06-27T13:29:13.282Z INFO webdriver: COMMAND elementClear(":wdc:1687872553269")
[0-0] 2023-06-27T13:29:13.282Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element/%3Awdc%3A1687872553269/clear
[0-0] 2023-06-27T13:29:13.302Z INFO webdriver: RESULT null
[0-0] 2023-06-27T13:29:13.303Z INFO webdriver: COMMAND elementSendKeys(":wdc:1687872553269", "<Screenshot[base64]>")
[0-0] 2023-06-27T13:29:13.303Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element/%3Awdc%3A1687872553269/value
[0-0] 2023-06-27T13:29:13.303Z INFO webdriver: DATA { text: 'tomsmith' }
[0-0] 2023-06-27T13:29:13.549Z INFO webdriver: RESULT null
[0-0] 2023-06-27T13:29:13.550Z INFO webdriver: COMMAND findElement("css selector", "#password")
[0-0] 2023-06-27T13:29:13.550Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element
[0-0] 2023-06-27T13:29:13.550Z INFO webdriver: DATA { using: 'css selector', value: '#password' }
[0-0] 2023-06-27T13:29:13.559Z INFO webdriver: RESULT {
[0-0]   'element-6066-11e4-a52e-4f735466cecf': ':wdc:1687872553270',
[0-0]   ELEMENT: ':wdc:1687872553270'
[0-0] }
[0-0] 2023-06-27T13:29:13.561Z INFO webdriver: COMMAND elementClear(":wdc:1687872553270")
[0-0] 2023-06-27T13:29:13.562Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element/%3Awdc%3A1687872553270/clear
[0-0] 2023-06-27T13:29:13.572Z INFO webdriver: RESULT null
[0-0] 2023-06-27T13:29:13.572Z INFO webdriver: COMMAND elementSendKeys(":wdc:1687872553270", "SuperSecretPassword!")
[0-0] 2023-06-27T13:29:13.572Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element/%3Awdc%3A1687872553270/value
[0-0] 2023-06-27T13:29:13.572Z INFO webdriver: DATA { text: 'SuperSecretPassword!' }
[0-0] 2023-06-27T13:29:13.628Z INFO webdriver: RESULT null
[0-0] 2023-06-27T13:29:13.629Z INFO webdriver: COMMAND findElement("css selector", "button[type="submit"]")
[0-0] 2023-06-27T13:29:13.629Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element
[0-0] 2023-06-27T13:29:13.629Z INFO webdriver: DATA { using: 'css selector', value: 'button[type="submit"]' }
[0-0] 2023-06-27T13:29:13.641Z INFO webdriver: RESULT {
[0-0]   'element-6066-11e4-a52e-4f735466cecf': ':wdc:1687872553271',
[0-0]   ELEMENT: ':wdc:1687872553271'
[0-0] }
[0-0] 2023-06-27T13:29:13.645Z INFO webdriver: COMMAND elementClick(":wdc:1687872553271")
[0-0] 2023-06-27T13:29:13.646Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element/%3Awdc%3A1687872553271/click
[0-0] 2023-06-27T13:29:13.670Z INFO webdriver: RESULT null
[0-0] 2023-06-27T13:29:13.672Z INFO webdriver: COMMAND findElement("css selector", "#flash")
[0-0] 2023-06-27T13:29:13.672Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element
[0-0] 2023-06-27T13:29:13.672Z INFO webdriver: DATA { using: 'css selector', value: '#flash' }
[0-0] 2023-06-27T13:29:13.704Z INFO webdriver: RESULT {
[0-0]   error: 'no such element',
[0-0]   message: 'An element could not be located on the page using the given search parameters.',
[0-0]   stacktrace: 'NoSuchElementError: An element could not be located on the page using the given search parameters.\n' +
[0-0]     '    at XCUITestDriver.findWebElementOrElements (/Users/aharada/source/ExpoAppium/node_modules/appium-xcuitest-driver/lib/commands/web.js:435:13)\n' +
[0-0]     '    at async XCUITestDriver.findElOrEls (/Users/aharada/source/ExpoAppium/node_modules/appium-xcuitest-driver/lib/commands/find.js:22:14)\n' +
[0-0]     '    at async XCUITestDriver.findElOrElsWithProcessing (/Users/aharada/source/ExpoAppium/node_modules/@appium/base-driver/lib/basedriver/commands/find.ts:60:12)\n' +
[0-0]     '    at async XCUITestDriver.findElement (/Users/aharada/source/ExpoAppium/node_modules/@appium/base-driver/lib/basedriver/commands/find.ts:75:12)'
[0-0] }
[0-0] 2023-06-27T13:29:13.707Z INFO webdriver: COMMAND findElements("css selector", "#flash")
[0-0] 2023-06-27T13:29:13.707Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/elements
[0-0] 2023-06-27T13:29:13.708Z INFO webdriver: DATA { using: 'css selector', value: '#flash' }
[0-0] 2023-06-27T13:29:13.718Z INFO webdriver: RESULT []
[0-0] 2023-06-27T13:29:14.221Z INFO webdriver: COMMAND findElements("css selector", "#flash")
[0-0] 2023-06-27T13:29:14.222Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/elements
[0-0] 2023-06-27T13:29:14.222Z INFO webdriver: DATA { using: 'css selector', value: '#flash' }
[0-0] 2023-06-27T13:29:14.232Z INFO webdriver: RESULT [
[0-0]   {
[0-0]     'element-6066-11e4-a52e-4f735466cecf': ':wdc:1687872554230',
[0-0]     ELEMENT: ':wdc:1687872554230'
[0-0]   }
[0-0] ]
[0-0] 2023-06-27T13:29:14.236Z INFO webdriver: COMMAND findElement("css selector", "#flash")
[0-0] 2023-06-27T13:29:14.236Z INFO webdriver: [POST] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element
[0-0] 2023-06-27T13:29:14.236Z INFO webdriver: DATA { using: 'css selector', value: '#flash' }
[0-0] 2023-06-27T13:29:14.242Z INFO webdriver: RESULT {
[0-0]   'element-6066-11e4-a52e-4f735466cecf': ':wdc:1687872554230',
[0-0]   ELEMENT: ':wdc:1687872554230'
[0-0] }
[0-0] 2023-06-27T13:29:14.244Z INFO webdriver: COMMAND getElementText(":wdc:1687872554230")
[0-0] 2023-06-27T13:29:14.245Z INFO webdriver: [GET] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5/element/%3Awdc%3A1687872554230/text
[0-0] 2023-06-27T13:29:14.255Z INFO webdriver: RESULT You logged into a secure area!
[0-0] ×
[0-0] 2023-06-27T13:29:14.257Z INFO webdriver: COMMAND deleteSession()
[0-0] 2023-06-27T13:29:14.257Z INFO webdriver: [DELETE] http://127.0.0.1:4723/session/918e97eb-a852-4e4d-a819-f99a31f73ac5
[0-0] 2023-06-27T13:29:15.352Z INFO webdriver: RESULT null
[0-0] PASSED in Safari - file:///test/specs/example.e2e.ts
2023-06-27T13:29:15.475Z INFO @wdio/cli:launcher: Run onWorkerEnd hook
2023-06-27T13:29:15.475Z INFO @wdio/cli:launcher: Run onComplete hook

 "spec" Reporter:
------------------------------------------------------------------
[Safari iOS #0-0] Running: Safari on iOS
[Safari iOS #0-0] Session ID: 918e97eb-a852-4e4d-a819-f99a31f73ac5
[Safari iOS #0-0]
[Safari iOS #0-0] » /test/specs/example.e2e.ts
[Safari iOS #0-0] My Login application
[Safari iOS #0-0]    ✓ should login with valid credentials
[Safari iOS #0-0]
[Safari iOS #0-0] 1 passing (5.6s)


Spec Files:	 1 passed, 1 total (100% completed) in 00:00:46

2023-06-27T13:29:15.476Z INFO @wdio/local-runner: Shutting down spawned worker
2023-06-27T13:29:15.728Z INFO @wdio/local-runner: Waiting for 0 to shut down gracefully
2023-06-27T13:29:15.728Z INFO @wdio/local-runner: shutting down
```

が、初期生成されているテストの内容が、ブラウザでどこかのWEBサイトを開いて要素をチェックしているコードだったので、アプリをテストするコードに書き換える。

```
// describe('My Login application', () => {
//     it('should login with valid credentials', async () => {
//         await browser.url(`https://the-internet.herokuapp.com/login`)

//         await $('#username').setValue('tomsmith')
//         await $('#password').setValue('SuperSecretPassword!')
//         await $('button[type="submit"]').click()

//         await expect($('#flash')).toBeExisting()
//         await expect($('#flash')).toHaveTextContaining(
//             'You logged into a secure area!')
//     })
// })

describe('test', () => {
    it('test hoge', () => {
        $('~TestContainer').waitForDisplayed({ timeout: 60000 });
        expect($('~TestContainer').isExisting()).to.be.true;
    });
});
```

再度テストを実行。

```
$ npx wdio wdio.conf.ts
```

Let's browse!ですって。なんだそりゃ。どっからこの画面出てきとるんや。

![Simulator Screen Shot - appiumTest-A257001C-8684-4FA7-819A-CF5CBD5CDAF7-iPhone X - 2023-06-27 at 22.50.39.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/47677/c0a48370-b590-df2d-30a4-5d9f9b86aefb.png)

とりあえず今日はもう時間なのでここまでとする。アプリが開く気配がないので、何か根本的にやろうとしていることが間違っている気がする。続きはまた後日。

次はこの辺をチェックしてみる。
https://dev.to/redclouds/simplest-way-to-automatic-test-your-react-native-app-with-appium-4j59