问题selenium代码维护

发布于 2024-08-25 06:26:04 字数 2344 浏览 6 评论 0原文

我想将常用方法分组在一个文件中并使用它。例如,使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小巷里的女流氓 2024-09-01 06:26:21

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...

2024-09-01 06:26:10

我们这样做的方式是,我们有带有静态方法的辅助类。在实际的测试用例中,我们设置了 selenium 对象并将该对象传递给静态方法,以便它可以对其进行操作。

public BaseHelper
{
    public static login( final String username, final String password, final DefaultSelenium selenium )
    {
     selenium.type("userName", username);
     selenium.type("password", password);
     selenium.click("loginbutton");
    }
}


public LoginTest
{
    DefaultSelenium selenium;

    onSetup()
    {
      selenium = new DefaultSelenium(...);
    }  

    public testLogin()
    {
      BaseHelper.login( "admin", "admin", selenium);
      // assert that this passed
      BaseHelper.login( "foo", "bar", selenium);
      // assert this failed because no user 'foo'
      BaseHelper.login( "admin", "bar", selenium);
      // assert this failed because admin's password was incorrect
    }
}

希望这能说明这一点。

除了更好的可读性和更容易的维护之外,您还可以通过创建两个(或更多)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.

public BaseHelper
{
    public static login( final String username, final String password, final DefaultSelenium selenium )
    {
     selenium.type("userName", username);
     selenium.type("password", password);
     selenium.click("loginbutton");
    }
}


public LoginTest
{
    DefaultSelenium selenium;

    onSetup()
    {
      selenium = new DefaultSelenium(...);
    }  

    public testLogin()
    {
      BaseHelper.login( "admin", "admin", selenium);
      // assert that this passed
      BaseHelper.login( "foo", "bar", selenium);
      // assert this failed because no user 'foo'
      BaseHelper.login( "admin", "bar", selenium);
      // assert this failed because admin's password was incorrect
    }
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文