0

我是测试世界的新手。我正在使用 selenium IDE 来记录测试。另外,我将测试用例导出为 JUnit4 测试用例。我的出口看起来像:

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class Test {
    private WebDriver driver;
    private String baseUrl;
    private StringBuffer verificationErrors = new StringBuffer();
    @Before
    public void setUp() throws Exception {
        driver = new FirefoxDriver();
        baseUrl = "http://192.168.8.207/";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @Test
    public void test() throws Exception {
        driver.get(baseUrl + "/");
        driver.findElement(By.name("user")).clear();
        driver.findElement(By.name("user")).sendKeys("admin");
        driver.findElement(By.name("password")).clear();
        driver.findElement(By.name("password")).sendKeys("infineta123");
        driver.findElement(By.id("btnLogin-button")).click();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }

    private boolean isElementPresent(By by) {
        try {
            driver.findElement(by);
            return true;
        } catch (NoSuchElementException e) {
            return false;
        }
    }
}

如何执行这个测试用例?在那之后,我怎样才能自动执行几个这样的测试用例?

4

2 回答 2

3

将此代码用作测试的运行器。

import org.junit.runner.JUnitCore;
import com.example.tests;

public static void main(String[] args) {
    Result result = JUnitCore.runClasses(Test.class);
    for (Failure failure : result.getFailures()) {
        System.out.println(failure.toString());
    }
}

Test.class 是包含测试代码的文件名(Test)。如果您有多个测试用例,您可以添加类列表。

于 2012-06-19T08:57:29.773 回答
0

看看这个页面:http ://code.google.com/p/selenium/wiki/UsingWebDriver

您可能必须下载 selenium 并将其导入项目。

如果这样做了,您可以转到“测试”-这取决于您使用的开发工具-在 NetBeans 中,我通过运行-测试文件(我认为是 Ctrl + F6)

于 2012-06-12T20:41:57.403 回答