Showing posts with label appium. Show all posts
Showing posts with label appium. Show all posts

Saturday, July 9, 2022

Setup and run Appium for Android with Java client on Linux

How to set up and run Appium for android apps with Java client with Emulator setup.
1. Introduction:

Appium is an open-source automation tool for mobile applications. It is a cross-platform tool that is used to automate native, hybrid, and mobile web applications running on ios, android, and windows platforms. In this tutorial, we are going to set up Appium and run a sample example.


2. prerequisites:

  1. Installed JDK
  2. NodeJs
3. Download and run Appium:

Download the latest version of the Appium desktop App Image from here. Under "Assets" download AppImage for Linux. Go to download directory and open terminal. Give permission for the app image file.

 
chmod a+x

Now, execute the AppImage file either by double-clicking it or running the following command.
./AppImage
You can see as the Appium Ui started similar to this.


Now, start the Appium server with default port 4723 by clicking the "Start Server" button.




4. Setup Android Emulator:

In order to set up an Android Emulator, you can set it up via a command-line tool without installing an android studio but here, we are going to download an android studio and set up android SDK to run Emulator. For this download android studio from here. Extract the downloaded tar file.
 
tar -xvf android-studio.tar.gz

Go to the extracted directory cd to the bin directory you can see the studio.sh simply execute this sh file by opening the terminal. Which will popup the android studio setting import option. Select "Do not import settings". Click "Next", select "Standard" and verify the setting. Make sure to verify the path of SDK installed.


Click "Next" and "Finish" which will download the SDK on the SDK folder mentioned.

Now, we need to configure the android virtual device for this click "Configure" and select "AVD Manager".


If you see "dev/kvm device: permission denied" you can give permission for the user by using the command:
sudo chown yourLinuxUser /dev/kvm
Create a Virtual Device







After that, select the downloaded image and click "Next" and verify android virtual device configuration and click "Finish" to finish the virtual device setup. Now you can launch the virtual device in the Emulator.



Now, let's configure the SDK path in our system. For this open bashrc file by using the command:
 
  sudo gedit ~/.bashrc

Add the following line at the end of the file.
 
  export ANDROID_HOME=/home/kchapagain/Downloads/Sdk
export PATH=$ANDROID_HOME/emulator:$ANDROID_HOME/tools:$PATH
export PATH=$PATH:$ANDROID_HOME/platform-tools
Make sure you provided the correct download SDK path in "ANDROID_HOME" in my case it's "/home/kchapagain/Downloads/Sdk". Save the file and reload the changes by using the command:
 
  source ~/.bashrc

You can launch the device on Emulator via the command line so that you don't need to open the android studio each time you run the device. For this, open the terminal use command:
emulator -list-avds
Output:
Pixel_2_API_28
Pixel_2_API_28_2

Here, we have two virtual devices installed in your case it will output the list of devices downloaded.

Now, run the device on the Emulator using command:
emulator -avd Pixel_2_API_28
User your device name instead "Pixel_2_API_28". It will run the android virtual device on Emulator.

 
5. Download and load library for Java Client:

Download the Appium java client jar from here.



Similarly, download JUnit jar from here.  Download selenium from here.

Create a java project and create a package called "libs" under "src". Copy all the downloaded jar file in this package and load the jar file from Ide.

For IntelliJ Idea:

Go to: File >> Project Structure(Ctr + Alt + Shift + s) >> Libraries and click the + icon on the top left corner and select package.


6. Setup driver config for Appium:

Create a sample class to config the android driver. Here, we are using the sample calculator app to test and the driver setup config looks like below:
 @Before
    public static void setUp() throws MalformedURLException {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("deviceName", "50b6ab0");
        cap.setCapability("appPackage", "");
        cap.setCapability("appActivity", "");
        cap.setCapability("automationName", "UiAutomator1");
        cap.setCapability("autoGrantPermissions", true);
        cap.setCapability("autoAcceptAlerts", "true");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    }
Share:

Sunday, June 14, 2020

Appium how to do scrolling.

In this post, I will show you how to do scrolling in Appium. Appium provides the TouchAction API for gesture implementation.

1. Create an Appium driver:

First, let's create an Appium driver. The general configuration looks like as below:

import io.appium.java_client.android.AndroidDriver;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.net.MalformedURLException;
import java.net.URL;

public class AppiumTest {

    private static AndroidDriver driver;
    private static WebDriverWait wait;

    @Before
    public static void setUp() throws MalformedURLException {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("deviceName", "your device name");
        cap.setCapability("appPackage", "appPackage");
        cap.setCapability("appActivity", "appActivity");
        cap.setCapability("automationName", "UiAutomator1");
        cap.setCapability("autoGrantPermissions", true);
        cap.setCapability("autoAcceptAlerts", "true");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        wait = new WebDriverWait(driver,60);
    }
    
    @After
    public static void tearDown(){
        driver.quit();
    }

}
This is a sample config to create an Appium driver which we will use later.





2. Scrolling:

//AppiumTest.java
private static void scroll(int scrollStart, int scrollEnd) {
        new TouchAction(driver)
                .press(point(0, scrollStart))
                .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(10)))
                .moveTo(point(0, scrollEnd))
                .release().perform();
    }
Here, we are dealing with scrolling, so we are adjusting the value for the y-axis. The press() method allows you to press on the position x, y coordinates. You can use some offset value for x coordinate instead of 0 x-offset value. The waitAction() method will wait until the duration provided. The moveTo() method allows moving current touch action to a new position specified. The release() method removes the touch. Next, we will adjust the scrollStart and scrollEnd arguments to move down and up.

3. Scrolling Down:


//AppiumTest.java
public static void scrollDown() {
        MobileElement element = (MobileElement) driver.findElement(By.id("resourceId"));
        if (element == null){
            return;
        }
        int numberOfTimes = 10;
        Dimension dimension = driver.manage().window().getSize();
        int windowsHeight = dimension.getHeight();
        int scrollStart = (int) (windowsHeight * 0.5);
        int scrollEnd = (int) (windowsHeight * 0.3);
        int elementLocationOffset = windowsHeight-500;
        for (int i = 0; i < numberOfTimes; i++) {
            int elementLocationY = element.getLocation().y;
            if (elementLocationY < elementLocationOffset){
                i = numberOfTimes;
                System.out.println("Element available.");
            }else {
                scroll(scrollStart, scrollEnd);
                System.out.println("Element not available. Scrolling...");
            }
        }
    }

In the above scrolling down example, we are trying to scroll down until the element is visible on the screen. Here, we are getting the dimension of the screen window; as we are scrolling so, we use windows height to manipulate the position to scroll.




For e.g, if windows height is 1000 scrollStart will be 500 and scrollEnd will be 300 which means while calling scroll() method it will press to the position (x,y) (0, 500) and move to (x,y)(0, 300) which results in scrolling downward. We are looping so that it will scroll down until the element will arrive to the elementLocationOffset position where the element will be visible on the screen.

For testing element availability, if the above approach doesn't work you can try finding the element as below:

for (int i = 0; i < numberOfTimes; i++) {
            List elements = driver.findElements(By.id("resourceId"));
            if (elements.size() > 0){
                i = numberOfTimes;
                System.out.println("Element available.");
            }else {
                scroll(scrollStart, scrollEnd);
                System.out.println("Element not available. Scrolling...");
            }
        }

4. Scrolling Up:

If you want to scroll Up then you need to adjust the coordinates as below:

 int scrollStart = (int) (windowsHeight * 0.3);
 int scrollEnd = (int) (windowsHeight * 0.7);
 
For e.g: if the window's height is 1000 then scrollStart will be 300 and scrollEnd will be 700 which means while calling scroll() method it will press to the position (x,y) (0, 300) and move to (x,y)(0, 700) which results in scrolling upward. Adjust the value that suits you.

The overall Implementation looks like below:

package appium;

import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.touch.WaitOptions;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;

import static io.appium.java_client.touch.offset.PointOption.point;


public class AppiumTest {

    private static AndroidDriver driver;
    

    @Before
    public static void setUp() throws MalformedURLException {
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("platformName", "Android");
        cap.setCapability("deviceName", "your device");
        cap.setCapability("appPackage", "appPackage");
        cap.setCapability("appActivity", "appActivity");
        cap.setCapability("automationName", "UiAutomator1");
        cap.setCapability("autoGrantPermissions", true);
        cap.setCapability("autoAcceptAlerts", "true");
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
    }


    public static void scrollDown() {
        MobileElement element = (MobileElement) driver.findElement(By.id("resourceId"));
        if (element == null){
            return;
        }
        int numberOfTimes = 10;
        Dimension dimension = driver.manage().window().getSize();
        int windowsHeight = dimension.getHeight();
        int scrollStart = (int) (windowsHeight * 0.5);
        int scrollEnd = (int) (windowsHeight * 0.3);
        int elementLocationOffset = windowsHeight-500;
        for (int i = 0; i < numberOfTimes; i++) {
            int elementLocationY = element.getLocation().y;
            if (elementLocationY < elementLocationOffset){
                i = numberOfTimes;
                System.out.println("Element available.");
            }else {
                scroll(scrollStart, scrollEnd);
                System.out.println("Element not available. Scrolling...");
            }
        }
    }

    private static void scroll(int scrollStart, int scrollEnd) {
        new TouchAction(driver)
                .press(point(0, scrollStart))
                .waitAction(WaitOptions.waitOptions(Duration.ofSeconds(1)))
                .moveTo(point(0, scrollEnd))
                .release().perform();
    }

    @After
    public static void tearDown(){
        driver.quit();
    }

}




Share: