Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Junit 发布selenium代码维护_Junit_Selenium - Fatal编程技术网

Junit 发布selenium代码维护

Junit 发布selenium代码维护,junit,selenium,Junit,Selenium,我想将常用方法分组到一个文件中并使用它。例如,可以多次使用selenium登录页面。在类A中定义它并在类B中调用它。然而,它抛出空指针异常 甲级有 public void test_Login() throws Exception { try{ selenium.setTimeout("60000"); selenium.open("http://localhost"); selenium.windowFocus();

我想将常用方法分组到一个文件中并使用它。例如,可以多次使用selenium登录页面。在类A中定义它并在类B中调用它。然而,它抛出空指针异常

甲级有

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)

我希望以前一定有人试过。我可能会错过一些东西

selenium对象在类A中是如何初始化的?你记得从B类中创建它的地方传入吗?如果这是它的工作方式,那就是很难看到代码的这一部分没有包括在内

我们这样做的方式是,我们有带有静态方法的helper类。在实际的测试用例中,我们设置了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对象来测试多用户行为,并在测试中传递这些对象

确保selenium服务器已启动,并且在运行此代码之前已启动selenium浏览器。我已经这样做了。它会打开浏览器,但无法将地址放在那里。您可以将如何进行设置添加到此问题中吗?如果需要在不同的位置重复执行某些操作序列,我希望将这些操作放在某个方法下,请在需要时调用该方法。例如,以下面的登录方法为例。在每个套件中,我希望在类文件中使用a来代替编写整个代码,并在类B、类C等中调用它。login(){open webpage enter username enter password click submit}这是程序员维护代码的常用方法。我只是用Selenese尝试一下。我不想在类A中初始化任何东西。只有代码的一部分是从类B调用的。这不正确吗?我不知道,我看不到您发布的代码中的作用域,但似乎如果您在类B中初始化selenium对象,那么您必须以某种方式将其传递给类A(可能是通过构造器)。抱歉,这可能还不清楚-我的意思是,在测试运行时,初始化扩展SeleneseTestCase的对象的selenium属性显然有些神奇。当您尝试从测试内部调用另一个类时,该属性未初始化,因此您必须从外部c传入selenium属性的值因此,在类A构造函数中添加一个参数,并使用该参数初始化A中的selenium属性。从B开始,您将通过执行新A(selenium)或其他操作来创建新A。如果这是问题所在:-)我将检查此初始化操作。