android.TestNGMethods Maven / Gradle / Ivy
The newest version!
package android;
import core.annotations.AttributesConfig;
import core.annotations.ClassAttributes;
import org.testng.ITestContext;
import org.testng.ITestResult;
import org.testng.annotations.*;
import java.lang.reflect.Method;
import static android.AndroidDriverInitializer.*;
import static core.StringHandler.getRandomString;
import static core.files.FileHelper.getFileAbsolutePath;
import static core.files.reader.ReadPropFile.getProperty;
import static core.reports.TestReporter.error;
/**
* 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" + getRandomString(5);
// 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 ? getFileAbsolutePath(testContext.getSuite().getParameter(mobileAppParam)) : "";
}
if (!mobileName.isEmpty() && !mobileApp.isEmpty())
// 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();
// Initialize mobile driver in case isn't initialized
if (androidDriver == null) {
if (getRunningTestClass().getAnnotation(ClassAttributes.class) != null) {
try {
ClassAttributes classAttributes = this.getClass().getAnnotation(ClassAttributes.class);
if (!attributesConfig.getCapabilitiesFile(classAttributes).isEmpty()) {
String capsFile = attributesConfig.getCapabilitiesFile(classAttributes);
// Retrieve file absolute path
String emulatorSettingsFilePath = getFileAbsolutePath(capsFile);
mobileName = getProperty(emulatorSettingsFilePath, mobileNameParam);
deviceType = getProperty(emulatorSettingsFilePath, deviceTypeParam);
mobileApp = getProperty(emulatorSettingsFilePath, mobileAppParam);
mobileApp = getFileAbsolutePath(mobileApp);
}
} catch (Throwable throwable) {
error("Something went wrong while reading class attributes to get mobile capabilities.", true);
}
} else {
error("You didn't provide mobile name or app, please check your config parameters or caps file.", true);
}
// Start WebDriver as Mobile Application
setUp();
}
}
// 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);
}
}