如何在另一个扩展类中获取一个类的变量

时间:2016-04-07 13:10:39

标签: java selenium-webdriver testng

在下面的测试脚本中我想将suiteResult的值从类SuiteOneBase传递到类SuiteOneCaseOne。我该如何做到这一点。假设我得到suiteResult =true,那么我应该在课程SuiteOneCaseOne中得到答案。但现在everytime我只是虚假

头等舱

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");

    }   
}

第二课

 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");                                                                           

}
}

3 个答案:

答案 0 :(得分:1)

您可以将ITestContext用于此目的:

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);    
    }   
}

public class SuiteOneCaseOne extends SuiteOneBase {

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

答案 1 :(得分:0)

您应该创建SuiteOneBase类的suiteResult public或protected变量

  public  boolean suiteResult;
  or
  protected  boolean suiteResult;

希望这会有效!!

答案 2 :(得分:0)

SuiteOneCaseOne扩展SuiteOneBase并且位于同一个包中,因此已经可以查看包级suiteResult。无需任何额外的传球。

@BeforeSuite中的SuiteOneBase@BeforeTest中的SuiteOneCaseOne 之前运行,因此后者看到suiteResult == false的事实证明了这一点前者 - 必须运行 - 必须明确设置suiteResult = false

您没有查看默认值或其他字段的值。 checkSuiteToRun()必须运行,并且没有例外或返回,因此:

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

...必须返回false。

(但是你没有提到由于SkipException而未能运行的测试。我不知道如何避免这种情况。除非你的@BeforeSuite已被导入错误的包,即它实际上没有运行。)