开发者

How to call Selenium to another class : NullPointerException

开发者 https://www.devze.com 2023-03-23 14:07 出处:网络
How can we cal an object selenium to the other file which has half code of selenium. In PHP i can by following code.

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")) {
开发者_JAVA百科            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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消