本文介绍了如何在不同的测试用例中使用相同的硒会话?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JUnit和Selenium.我想登录一次网页,运行后运行两个测试用例,而无需打开新的浏览器/会话.如果我在setUp()方法中执行登录",则每次在测试用例之前都会调用此方法.如何在所有测试用例中仅使用一种setUp()方法?

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?

推荐答案

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

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

}

这篇关于如何在不同的测试用例中使用相同的硒会话?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 11:23