How to use the same selenium session with different test cases?

允我心安 提交于 2019-12-11 06:38:35

问题


I'm using JUnit and Selenium. I would like to log in once to a web page, after run I run two test cases, without opening a new browser/session. If I do the "logging in" in setUp() method, then this called at every time before the test cases. How can I use only one setUp() method for all of my test cases?


回答1:


I think it could be achieved in the following manner

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");
    }

}


来源:https://stackoverflow.com/questions/12932632/how-to-use-the-same-selenium-session-with-different-test-cases

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!