问题selenium代码维护
我想将常用方法分组在一个文件中并使用它。例如,使用selenium登录页面可能会被多次使用。在类A中定义它并在类B中调用它。但是,它会抛出空指针异常。
类 A 具有
public void test_Login() throws Exception
{
try{
selenium.setTimeout("60000");
selenium.open("http://localhost");
selenium.windowFocus();
selenium.windowMaximize();
selenium.windowFocus();
selenium.type("userName", "admin");
selenium.type("password", "admin");
Result=selenium.isElementPresent("//input[@type='image']");
selenium.click("//input[@type='image']");
selenium.waitForPageToLoad(Timeout);
}
catch(Exception ex)
{
System.out.println(ex);
ex.printStackTrace();
}
}
所有其他 java 语法
,类 B
public void test_kk() throws Exception
{
try
{
a.test_Login();
}
catch(Exception ex)
{
System.out.println(ex);
ex.printStackTrace();
}
}
具有所有语法。
当我执行B类时,我得到了这个错误,
java.lang.NullPointerException
at A.test_Login(A.java:32)
at B.test_kk(savefile.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.j
ava:212)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at junit.textui.TestRunner.run(TestRunner.java:77)
at B.main(B.java:77)
我希望有人以前尝试过这个。我可能会错过这里的一些东西。
I want to group the common methods in one file and use it. For example, login to a page using selenium may be used in multiple times. Define that in class A and call it in class B. However, it throws null pointer exception.
class A has
public void test_Login() throws Exception
{
try{
selenium.setTimeout("60000");
selenium.open("http://localhost");
selenium.windowFocus();
selenium.windowMaximize();
selenium.windowFocus();
selenium.type("userName", "admin");
selenium.type("password", "admin");
Result=selenium.isElementPresent("//input[@type='image']");
selenium.click("//input[@type='image']");
selenium.waitForPageToLoad(Timeout);
}
catch(Exception ex)
{
System.out.println(ex);
ex.printStackTrace();
}
}
with all other java syntax
in class B
public void test_kk() throws Exception
{
try
{
a.test_Login();
}
catch(Exception ex)
{
System.out.println(ex);
ex.printStackTrace();
}
}
with all syntax.
When I execute class B, I got this error,
java.lang.NullPointerException
at A.test_Login(A.java:32)
at B.test_kk(savefile.java:58)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.j
ava:212)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at junit.textui.TestRunner.doRun(TestRunner.java:116)
at junit.textui.TestRunner.doRun(TestRunner.java:109)
at junit.textui.TestRunner.run(TestRunner.java:77)
at B.main(B.java:77)
I hope someone must have tried this before. I may miss something here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
A类中的selenium对象是如何初始化的?您是否记得将其从 B 类中创建的位置传递过来?如果这就是它的工作方式,那就很难看出何时不包含这部分代码......
How is the selenium object initialized in class A? Do you remember to pass it in from where it gets created in class B? If that is the way it works, that is it's hard to see when that part of the code is not included...
我们这样做的方式是,我们有带有静态方法的辅助类。在实际的测试用例中,我们设置了 selenium 对象并将该对象传递给静态方法,以便它可以对其进行操作。
希望这能说明这一点。
除了更好的可读性和更容易的维护之外,您还可以通过创建两个(或更多)selenium 对象并在测试中传递它们来测试多用户行为。
The way we do it is, we have helper classes with static methods on them. In the actual test cases we set up our selenium object and pass the object into the static method so it can operate on it.
Hope this illustrates the point.
Besides the better readability and easier maintenance, you are also able to test multiuser behavior by creating two (or more) selenium objects and pass those around in the tests.