Tue. Dec 10th, 2019

The best way to Automate Appium Java Exams In Parallel the use of TestNG

6 min read

The wonderful thing about Appium for cell trying out is that its assessments will also be written in any programming language together with Python , Ruby , Java , JavaScript and C# . Whilst we’ve got coated intensive Appium tutorials , on this weblog publish we’ll stroll thru find out how to automate Appium assessments in parallel towards our actual units the use of TestNG Java pattern assessments (you’ll in finding our Appium JUnit sample for parallel testing here ).

What’s and Why to Use TestNG?

TestNG , the place NG denotes Subsequent Technology, is an automatic trying out framework this is impressed via each JUnit and NUnit.

It’s designed to simplify quite a lot of trying out wishes together with unit trying out, purposeful trying out and integration trying out. And it offers cell builders and testers the facility to jot down extra versatile and robust assessments.

Advantages of the use of TestNG

TestNG is very similar to JUnit however introduces some new options that make it extra tough and more uncomplicated to make use of. Underneath are one of the advantages Appium Java customers can experience with TestNG:

  • Extra annotations: TestNG has an in depth collection of annotations. See a full list of annotations available in TestNG .
  • Check Grouping: The teams characteristic with the @Check annotation eases the grouping of check circumstances.
  • Check Prioritization: The use of the precedence characteristic to set other priorities for each and every check case.
  • Parameterized Exams: The use of parameterized assessments makes it simple to run the similar check again and again the use of other values.
  • Parallel Checking out: It’s conceivable and simple to parallelize check execution with the parallel characteristic. We’re going to make use of it in our pattern under.

The Fundamental Setup for Operating Appium Exams in Java The use of TestNG

To start with, to run assessments concurrently on other units, you need to use ThreadLocal in an effort to save a number of drivers at one time.

In case you use just one motive force example, the check shall be run at the one tool most effective for the reason that diver will use the closing created consultation.

In the beginning, you must create the ThreadLocal object:

personal static ThreadLocal motive force = new ThreadLocal<>();

Then, it’s a must to upload the motive force in ThreadLocal the use of the motive force.set(…) way:

motive force.set(new RemoteWebDriver(new URL("remote_hub_url_here"), features));

And now, if you wish to get the motive force which is lively in this day and age, you’ll use the motive force.get() way:

public static WebDriver getDriver() {
  go back motive force.get();
}

Coping with Appium Desired Functions

To be able to run RemoteWebDriver, you must set up the wanted features. They may be able to range relying at the tool the place the check must be carried out. Most commonly, experts use the fundamental ones, equivalent to deviceName, platformName, browserName, platformVersion.

Right here we’ve got features which are had to run the assessments on Bitbar cloud for Android units:

DesiredCapabilities features = new DesiredCapabilities();
features.setCapability("bitbar_apiKey", );
features.setCapability("bitbar_testrun", "Bitbar check run");
features.setCapability("bitbar_project", "Bitbar check mission");
features.setCapability("bitbar_device", bitbarDevice);
features.setCapability("browserName", browserName);
features.setCapability("platformName", platformName);
features.setCapability("deviceName", deviceName);

Homes shall be as follows:

bitbar_device = Google Pixel XL
browser_name = chrome
platform_name = Android
device_name = Android Telephone

Within the case with iOS units, it’s a must to upload another capacity:

features.setCapability("automationName", automationName);

Homes document shall be like this:

bitbar_device = iPhone7 11.3.1 (ios02)
browser_name = safari
platform_name = iOS
device_name = iPhone tool
automation_name = XCUITest

The overall configuration of features would be the following:

DesiredCapabilities features = new DesiredCapabilities();
features.setCapability("bitbar_apiKey", );
features.setCapability("bitbar_testrun", this.getClass().getSimpleName());
features.setCapability("bitbar_project", xmlTestName);
features.setCapability("bitbar_device", bitbarDevice);
features.setCapability("browserName", browserName);
features.setCapability("platformName", platformName);
features.setCapability("deviceName", deviceName);
if (automationName != null)
   features.setCapability("automationName", automationName);

motive force.set(new RemoteWebDriver(new URL("https://appium.bitbar.com/wd/hub/"), features));

To get extra details about wanted features for more than a few units, please talk over with Bitbar Capabilities Creator .

Get Began with a Easy Appium Check The use of Java TestNG

Now let’s create a easy Google seek check:

import base.BaseTest;
import org.openqa.selenium.By means of;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Check;

import java.util.Checklist;

public magnificence SimpleGoogleTest extends BaseTest {

  personal static ultimate String GOOGLE_SEARCH_URL = "https://www.google.com/";

  personal static By means of searchInput = By means of.cssSelector("[name='q']");
  personal static By means of searchButton = By means of.cssSelector(".Tg7LZd");

  personal static By means of searchResultTable = By means of.identification("cnt");
  personal static By means of searchResultItemHeader = By means of.cssSelector("#rso div.zlBHuf");

  @Check
  public void searchForBitbar() throws InterruptedException {

    // Get motive force example
    WebDriver motive force = BaseTest.getDriver();

    // Open major Google Seek web page
    motive force.get(GOOGLE_SEARCH_URL);
    Assert.assertEquals(motive force.getCurrentUrl(), GOOGLE_SEARCH_URL);

    // Enter "Bitbar" seek time period, click on on seek button and stay up for web page to be loaded
    motive force.findElement(searchInput).sendKeys("Bitbar");
    motive force.findElement(searchButton).click on();
    Thread.sleep(2000);
    // Test that Seek end result desk provide with seek effects
    Assert.assertTrue(motive force.findElements(searchResultTable).dimension() > 0);

    // Test that no less than one seek end result merchandise accommodates phrase "Bitbar"
    Checklist resultItems = motive force.findElements(searchResultItemHeader);
    boolean isPresent = false;
    for (WebElement merchandise : resultItems) {
      if (merchandise.getText().accommodates("Bitbar"))
        isPresent = true;
    }
  Assert.assertTrue(isPresent);
  }
}

Additionally, we will be able to create a check suite to run two assessments concurrently:





  
    
    
      
    
  

  
    
    
      
    
  

Report with motive force configurations shall be as follows:

bundle base;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.far flung.DesiredCapabilities;
import org.openqa.selenium.far flung.RemoteWebDriver;
import org.testng.ITestContext;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import utils.Constants;
import utils.Reporter;

import java.io.FileInputStream;
import java.io.IOException;
import java.web.MalformedURLException;
import java.web.URL;
import java.util.Homes;
import java.util.concurrent.TimeUnit;

public magnificence BaseTest {

  personal static ThreadLocal motive force = new ThreadLocal<>();

  personal String deviceName;
  personal String bitbarDevice;
  personal String browserName;
  personal String platformName;
  personal String automationName;

  @BeforeMethod
  @Parameters(worth = {"Instrument"})
  public void setUp(String tool, ITestContext context) throws MalformedURLException {
    setupRemoteDriver(tool, context.getCurrentXmlTest().getName());

    getDriver().arrange().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  }

  personal void setupRemoteDriver(String tool, String xmlTestName) throws MalformedURLException {
    loadProperties(tool);

    DesiredCapabilities features = new DesiredCapabilities();
    features.setCapability("bitbar_apiKey", );
    features.setCapability("bitbar_testrun", this.getClass().getSimpleName());
    features.setCapability("bitbar_project", xmlTestName);
    features.setCapability("bitbar_device", bitbarDevice);
    features.setCapability("browserName", browserName);
    features.setCapability("platformName", platformName);
    features.setCapability("deviceName", deviceName);
    if (automationName != null)
      features.setCapability("automationName", automationName);

    motive force.set(new RemoteWebDriver(new URL("https://appium.bitbar.com/wd/hub/"), features));
  }

  personal void loadProperties(String tool) {
    FileInputStream propertiesFIS;
    Homes houses = new Homes();
    String propertiesFilePath = String.structure("src/check/sources/houses/%s.houses", tool);

    take a look at {
      propertiesFIS = new FileInputStream(propertiesFilePath);
      houses.load(propertiesFIS);

      bitbarDevice = houses.getProperty("bitbar_device");
      browserName = houses.getProperty("browser_name");
      platformName = houses.getProperty("platform_name");
      deviceName = houses.getProperty("device_name");
      if (houses.getProperty("automation_name") != null)
      automationName = houses.getProperty("automation_name");

    } catch (IOException e) {
      Reporter.err("Homes document is lacking or invalid! Test trail to document: " + propertiesFilePath);
      Device.go out(0);
    }
  }

  static WebDriver getDriver() {
    go back motive force.get();
  }

  @AfterMethod
  public void tearDown() {
    if (getDriver() != null)
      getDriver().give up();
  }
}

Conclusion

TestNG is a smart trying out framework of selection for Appium assessments written in Java. When you’ve got carried out your Appium TestNG assessments and are searching for a mobile app testing platform for automating it in parallel, try to run your tests on Bitbar Cloud .

The publish How to Automate Appium Java Tests In Parallel using TestNG seemed first on Bitbar .

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © All rights reserved. | Newsphere by AF themes.