DEV Community

Khairun Nahar Nowrin
Khairun Nahar Nowrin

Posted on

2

Appium automation code structure

Desired Capabilities: Desired capabilities are a set of key-value pairs that specify the characteristics of the device or emulator to be used for testing. This includes details such as device name, platform version, app package name, and app activity name. These capabilities are used to initialize the driver object.

    @BeforeTest
    public void setup() throws Exception {
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("deviceName", "Android Emulator");
        caps.setCapability("platformVersion", "11");
        caps.setCapability("appPackage", "com.example.myapp");
        caps.setCapability("appActivity", "com.example.myapp.MainActivity");
        caps.setCapability("automationName", "UiAutomator2");
        caps.setCapability("noReset", true);

        driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"), caps);
    }
Enter fullscreen mode Exit fullscreen mode

Page Objects: Page objects are classes that encapsulate the behavior and properties of the user interface elements of an app. They provide a layer of abstraction between the test code and the app's user interface. Each screen or page of the app can have a corresponding page object class.

Test Code: The test code is written to interact with the user interface elements of the app through the page objects. It typically includes the steps that the user takes to navigate through the app, interact with the elements, and perform validations.

    @Test
    public void loginTest() {
        LoginPage loginPage = new LoginPage(driver);
        loginPage.enterUsername("myusername");
        loginPage.enterPassword("mypassword");
        loginPage.clickLoginButton();

        HomePage homePage = new HomePage(driver);
        Assert.assertTrue(homePage.isUserLoggedIn());
    }
Enter fullscreen mode Exit fullscreen mode

Test Runner: The test runner is a program that runs the test code and communicates with the Appium server to automate the app. There are different test runners available for Appium, including TestNG, JUnit, and Appium Studio. The test runner usually handles the configuration, setup, and execution of the tests.

Supporting files: These are files that contain additional information or configuration for the tests, such as test data, configuration files, or utility classes. They can be organized in folders for better structure and maintainability.

- src
  - main
    - java
      - com
        - example
          - pages
            - LoginPage.java
            - HomePage.java
          - tests
            - LoginTest.java
          - utils
            - AppiumUtils.java
    - resources
      - app
        - MyApp.apk
  - test
    - java
      - com
        - example
          - runners
            - TestRunner.java

Enter fullscreen mode Exit fullscreen mode

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

👋 Kindness is contagious

If you found this article helpful, a little ❤️ or a friendly comment would be much appreciated!

Got it