@Listeners({ExecutionListener.class, ExtentListener.class})
public abstract class BaseClass implements ITest{
protected static ResultWriterCSV resultWriter;
private static Runtime runTime = Runtime.getRuntime();
private static Boolean setupComplete = false;
protected static AppiumDriver<MobileElement> driver;
private AppiumDriverLocalService service;
public static ClinicianProgrammer cpApp;
public static TabletIO tablet;
private static DesiredCapabilities caps;
protected Logger log = LogManager.getLogger(this.getClass());
protected ThreadLocal<String> testName = new ThreadLocal<>();
private String TestID;
private LoadPropertiesFile loadPropFile;
private static File appium_logfile_folder;
@BeforeSuite
public void timestamp_AppiumLogs_Folder(ITestContext ctx) {
//If appium_logfile_folder exists from previous run, timestamp it
appium_logfile_folder = new File(TestDataConstants.APPIUM_LOGFILE_LOCATION + "/" + ctx.getSuite().getXmlSuite().getName());
String fileName = new SimpleDateFormat("yyyy-MM-dd-HH-mm").format(new Date());
if(appium_logfile_folder.exists()) {
appium_logfile_folder.renameTo(new File(appium_logfile_folder + "-" + fileName));
appium_logfile_folder.delete();
}
}
@BeforeTest
public void setup(ITestContext ctx) {
setupComplete = false;
resultWriter = ResultWriterCSV.getInstance();
try {
runTime.exec("adb shell am force-stop '
Nalu.CP.Android'");
} catch (IOException e) {
e.printStackTrace();
}
//Create output folder within the outputs/AppiumLogs folder with TP name and procedure name
if(!appium_logfile_folder.exists()) {
appium_logfile_folder.mkdir();
}
}
@Parameters({"deviceName","platformVersion", "portNumber"})
@BeforeClass
public void initialize( String deviceName, String platformVersion, String portNumber) throws MalformedURLException, WebDriverException{
try {
log.info("BeforeClass from NaluTestSection");
//Start Appium Service
if(checkIfServiceIsRunning(Integer.parseInt(portNumber)))
stopAppiumService();
try {
startAppiumService(portNumber, this.getClass().getSimpleName());
} catch (Exception e) {
e.printStackTrace();
}
//Set Appium driver capabilities
loadPropFile = new LoadPropertiesFile(System.getProperty("user.dir") + "\\framework.properties");
loadPropFile.loadPropertiesFile();
caps = new DesiredCapabilities();
caps.setCapability("platformName", loadPropFile.getProperty("appium.platformName"));
caps.setCapability("newCommandTimeout", loadPropFile.getProperty("appium.newCommandTimeout"));
caps.setCapability("resetKeyboard", loadPropFile.getProperty("appium.resetKeyboard"));
caps.setCapability("autoGrantPermissions", loadPropFile.getProperty("appium.autoGrantPermissions"));
caps.setCapability("disableAndroidWatchers", true);
caps.setCapability("skipUnlock", true);
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, platformVersion);
caps.setCapability(MobileCapabilityType.UDID, deviceName);
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, loadPropFile.getProperty("appium.appPackage"));
caps.setCapability(MobileCapabilityType.AUTOMATION_NAME, loadPropFile.getProperty("appium.automationName"));
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, loadPropFile.getProperty("appium.appActivity"));
caps.setCapability(MobileCapabilityType.NO_RESET, loadPropFile.getProperty("appium.dontStopAppOnReset"));
log.info("Starting Appium driver...");
//Start Appium driver
driver = new AppiumDriver<MobileElement>(new URL("
http://127.0.0.1:" + portNumber + "/wd/hub"), caps);
log.info("Appium driver started...");
cpApp = new ClinicianProgrammer();
tablet = new TabletIO();
} catch (WebDriverException e) {
try {
log.info("Retrying Appium driver start...");
driver = new AppiumDriver<MobileElement>(new URL("
http://127.0.0.1:" + portNumber + "/wd/hub"), caps);
}catch(WebDriverException we) {
log.error("Error starting Appium driver..." + we);
we.printStackTrace();
throw new WebDriverException();
}
} catch (MalformedURLException e) {
log.error("Error starting Appium driver..." + e.getMessage());
log.error("Error starting Appium service...: full stack trace follows:", e);
e.printStackTrace();
throw new MalformedURLException();
}
}
public static WebDriver getDriver() {
return driver;
}
/*Here alwaysRun is set to true because in normal circumstances BeforeMethod is skipped if BeforeClass fails
we want to set the TestName to be set even if the BeforeClass fails. Setting alwaysRun to true ensures
that the testName will always be set. This prevents bug in extentListener which was taking the previous testcase Name
instead of the current one while logging in Extent Reports
*/
@BeforeMethod(alwaysRun=true)
public void setUpTest(Method method, Object[] testData, ITestContext ctx) {
log.info("BeforeMethod from NaluTestSection");
try {
Map<String,String> map = new HashMap<String,String>();
//Updates names of testcases that use DataProviders
if (testData.length > 0) {
//Code used for dataproviders from excel sheets that contain TestID
String[] entries = testData[0].toString().split(",");
if(entries.length>1) {
for(String entry:entries) {
map.put(entry.split("=")[0].trim(), entry.split("=")[1].trim());
}
TestID = map.get("TestID");
testName.set(method.getName() + "_" + TestID);
ctx.setAttribute("testName", method.getName() + "_" + TestID);
}
else {
//Code used for dataproviders that have single entry and no TestID's, viz create_patient
testName.set(method.getName() + "_" + testData[0].toString());
ctx.setAttribute("testName", method.getName() + "_" + testData[0].toString());
}
} else {
TestID = method.getName();
testName.set(method.getName());
ctx.setAttribute("testName", method.getName());
}
log.info("Testname in NaluTestSection is: " + ctx.getAttribute("testName"));
//Also check if app is already running. Helpful in conditions where app shutsdown in middle of any testcase though setup was completed successfully
if (!setupComplete && driver!=null){
log.info("Relaunching app due to setup incomplete or driver==null...");
try {
driver.launchApp();
beforeEachTestMethod();
}catch(Exception e) {
e.printStackTrace();
throw new Exception();
}
}
setupComplete=true;
}catch(Exception e) {e.printStackTrace();}
}