Simplest way to Automatic test your React Native app with Appium
Tags: appium, reactnative, expo
Published: 2021-06-18 09:53:05

You must hear about automation test . The instruction you found with google do not statisfy you . Ok . Here is the shortest one to help you test with the Appium for React Native . Pls notice , this tutorial is for the short test and fastest ways to test , not provide fully document about all kind of test aspect . 

Ok lets go . 

## First . Download Appium 
Come [here](https://github.com/appium/appium-desktop/releases/tag/v1.21.0) and get yoursefl a desktop version of Appium .

## Then . Open it ( Appium )
Open the appium app , run the server. Note the port , here is 4723
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7itaujn3bhdu4sv0cal.png)
Press Start Server.

## Write the code .
You will need webdriverio,jest and jasmine install
```
yarn add -D webdriverio jest jasmine
```
put this in appium/TestAppium.test.js ( or any directory if you know Jest )

```javascript
import { remote } from "webdriverio";

// eslint-disable-next-line no-undef
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000;
let driver;

beforeAll(async () => {
  driver = await remote({
    path: "/wd/hub",
    host: "localhost",
    port: 4723,
    capabilities: {
      platformName: "iOS",
      platformVersion: "14.5", // must correct the stimuator
      deviceName: "iPhone 12 Pro Max", // must correct the stimuator
      appium: { connectHardwareKeyboard: true }
      // automationName: "XCUITest",
      // app: "org.reactjs.native.example.LearnRnE2eTest", // this is for open specify app
      // udid: process.env.IOS_DEVICE_UUID,
      // xcodeOrgId: "xxx",
      // xcodeSigningId: "Apple Development"
    }
    // logLevel: "silent"
  });
});

afterAll(async () => {
  if (driver) {
    await driver.deleteSession();
  }
});
test("Login test", async () => {
  await driver.pause(2000);

  const loginUsernameInput = await driver.$("~LoginInput");
  await loginUsernameInput.clearValue();
  await loginUsernameInput.setValue("TestAccount@test.com");
  await driver.hideKeyboard("pressKey", "next");

  const loginNextButton = await driver.$("~LoginNextButton");
  await loginNextButton.waitForExist();
  loginNextButton.click();
  await driver.pause(3000);

  const loginPasswordInput = await driver.$("~LoginPasswordInput");
  await loginPasswordInput.setValue("Test1234");
  const loginButton = await driver.$("~LoginButton");
  await loginButton.click();
  await driver.pause(3000);
});
```

Input device setting must correct the emulator 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/no5lm7urlrwqthhjodce.png)
 

To access the id like LoginButton , you must set it up in React Native 
```javascript
<GreenButton
  testID={"LoginNextButton"}
  title={'Login'}
/>
```
testID with work on iOS

## Run code with Jest

Ok . In conclusion , you will need those things to run 
1. Appium started
2. Your app run on emulator ( through expo , react native metro (dev mode ) , stimulator app run on , ipa install on emulator ... any kind , just run the app , doesn't matter .. )
3. Jest ready

Then run the test with jest

```
jest appium
```

You can run jest only ( without appium , I put appium here beacause I put my test file in appium folder ).

And that is all . If you need more instructions , pls comment below so I can answer or make another tutorial for this .