All Downloads are FREE. Search and download functionalities are using the official Maven repository.

android.TestNGMethods Maven / Gradle / Ivy

There is a newer version: 1.3.0
Show newest version
package android;

import core.annotations.AttributesConfig;
import core.files.StringHandler;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.*;

import java.lang.reflect.Method;

import static android.AndroidDriverInitializer.*;
import static core.files.FileHelper.getFileAbsolutePath;
import static core.files.reader.ReadPropFile.getProperty;

/**
 * Created by Ismail on 6/3/2018.
 */
public class TestNGMethods extends core.TestNGMethods {

    /*************** Class Variables Section ***************/
    AttributesConfig attributesConfig = new AttributesConfig();

    /*************** Class Methods Section ***************/
    // This method run automatically before each TestNG Suite
    @BeforeSuite(alwaysRun = true)
    public void setUpSuite(ITestContext testContext) {
        // Run Super class method
        super.setUpSuite(testContext);
    }

    // This method run automatically after each TestNG Suite
    @AfterSuite(alwaysRun = true)
    public void tearDownSuite() {
        // Run Super class method
        super.tearDownSuite();
    }

    // This method run automatically before each TestNG Test
    @BeforeTest(alwaysRun = true)
    public void setUpTest(ITestContext testContext) {
        // Run Super class method
        super.setUpTest(testContext);
        // Check if emulatorSettings is provided then assign required values from the file
        emulatorSettingsFile = testContext.getSuite().getParameter(emulatorSettingsFileParam) != null ? testContext.getSuite().getParameter(emulatorSettingsFileParam) : "";
        // If file provided then assign values for suitable variables
        if (!emulatorSettingsFile.isEmpty()) {
            // Retrieve file absolute path
            String emulatorSettingsFilePath = getFileAbsolutePath(emulatorSettingsFile);
            mobileName = getProperty(emulatorSettingsFilePath, mobileNameParam);
            deviceType = getProperty(emulatorSettingsFilePath, deviceTypeParam);
            mobileApp = getProperty(emulatorSettingsFilePath, mobileAppParam);
        } else {
            // Check mobile name if provided then save it else will generate a one with random name
            mobileName = testContext.getSuite().getParameter(mobileNameParam) != null ? testContext.getSuite().getParameter(mobileNameParam) : "automated" + StringHandler.getRandomString(5);
            mobileName = "Nexus_5X_API_27_x86";
            // Save device Type from testNG file to deviceType variable
            if (testContext.getSuite().getParameter(deviceTypeParam) != null) {
                // Check if deviceType has correct value (In case not will assign emulator value)
                deviceType = testContext.getSuite().getParameter(deviceTypeParam).toLowerCase().startsWith("real") ? "real" : "emulator";
            }
            // Save App Path from testNG file to mobileApp variable
            mobileApp = testContext.getSuite().getParameter(mobileAppParam) != null ? testContext.getSuite().getParameter(mobileAppParam) : "C:/Users/Ismail/Downloads/testing.apk";
        }
        // Start WebDriver as Mobile Application
        setUp();
    }

    // This method run automatically after each TestNG Test
    @AfterTest(alwaysRun = true)
    public void tearDownTest() {

        // If mobile then call close WebDriver for Mobile applications
        tearDown();
        // Run Super class method
        super.tearDownTest();
    }

    // This method run automatically before each TestNG Class
    @BeforeClass(alwaysRun = true)
    public void setUpClass() {
        // Run Super class method
        super.setUpClass();
    }

    // This method run automatically after each TestNG Class
    @AfterClass(alwaysRun = true)
    public void tearDownClass() {
        // Run Super class method
        super.tearDownClass();
    }

    // This method run automatically before each TestNG Method
    @BeforeMethod(alwaysRun = true)
    public void setUpMethod(ITestContext testContext, Method method) {
        // Run Super class method
        super.setUpMethod(testContext, method);
        // Run initElements for WebPageObject
        if (AndroidScreenObject.childClass != null)
            AndroidScreenObject.initElements();
    }

    // This method run automatically after each TestNG Method
    @AfterMethod(alwaysRun = true)
    public void tearDownMethod(ITestResult testResult) {
        /******************** Method Report Section Start ********************/
        // Provide a screenShot when Test case fail and add to report
        //TakeScreenShot.captureScreenshotOnFail(testResult);
        /******************** Method Report Section End ********************/
        /******************** Method Reset vars Section Start ********************/
        //WebElementHandler.element = null;
        /******************** Method Reset vars Section End ********************/
        // Run Super class method
        super.tearDownMethod(testResult);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy