在另一个类

时间:2015-05-21 05:27:44

标签: java

我有一个扩展Mainpage的抽象类控制器。我也不能对MainPage进行任何更改。它不可编辑

public abstract class Controller extends MainPage {  
    public Controller(Remote remor) throws UnsuppoOSException, PException {     
           super(remor);           

    }
    }

MainPage类

public abstract class MainPage implements IPageObject {
    public Remote mDriver;
    public MainPage(Remote driver) throws PException {
        if (driver == null)
            throw new NullPointerException("Cannot initialize with null driver");
        this.mDriver = driver;      
    }
}   

我需要的是获取Controller的对象并在另一个类FactoryIndo

中使用

为此我扩展了Controller类。

public abstract class FactoryIndo extends Controller {
    public static Remote mDriver;

    public FactoryIndo(Remote remor) throws UnsuppoOSException, PException {
        super(remor);
        mDriver = remor;
    }

public List<TestContext> getBrowserTestContext(List<String> browsers)
        throws Exception {
    Map<String, Object> browserMap = new HashMap<String, Object>();
    browserMap.put(MasterConstants.BROWSER, this); // HERE am passing the object of Controller
    TestContext testContext = new TestContext(browser, browserMap);
    return testContext;
}

    public static List<TestContext> getTestContext(List<String> browsers)
            throws Exception {
        FactoryIndo instanceSel = new FactoryIndo(mDriver);
        List<TestContext> contexts = instanceSel
                .getBrowserTestContext(browsers);
        return contexts;
    }
}

在另一个类中调用此getTestContext方法时,无法使用null驱动程序初始化。 我的代码出了什么问题。 在没有扩展Controller类的情况下,还可以通过任何方式在FactoryIndo类中获取此对象

1 个答案:

答案 0 :(得分:1)

那是因为getTestContext是静态的,当它被初始化时,mDriver为null。 在调用

之前,您必须先为mDriver分配一个值
FactoryIndo instanceSel = new FactoryIndo(mDriver);

编辑:像这样的东西

public static List<TestContext> getTestContext(List<String> browsers)
        throws Exception {
    mDriver = //Your code goes here
    FactoryIndo instanceSel = new FactoryIndo(mDriver);
    List<TestContext> contexts = instanceSel
            .getBrowserTestContext(browsers);
    return contexts;
}