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
如何在不同的测试用例中使用相同的selenium会话?_Selenium_Junit_Selenium Rc - Fatal编程技术网

如何在不同的测试用例中使用相同的selenium会话?

如何在不同的测试用例中使用相同的selenium会话?,selenium,junit,selenium-rc,Selenium,Junit,Selenium Rc,我正在使用JUnit和Selenium。我想登录一次网页,运行后我运行两个测试用例,而不打开新的浏览器/会话。如果我在setUp()方法中执行“登录”,那么每次在测试用例之前都会调用它。如何对所有测试用例只使用一个setUp()方法?我认为可以通过以下方式实现 package com.java; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.Befor

我正在使用JUnit和Selenium。我想登录一次网页,运行后我运行两个测试用例,而不打开新的浏览器/会话。如果我在
setUp()
方法中执行“登录”,那么每次在测试用例之前都会调用它。如何对所有测试用例只使用一个
setUp()
方法?

我认为可以通过以下方式实现

package com.java;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;

public class TestAnnt {

    public static Selenium sel;

    @BeforeClass
    public static void beforeClass() {
        sel = new DefaultSelenium("localhost", 5555, "*firefox",
                "http://www.google.com");
        sel.start();
        System.out.println("Before Class");
    }

    @Before
    public void beforeTest() {

        System.out.println("Before Test");

        // Actions before a test case is executed
    }

    @Test
    public void testone() {
        sel.open("/");
        sel.waitForPageToLoad("30000");
        System.out.println("Test one");
        // Actions of test case 1
    }

    @Test
    public void testtwo() {
        sel.open("http://au.yahoo.com");
        sel.waitForPageToLoad("30000");
        System.out.println("test two");
        // Actions of test case 2
    }

    @After
    public void afterTest() {
        System.out.println("after test");
        // Actions after a test case is executed
    }

    @AfterClass
    public static void afterClass() {
        sel.close();
        sel.stop();
        sel.shutDownSeleniumServer();
        System.out.println("After Class");
    }

}

您需要在测试之间更改用户吗?不,同一个用户对我很好。@Balint您可以更好地使用“@AfterClass”和“@BeforeClass”annotations@QATest我试图将setUp()方法放在“@BeforeClass”中,它为第二个测试用例打开了一个新的浏览器。或者你认为还有别的事吗?@Balint我能很好地理解我说的话。我想看看你的code@Balint,我正在更新我的答案,你可以验证一下谢谢,你帮了我很多忙。现在我理解了这些注释。