How to call Selenium to another class : NullPointerException

霸气de小男生 提交于 2019-12-02 10:23:49

问题


How can we cal an object selenium to the other file which has half code of selenium.

In PHP i can by following code.

login($this); ----> login($sel){ ..... }

Can i do the same in Java as my selenium setup is in one file and the function which uses it is in another file can we pass the selenium to other as I am getting the NullPointerException.
Let me know if you want more details related to this.

Update

Library.java

public class Library extends SeleneseTestCase {
    public int Login() throws Exception {

        if (selenium.isElementPresent("companyID")) {
            selenium.type("companyID", "COMP");
            selenium.click("submit_logon");
            selenium.waitForPageToLoad("80000");
        }
    }
}

Login.java

public class Login extends Library {

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*chrome", "https://businessbanking.com/");
        selenium.start();
    }

    public void testAllTests() throws Exception {
        Library obj1 = new Library();
        obj1.Login();
    }
}

As per my observation selenium instance started on login file is not addressed to Library. I tried to pass "selenium" as parameter but failed, in Library i tried "super.setUp()" it also failed.

Thanks.


回答1:


Replace:

public void testAllTests() throws Exception {
    Library obj1 = new Library();
    obj1.Login();
}

With:

public void testAllTests() throws Exception {
    super.Login();
}

Since your Login class already extends Library it already has the Login() method present in it. What you are currently doing is creating a new Library object which does not run the @Before and hence the Selenium field is not initialised (in the new object).

When a subclass extends a base class it will inherit its methods. This is a fundamental Java and OOP concept.



来源:https://stackoverflow.com/questions/6832319/how-to-call-selenium-to-another-class-nullpointerexception

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!