简体   繁体   English

无法在 Java 中访问子类中的父类对象属性

[英]not able to access parent class object properties in child class in Java

My question is pretty straightforward I have parent class and I have created a new driver object of webdriver class there In my subclass I am able to access the driver object but its value is null I am not able to access the object properties My parent Class is我的问题很简单,我有父类,并且在那里创建了一个 webdriver 类的新驱动程序对象 在我的子类中,我可以访问驱动程序对象,但它的值为 null 我无法访问对象属性

public class BaseTest {

    public static  WebDriver driver;
    @BeforeTest
    @Parameters("browser")
    public void  VerifyPageTitle(String browser)
    {
        if(browser.equalsIgnoreCase("chrome")){
            System.setProperty("webdriver.chrome.driver","C://chromedriver_win32(1)//chromedriver_win32//chromedriver.exe");
             driver = new ChromeDriver();
        }
        if(browser.equalsIgnoreCase("IE")){
            System.setProperty("webdriver.ie.driver","C://IEDriverServer_x64_3.3.0//IEDriverServer.exe");
            driver = new InternetExplorerDriver();
        }
        driver.manage().window().maximize();
    }
}

in my sub-class code在我的子类代码中

public class SignPageTest extends BaseTest{

    SignPage obj ;
    Boolean stu;
    @Test
    public void navigateToSignPage(){
    obj = new SignPage(driver);
    stu = obj.navigateToSignIn();
    }
    @Test(priority=2)
    public void getHandles(){
        stu = obj.handlingWindows();
    }

}

I am getting null-pointer as my driver is not initialized So please tell me the way to simply access the objects properties of super-class由于我的驱动程序未初始化,我得到空指针 所以请告诉我简单访问超类对象属性的方法

the moment I make my driver as static I am able to access its properties in all other classes but I don't want it to be static当我将我的驱动程序设为静态时,我可以在所有其他类中访问它的属性,但我不希望它是静态的

The problem lies in your test code.问题在于您的测试代码。

You are initialising a WebDriver instance via an @BeforeTest annotated method.您正在通过@BeforeTest注释方法初始化WebDriver实例。 TestNG invokes a @BeforeTest method only ONCE per <test> tag. TestNG的调用@BeforeTest每方法仅ONCE <test>标记。 So the below combination (which am guessing is what you have) will cause NullPointerException in the @Test methods of the second class.所以下面的组合(我猜你有什么)会在第二个类的@Test方法中导致NullPointerException

  • You have SignPageTest extend BaseTest and lets say you have another class called SomeFlowTest which also extends BaseTest您有SignPageTest扩展BaseTest并假设您有另一个名为SomeFlowTest类,它也扩展了BaseTest
  • You have created a <test> that includes both SignPageTest and SomeFlowTest .您已经创建了一个包含SignPageTestSomeFlowTest<test>

This would cause the @BeforeTest to be executed only once for either SignPageTest (or) SomeFlowTest (depending upon the order in which they occur in your <test> tag), because both these classes extend from the same base class.这将导致@BeforeTest只为SignPageTest (或) SomeFlowTest执行一次(取决于它们在<test>标签中出现的顺序),因为这两个类都从同一个基类扩展。 So once TestNG executes VerifyPageTitle() via SignPageTest it will not execute the same method via SomeFlowTest and so the WebDriver instance for SomeFlowTest is null.所以一旦TestNG的执行VerifyPageTitle()通过SignPageTest它不会执行通过相同的方法SomeFlowTest等等WebDriver例如SomeFlowTest为空。

To fix this problem change @BeforeTest to either @BeforeClass (gets executed once per test class) or @BeforeMethod (gets executed for every @Test method).要解决此问题, @BeforeTest更改为@BeforeClass (每个测试类执行一次)或@BeforeMethod (每个@Test方法执行一次)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM