serenity-bdd / serenity-core

Serenity BDD is a test automation library designed to make writing automated acceptance tests easier, and more fun.
http://serenity-bdd.info
Other
719 stars 516 forks source link

Is there a way to update serenity bdd properties programatically? #2482

Open skhandekar1 opened 3 years ago

skhandekar1 commented 3 years ago

I am running Serenity with appium and junit. I want to update the appium.dontStopAppOnReset = true only for certain test scripts. Is it possible to do this through junit? My junit script looks as follows:

package com.tp.tp397.Procedure1_ORSession;

//@RunWith(SerenityRunner.class)

//@FixMethodOrder(MethodSorters.NAME_ASCENDING) @WithTags({ @WithTag(type = "TP", name = "TP-000397") }) @RunWith(SerenityRunner.class) public class TP397_P1_S06 {

@Managed
WebDriver appiumDriver;

@Steps
UserLoginsWithValidCredentials user;

@Steps
TP_Steps tpSteps;

@Steps
UserCreatesPainAreaMap patientLocator;

@Steps
UserCreatesORSession session;

@Steps
UserCreatesORSession createOR;

@Steps
ConfigElectrodePage setAnodeCathode;

@Steps
ORSession_CheckRangeValuesOfTonicStimulationMode verifier, rangeVal;

@Steps
UserVerifiesElectrodeOnORSession electrodeConfig;

@Steps
ProgramConfiguration programConfiguration;

@Steps
ORSession_CheckRangeValuesOfTonicStimulationMode stepSizeSelect;

@Steps
ORSession_RangeValuesPage ampSize, ampUpDown, upDownArrowDisabledForRange, verifyStepSize;

@Steps
NewPatientProfilePage impedances;

@Steps
NewPatientProfilePage screenSelector, impedance;

@Steps
ToastMessageStep toastMessageStep;

String amp, pw, rate, cathode, anode;

int initialImpedanceListCount, finalImpedanceListCount, result;
private static final Logger log = LogManager.getLogger(com.tp.tp397.Procedure1_ORSession.TP397_P1_S06.class);
private static ExecutionListener execListener;
private static ResultWriterCSV resultWriter;
private static Runtime runTime = Runtime.getRuntime();
private static Boolean setupComplete = false;

@BeforeClass
public static void init() {

    try {
        runTime.exec("adb shell am force-stop 'CP.Android'");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    resultWriter = ResultWriterCSV.getInstance();
    execListener = new ExecutionListener();
    StepEventBus.getEventBus().registerListener(execListener);
    runSerenity().inASingleSession();

    try {
        runTime.exec("adb shell am start -n CP.Android/crc6429e3927486beccbc.ActivityLicenseBrowse");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

@Before
public void setUpTest() {
    if (!setupComplete){
        user.userOpensTheApplication();     
        user.userEntersNewCredentials();    
        user.userClickOnLogin();
        patientLocator.userShouldBeAbleToSearchPatientName("Thomas");
        patientLocator.userViewsPatientRecord();
        session.userShouldBeAbleToViewSessionListPageAfterClickingOnSessionTab();
        session.userShouldBeAbleToViewTheChooseSessionTypePopupAfterClickingOnPlusButton();
        session.userViewsCreateNewSessionPageAfterClickingOnORSessionButton();

        log.info("user will set A1 as Anode and A2 as Cathode");
        setAnodeCathode.setAnode("A1");
        setAnodeCathode.setCathode("A2");
        setupComplete = true;
    }
}

@Title("Increase Amplitudes according to the step sizes")
@Test
public void Test_1() {
    ResultWriterParams.testCaseId = "TP397_ID27";
    ResultWriterParams.acceptanceCriteria = "Amplitudes increases according to the step size";

    rangeVal.userShouldBeAbleToEnterTheRequiredParameterValues("Amplitude", "120");

    verifier.byDefaultUserShouldBeAbleToIncreaseAmplitudeValueAccordingToThierDefaultStepSize(
            SessionTestDataConstants.STEP_SIZE_40);
}

@Title("Decrease Amplitudes according to the step sizes")
@Test
public void Test_2() {
    ResultWriterParams.testCaseId = "TP397_ID28";
    ResultWriterParams.acceptanceCriteria = "Amplitudes decreases according to the step size";

    verifier.byDefaultUserShouldBeAbleToDecreaseAmplitudeValueAccordingToThierDefaultStepSize(
            SessionTestDataConstants.STEP_SIZE_40);
}

@Title("Amplitude increase is limited to maximum 10200µA")
@Test
public void Test_3() {
    ResultWriterParams.testCaseId = "TP397_ID10";
    ResultWriterParams.acceptanceCriteria = "Amplitude decrease is limited to minimum 40µA";

    rangeVal.userShouldBeAbleToEnterTheRequiredParameterValues("Amplitude", "30");
    toastMessageStep.isErrorMsgDisplayed("Warning! Value out of range. Valid Range: 40 - 10200");

}

@Title("Amplitude decrease is limited to minimum 40µA")
@Test
public void Test_4() {
    ResultWriterParams.testCaseId = "TP397_ID10";
    ResultWriterParams.acceptanceCriteria = "Amplitude decrease is limited to minimum 40µA";

    rangeVal.userShouldBeAbleToEnterTheRequiredParameterValues("Amplitude", "30");
    toastMessageStep.isErrorMsgDisplayed("Warning! Value out of range. Valid Range: 40 - 10200");

}       

@AfterClass
public static void cleanup() {
    System.out.println("After class cleanup executed*****************");
    try {
        runTime.exec("adb shell am force-stop 'CP.Android'");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    resultWriter.close();
}

}

skhandekar1 commented 3 years ago

I tried updating the serenity.properties file in @BeforeClass, but the properties file is not getting picked up by junit and it still uses the old properties. Any ideas how to solve this problem?

    private static FileInputStream fis = null;
    private static String propFile = "serenity.properties";

@BeforeClass
    public static void init() {
        try {
            FileInputStream in = new FileInputStream(propFile);
            Properties props = new Properties();
            props.load(in);
            in.close();

            FileOutputStream out = new FileOutputStream(propFile);
            props.setProperty("appium.dontStopAppOnReset", "true");
            props.store(out, null);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
}
Jacobvu84 commented 3 years ago

If you want to apply only for certain test scripts. It should not set the configuration in the @BeforeClass because this annotation will run first for all class.

For this goals: you should create an annotation that is used for each certain test.

The idea is like that

@AppiumCap(dontStopAppOnReset=true)
@Test
public void your_test(){  ...  }
skhandekar1 commented 3 years ago

Could you please explain how to create such an annotation. My problem is that Serenity bdd, it is going to look at the code and already set the parameter as false when it doesnt find it in the propeties, so how can i set it to true through the annotation? I trued System.setProperty and it does not work as Serenity bdd does now allow change in properties. So your method looks interesting, just i am not sure how to create such an annotation. Could you please explain? Thank you!

jmillarMC commented 3 years ago

The @BeforeClass method it needs to be static and what I have done is to use System.setProperty("webdriver.driver", "remote"); @BeforeClass will run before the test suite so if you had 2 testsuite classes each with a different @BeforeClassmethod to set that property to what you need. it should work