0

I am using JUnit to make automation test , the context is changed to hybrid App , after tests I`m making output excel file with the result. I am new to the automation and found a way if something is not found and etc. to use try / catch block to fill the excel file with the result . Everything looks that work like a charm I think ,but I have issue when there is a long Test Case ( with a lot of test steps) , if something goes to the "catch block" the test steps in the excel file are marked as "Pass" after the "Fail" test step . Which I want to fix . Is there a way if some test step is marked as "FAIL" , all the other test steps under him to be marked also like "FAIL" or something like that .

public class LoginProcess extends Settings {
static SXSSFWorkbook workbook;
public static String[] columns = { "TestStep", "ExpectedResult", "Pass/Fail" };
public static List<TestCaseForExcel> testCases = new ArrayList<>();

@Before
public void BeforeTest() throws MalformedURLException, InterruptedException {
    Capabilities();

}

@Test
public void TC_1_LoginProcess() throws MalformedURLException, InterruptedException {


        try {
            driver.findElement(By.xpath("/html/body/div[2]")).isDisplayed();
            testCases.add(new TestCaseForExcel("Opened Customer application", "Splash screen opened", "PASS"));
        } catch (Exception e) {
            testCases.add(new TestCaseForExcel("Opened Customer application", "Splash screen opened", "FAIL"));

        }

        // Click on Login Button
        try {
            driver.findElement(By.className("button-simple")).click();
            testCases.add(new TestCaseForExcel("Press Login button", "Login screen opened", "PASS"));
        } catch (Exception e ) {
            testCases.add(new TestCaseForExcel("Press Login button", "Login screen openedd", "FAIL"));

        }

}   

In that case if the xpath is broken , and the element is not shown , the 2nd test step is written as "PASS" ,and its not even properly checked ,because of the first test step "FAIL"

0

Is there a way if some test step is marked as "FAIL" , all the other test steps under him to be marked also like "FAIL" or something like that?

At the start of the test declare a boolean to serve as your flag and wrap each try catch.

var keepGoing = true;
if (keepGoing) {
    try {
        // your code
    } catch {
        // your code
        keepGoing = false;
    }
}
// rinse repeat

This gets the job done but I wouldn't call it a best practice. This is where Asserts came in. If you Assert a condition and it fails, the test method stops in its tracks (via throwing an Exception, which you would then log).

  • 1
    @It works , just " keep going " must be boolean type not " var " in java , because variable cant be boolean type – RosS Apr 27 '18 at 11:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.