如何将Selenium调用到另一个类:NullPointerException
我们如何将对象硒校正为硒的一半代码的另一个文件.
How can we cal an object selenium to the other file which has half code of selenium.
在PHP中,我可以按照以下代码进行操作.
In PHP i can by following code.
login($ this);
----> login($ sel){.....}
我可以在Java中做同样的事情吗,因为我的硒设置位于一个文件中,而使用它的函数位于另一个文件中,当我得到NullPointerException时,我们可以将硒传递给另一个吗?
如果您需要与此相关的更多详细信息,请告诉我.
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.
更新
Library.java
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
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();
}
}
根据我的观察,在登录文件上启动的硒实例未寻址到库.我试图将"selenium"作为参数传递,但失败了,在库中,我尝试了"super.setUp()",但它也失败了.
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.
谢谢.
替换:
public void testAllTests() throws Exception {
Library obj1 = new Library();
obj1.Login();
}
使用:
public void testAllTests() throws Exception {
super.Login();
}
由于您的Login类已经扩展了Library,因此它已经具有Login()方法.您当前正在做的是创建一个不会运行 @Before
的新Library对象,因此Selenium字段未初始化(在新对象中).
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).
当子类扩展基类时,它将继承其方法.这是Java和OOP的基本概念.
When a subclass extends a base class it will inherit its methods. This is a fundamental Java and OOP concept.