TestNG测试在servlet中执行时跳过

时间:2014-03-10 10:46:51

标签: java servlets selenium selenium-webdriver testng

我试图在“TestListenerAdapter”和“TestNG的帮助下在servlet内执行TestNG测试”我的代码片段是这样的:

TestListenerAdapter tla = new TestListenerAdapter();
TestNG testng = new TestNG();
testng.setTestClasses(new Class[] {src.scriptDemo.LoginTest.class});
testng.addListener(tla);
testng.run();

因此,当我在Java程序中执行此代码片段时,测试名称“LoginTest”正在成功执行(即测试开始,浏览器打开并执行整个测试)。 但是当相同的代码在Servlet中运行时,它会给出:

[TestNG] Running:
  Command line suite


===============================================
Command line suite
Total tests run: 1, Failures: 0, Skips: 1
Configuration Failures: 1, Skips: 1
===============================================

似乎测试开始执行但跳过了。我不知道背后的原因。

PS:我调试了我的代码并发现,由于LoginTest selenium脚本文件中的FirefoxDriver(),它跳过了测试。 LoginTest.java的代码是:

package src.scriptDemo;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class LoginTest {

    WebDriver driver;
    String baseUrl;
    @BeforeTest
    public void setupSelenium() {
        driver = new FirefoxDriver(true);
        baseUrl = "https://example.com/login";
        System.out.println("1:");
    }

    @Test
    public void testCsrLogin() {
        driver.get(baseUrl);
        System.out.println("2:");
        driver.findElement(By.id("username")).sendKeys("blah");
        driver.findElement(By.id("password")).sendKeys("blah");
        driver.findElement(By.className("btn-primary")).click();
    }

    @AfterTest
    public void closeSelenium() {
            System.out.println("3:");
            driver.close();
            driver.quit();
    }
}

但是在通过评论FirefoxDriver()再次执行时,测试运行没有任何问题并提供输出

Total tests run: 1, Failures: 1, Skips: 0

有人能说明这里有什么,以及如何解决它, 如果需要更多说明,请告诉我。

先谢谢

1 个答案:

答案 0 :(得分:0)

如果你在容器内运行它们,应该使用selenium网格。(例如,你必须创建一个RemoteWebDriver实例而不是你的浏览器特定实例)。要开始使用selenium网格,请访问this link。确保在战争中添加和打包Selenium和网格依赖关系。