0

I am new to the world of testing. I am using selenium IDE for recording the tests. Also, I am exporting the test case as JUnit4 test case. My export looks like:

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

How to execute this test case? Now after that, how can I automate the execution of several such test cases?

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

おそらく、セレンをダウンロードしてプロジェクトにインポートする必要があります。

これが完了したら、「テスト」に移動できます-使用している開発ツールによって異なります-NetBeansでは、実行-テストファイル(Ctrl + F6だと思います)を介して実行します

于 2012-06-12T20:41:57.403 に答える