我将一个WebDriver
实例传递给下面的类
FunctionalComponents fc = new FunctionalComponents(driver)
来自另一个类,但对象创建发生在构造函数执行之前。实际上,创建的对象null
在驱动程序实例中具有价值。
我怎么解决这个问题?
public class FunctionalComponents
{
public FunctionalComponents(WebDriver driver)
{
this.driver = driver;
}
CaptureElement element= new CaptureElement(driver);
public void Method()
{
// method logic
// i call object element here
}
}
不要在字段定义期间在外部设置成员变量的值。在构造函数中执行此操作,这将保证您想要的变量数量。以机智:
public class FunctionalComponents
{
private IWebDriver driver;
private CaptureElement element;
public FunctionalComponents(WebDriver driver)
{
this.driver = driver;
this.element = new CaptureElement(driver);
}
public void Method()
{
// method logic
// i call object element here
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句