DoubtClear
DoubtClear

Reputation: 149

how to get variable of one class in another extended class

in below test script I want to pass value of suiteResult from class SuiteOneBase to class SuiteOneCaseOne .How can i do this. Suppose if I am getting suiteResult =true then i should get true in class SuiteOneCaseOne . But now everytime i am getting false only

First class

public class SuiteOneBase extends SuiteBase{ 
  boolean suiteResult;
@BeforeSuite
    public void checkSuiteToRun() throws IOException{
        init();
        //To set TestSuiteList.xls file's path In FilePath Variable.
        FilePath = TestSuiteListExcel;
        SheetName = "SuitesList";
        SuiteName = "SuiteOne";
        ToRunColumnName = "SuiteToRun";
        suiteResult= SuiteUtility.checkToRunUtility(FilePath, SheetName,ToRunColumnName,SuiteName);

          if(!suiteResult){ 

            SuiteUtility.WriteResultUtility(FilePath, SheetName, "Skipped/Executed", SuiteName, "TestSuite Skipped");
                throw new SkipException(SuiteName+"'s SuiteToRun Flag Is 'N' Or Blank. So Skipping Execution Of "+SuiteName);
        }
        SuiteUtility.WriteResultUtility(FilePath, SheetName, "Skipped/Executed", SuiteName, "TestSuite Executed");

    }   
}

Second Class

 public class SuiteOneCaseOne extends SuiteOneBase{

        @BeforeTest
        public void checkCaseToRun() throws IOException{


            System.out.println("suiteResult "+suiteResult );
            if(!suiteResult){
            if(!SuiteUtility.checkToRunUtility(FilePath, SheetName,ToRunColumnNameTestCase,TestCaseName)){

                SuiteUtility.WriteResultUtility(FilePath, SheetName, "Pass/Fail/Skip", TestCaseName, "TESTCASE SKIP");

                throw new SkipException(TestCaseName+"'s CaseToRun Flag Is 'N' Or Blank. So Skipping Execution Of "+TestCaseName);
            }   
            }

            TestDataToRun = SuiteUtility.checkToRunUtilityOfData(FilePath, TestCaseName, ToRunColumnNameTestData);

        }

SuiteBase

package com.stta.TestSuiteBase;

import java.io.IOException;
import com.stta.utility.Read_XLS;

public class SuiteBase {    
    public static Read_XLS TestSuiteListExcel=null;
    public static Read_XLS TestCaseListExcelOne=null;
    public static Read_XLS TestCaseListExcelTwo=null;

    public void init() throws IOException{

    TestSuiteListExcel = new Read_XLS(System.getProperty("user.dir")+"\\src\\com\\stta\\ExcelFiles\\TestSuiteList.xls");

    TestCaseListExcelOne = new Read_XLS(System.getProperty("user.dir")+"\\src\\com\\stta\\ExcelFiles\\SuiteOne.xls");

        TestCaseListExcelTwo = new Read_XLS(System.getProperty("user.dir")+"\\src\\com\\stta\\ExcelFiles\\SuiteTwo.xls");                                                                           

}
}

Upvotes: 0

Views: 93

Answers (3)

juherr
juherr

Reputation: 5740

You can use ITestContext for that purpose:

public class SuiteOneBase extends SuiteBase { 

    @BeforeSuite
    public void checkSuiteToRun(ITestContext context) throws IOException {
        // [...]
        boolean suiteResult = SuiteUtility.checkToRunUtility(FilePath, SheetName,ToRunColumnName,SuiteName);    
        // [...]
        context.setAttribute("suiteResult", suiteResult);    
    }   
}

And

public class SuiteOneCaseOne extends SuiteOneBase {

    @BeforeTest
    public void checkCaseToRun(ITestContext context) throws IOException {    
        boolean suiteResult = context.getAttribute("suiteResult");
        System.out.println("suiteResult "+suiteResult );
        // [...]    
    }
}

Upvotes: 1

Andrew Regan
Andrew Regan

Reputation: 5113

SuiteOneCaseOne extends SuiteOneBase and is in the same package, so it can already see the package-level suiteResult. No need for any additional passing.

The @BeforeSuite in SuiteOneBase runs before the @BeforeTest in SuiteOneCaseOne, so the fact that the latter sees suiteResult == false proves that the former - which must have run - must have explicitly set suiteResult = false.

You're not looking at a default value, or the value of a different field. checkSuiteToRun() must run, and there are no exceptions or returns, so:

suiteResult= SuiteUtility.checkToRunUtility(FilePath, SheetName,ToRunColumnName,SuiteName);

... must have returned false.

(And yet you don't mention the tests failing to run as a result of the SkipException. I don't know how that can be avoided. Unless perhaps your @BeforeSuite has been imported from the wrong package, i.e. it doesn't actually get run.)

Upvotes: 0

Sadik Ali
Sadik Ali

Reputation: 1205

You should create suiteResult public or protected variable of class SuiteOneBase

  public  boolean suiteResult;
  or
  protected  boolean suiteResult;

Hope this will work!!

Upvotes: 0

Related Questions