Monday, September 2, 2019

Appium - How to run tests parallel on Appium | Mobile Automation


Appium - How to run tests parallel on Appium | Mobile Automation

 Let us learn how to run tests parallel using appium. here i have used 2 emulators, testng, appium & Maven.

Prerequisites:

1. Appium setup should be completed. In case you are new , please follow these steps to do "Appium setup"

Steps:

1. Create maven project. In case you are new to maven project creation follow this post How to create maven project for automation

2. Post maven project creation, project structure should like below:









3. Open the “pom.xml” file add the below maven dependencies:

Appium & TestNg dependency:
            <dependencies>
               
                         <dependency>
                                    <groupId>io.appium</groupId>
                                    <artifactId>java-client</artifactId>
                                    <version>7.0.0</version>
                        </dependency>

                         <dependency>
                                    <groupId>org.testng</groupId>
                                    <artifactId>testng</artifactId>
                                    <version>6.9.13.6</version>
                        </dependency>
            </dependencies>
               

4. Now POM looks like below:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>POMAutomation</groupId>
  <artifactId>POMAutomation</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>POMAutomation</name>
  <url>http://maven.apache.org</url>

  <properties>
             <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
       </properties>

       <repositories>
             <repository>
                    <id>spring-releases</id>
                    <name>Spring Releases</name>
                    <url>https://repo.spring.io/libs-release</url>
             </repository>
       </repositories>
       <pluginRepositories>
             <pluginRepository>
                    <id>spring-releases</id>
                    <name>Spring Releases</name>
                    <url>https://repo.spring.io/libs-release</url>
             </pluginRepository>
       </pluginRepositories>
       <dependencies>


             <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>3.8.1</version>
                    <scope>test</scope>
             </dependency>

             <dependency>
                    <groupId>io.appium</groupId>
                    <artifactId>java-client</artifactId>
                    <version>7.0.0</version>
             </dependency>

             <dependency>
                    <groupId>org.testng</groupId>
                    <artifactId>testng</artifactId>
                    <version>6.9.13.6</version>
             </dependency>
             <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-math3</artifactId>
                    <version>3.2</version>
             </dependency>

             <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-tx</artifactId>
                    <version>3.2.2.RELEASE</version>
             </dependency>
             <dependency>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-orm</artifactId>
                    <version>3.2.2.RELEASE</version>
             </dependency>

             <dependency>
                    <groupId>commons-lang</groupId>
                    <artifactId>commons-lang</artifactId>
                    <version>2.6</version>
             </dependency>

             <dependency>
                    <groupId>org.apache.commons</groupId>
                    <artifactId>commons-lang3</artifactId>
                    <version>3.1</version>
             </dependency>

             <dependency>
                    <groupId>com.relevantcodes</groupId>
                    <artifactId>extentreports</artifactId>
                    <version>2.41.2</version>
             </dependency>

             <dependency>
                    <groupId>org.apache.poi</groupId>
                    <artifactId>poi</artifactId>
                    <version>4.0.0</version>
             </dependency>
            
             <!-- https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -->
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.17</version>
</dependency>


       </dependencies>

<build>
             <plugins>
                    <!-- Compiler plug-in -->
                    <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-compiler-plugin</artifactId>
                           <configuration>
                                 <source>${jdk.level}</source>
                                 <target>${jdk.level}</target>
                           </configuration>
                    </plugin>
                    <!-- Below plug-in is used to execute tests -->
                    <plugin>
                           <groupId>org.apache.maven.plugins</groupId>
                           <artifactId>maven-surefire-plugin</artifactId>
                           <version>2.18.1</version>
                           <configuration>
                                 <suiteXmlFiles>
                                        <!-- TestNG suite XML files -->
                                        <suiteXmlFile>testng.xml</suiteXmlFile>
                                 </suiteXmlFiles>
                           </configuration>
                    </plugin>
             </plugins>
       </build>

</project>



5. Create “baseClass” package: right click on your project and select New – Package and give name as “baseClass” and click on finish.


6. Then right click on “baseClass” package and select New -> Class and name as “BaseClass.java” .


7. After this step project structure should look like below:










8. Open “BaseClass.java” and add the below code:
package baseClass;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;

public class BaseClass {

            public static AppiumDriver<MobileElement> driver;
            @BeforeTest
            @Parameters({"deviceName","udid", "platformVersion","url"})
            public void setup(String deviceName, String udid,String platformVersion,String url) throws InterruptedException, MalformedURLException
            {
                        DesiredCapabilities cap=new DesiredCapabilities();
                cap.setCapability("deviceName", deviceName);
                cap.setCapability("udid", udid);
                cap.setCapability("appActivity", "com.android.calculator2.Calculator");
                cap.setCapability("appPackage", "com.android.calculator2");
                cap.setCapability("platformName", "Android");
                cap.setCapability("platformVersion", platformVersion);
                driver=new AppiumDriver<MobileElement>(new URL(url), cap);
                driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
                Thread.sleep(5000);
              
            }

}



9. Create “pages” package: right click on your project and select New – Package and give name as “pages” and click on finish.

10. Then right click on “pages” package and select New -> Class and name as “BasePOMpage.java” .

11. After adding above step, project structure should look like below:






12. Open “BasePOMpage.java” and add below code and save it:
package pages;

import org.openqa.selenium.support.PageFactory;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;

public class BasePOMpage {

public AppiumDriver<MobileElement> driver;
   
    public BasePOMpage(AppiumDriver<MobileElement> driver) {
        this.driver = driver;
        PageFactory.initElements(new AppiumFieldDecorator(driver), this);
    }
   
}



13. Then right click on “pages” package and select New -> Class and name as “CalculatorScreen.java” .




14. After this step project structure looks like below:






15. Open “CalculatorScreen.java” and add below code and save it:

package pages;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.pagefactory.AndroidFindBy;


public class CalculatorScreen  extends BasePOMpage{
           


    @AndroidFindBy(id = "com.android.calculator2:id/digit_1")
    private AndroidElement button1;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_2")
    private AndroidElement button2;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_3")
    private AndroidElement button3;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_4")
    private AndroidElement button4;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_5")
    private AndroidElement button5;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_6")
    private AndroidElement button6;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_7")
    private AndroidElement button7;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_8")
    private AndroidElement button8;
    
    @AndroidFindBy(id = "com.android.calculator2:id/digit_9")
    private AndroidElement button9;
   
    @AndroidFindBy(id = "com.android.calculator2:id/digit_0")
    private AndroidElement button0;
   
    @AndroidFindBy(id = "com.android.calculator2:id/op_add")
    private AndroidElement buttonPlus;
   
    @AndroidFindBy(id = "com.android.calculator2:id/eq")
    private AndroidElement buttonEqual;

    public CalculatorScreen(AppiumDriver<MobileElement> driver) {
        super(driver);
    }
   
    public void clickButton_1()
    {
     
            button1.click();
    }
   
    public void clickButton_2()
    {
     
            button2.click();
    }
   
    public void clickPlusButton()
    {
            buttonPlus.click();
    }
}


16. Create “testScripts” package: right click on your project and select New – Package and give name as testScripts” and click on finish.

17. Then right click on “testScripts” package and select “New -> Class and name as “TestScript1.java” .


18. After this step project structure looks like below:




19. Open “TestScript1.java” and add below code and save it:
package testScripts;

import org.testng.annotations.Test;

import baseClass.BaseClass;
import pages.CalculatorScreen;

public class TestScript1 extends BaseClass {

            @Test
            public void setup() throws InterruptedException {

                        CalculatorScreen cs = new CalculatorScreen(driver);
                        cs.clickButton_1();
                        cs.clickPlusButton();
                        cs.clickButton_2();
                        Thread.sleep(5000);
                       
            }
}



20. Right click on your project and select New – File and name it as “testing.xml” and click on Finish





21. Add the below code in testing.xml file.
<suite name="parallel" parallel="Methods" thread-count="2">
       <test name="Device-1">
             <parameter name="deviceName" value="emulator-5554" />
             <parameter name="udid" value="emulator-5554" />
        <parameter name="platformVersion" value="9.0" />
        <parameter name="url" value="http://0.0.0.0:4724/wd/hub" />
             <classes>
                    <class name="testScripts.TestScript1"/>
             </classes>
       </test>
       <test name="Device-2">
             <parameter name="deviceName" value="emulator-5556" />
             <parameter name="udid" value="emulator-5556" />
        <parameter name="platformVersion" value="8.0" />
        <parameter name="url" value="http://127.0.0.2:4723/wd/hub" />
             <classes>
                    <class name="testScripts.TestScript1"/>
             </classes>
       </test>
</suite>


22. Now your project structure looks like below:



23. Open one Appium Desktop instance and set  Host as “0.0.0.0” and Port as “4723”


24. Click on “Start Server” button :



25. Open another Appium Desktop instance and set Host as “127.0.0.2” and Port as “4725”



26. Click on “Start Server” button:




27. Open two emulators or connect two android devices to your system.

Open 1st emulator :



Open 2nd Emulator :


28. Open command prompt and run “adb devices” command:



29. Take those devices name and update in testng.xml file as below:

<suite name="parallel" parallel="Methods" thread-count="2">
       <test name="Device-1">
             <parameter name="deviceName" value="emulator-5554" />
             <parameter name="udid" value="emulator-5554" />
        <parameter name="platformVersion" value="9.0" />
        <parameter name="url" value="http://0.0.0.0:4723/wd/hub" />
             <classes>
                    <class name="testScripts.TestScript1"/>
             </classes>
       </test>
       <test name="Device-2">
             <parameter name="deviceName" value="emulator-5556" />
             <parameter name="udid" value="emulator-5556" />
        <parameter name="platformVersion" value="8.0" />
        <parameter name="url" value="http://127.0.0.2:4725/wd/hub" />
             <classes>
                    <class name="testScripts.TestScript1"/>
             </classes>
       </test>
</suite>


30. Click on “testng.xml” file and select run as -> testng suite


31. After execution Output on 1st emulator:



32. After execution Output on 2nd emulator:



33. Console Output:


34. TestNg output:



Note: Make sure you are giving correct appium server address and your device details correctly in the testng.xml file. 

If any issues let me know.


No comments:

Post a Comment

If any suggestions or issue, please provide