当通过testng Framework执行时,我的硒测试不能从cmd或Jenkins运行。结果:总测试运行:0,失败:0,跳过:0

时间:2018-03-08 12:24:07

标签: java selenium jenkins selenium-webdriver testng

Selenium和自动化的新手。

首先,当我使用Junit运行时,我的测试在Eclipse中正常运行。但是,如果我通过Jenkins或CLI启动测试,我会得到结果:总测试运行:0,失败:0,跳过:0

现在我可能会把测试结构弄错,或者我可能做错了。我看了很多,网上的所有解决方案似乎都无法解决我的问题。

我的测试只是一个简单的登录测试。

我想我可能会将框架混淆或以错误的方式一起使用它们。我正在使用Testng开始我的硒测试我认为。我再次举起手来承认我是新手,所以我希望有人可以告诉我哪里出错了。我注意到在我的实际硒测试文件中我没有使用任何Testng函数或任何东西,只有selenium web驱动程序。我在下面列出了我的代码文件,结果是我从测试中获得的结果。

如果我错过了一个简单的步骤,或者我尝试了一些不可能的事情,我再次道歉,我只需要专家的帮助或指向正确的方向。在此先感谢您的帮助。

测试

出于安全原因,我删除了实际的网址和信用卡。

    package testing;

    import static org.junit.Assert.fail;

    import java.util.concurrent.TimeUnit;

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import org.openqa.selenium.Alert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.NoAlertPresentException;
    import org.openqa.selenium.NoSuchElementException;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;

    import org.testng.Assert;



    public class login {
      private WebDriver driver;
      private String baseUrl;
      private boolean acceptNextAlert = true;
      private StringBuffer verificationErrors = new StringBuffer();

      @Before
      public void setUp() throws Exception {
        System.setProperty("webdriver.gecko.driver","C:\\gecko\\geckodriver.exe");
        driver = new FirefoxDriver();
        baseUrl = "";
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
      }

      @Test
      public void testLogin() throws Exception {
        driver.get(baseUrl + "");
        driver.findElement(By.id("email")).clear();
        d

river.findElement(By.id("email")).sendKeys("");
    driver.findElement(By.id("password")).clear();
    driver.findElement(By.id("password")).sendKeys("");
    driver.findElement(By.xpath("//input[@value='Login »']")).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;
    }
  }

  private boolean isAlertPresent() {
    try {
      driver.switchTo().alert();
      return true;
    } catch (NoAlertPresentException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
}

的testng.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Sample Suite">

        <test name="Learning Jenkins">
        <classes>

            <class name="testing.login"></class>

        </classes>

    </test>

</suite> 

CLI或Jenkins测试的结果

[TestNG] Running:
  C:\Users\keating99\workspace\FdimTests\testng.xml


===============================================
Sample Suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

Finished: SUCCESS

我再次意识到我在实际测试中没有使用任何Testng,但我的测试目的是开启selenium来进行测试。也许我以错误的方式解决这个问题。

selenium测试有效,我可以在Eclipse中启动。

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

虽然这可能更适合作为评论,但有几点需要澄清,因为import org.junit.Test, Before, After, etc表示你已经编写了JUnit测试,但是你提到了TestNG,所以这取决于你真正想要的是什么要做:

  • 编写JUnit测试并通过TestNG 执行它们:虽然有点奇怪(我要判断谁?!),根据the docs可以确保JUnit在执行期间在类路径中可用,并将junit属性设置为true,例如<test name=name="Learning Jenkins" junit="true">

  • 编写并执行TestNG测试:用TestNG counterparts替换JUnit导入,例如org.testng.annotations.Test

  

我再次意识到我在实际测试中没有使用任何Testng,但我的测试目的是启动selenium来进行测试。也许我会以错误的方式解决这个问题。

暂时你正在使用JUnit,而不是TestNG :-)。无论如何,fail(verificationErrorString)你实际上正在使用它,虽然你的测试可能只是通过验证没有错误消息而不完整。您应该添加一些断言来检查您加载的页面上是否存在任何数据,这是正确的。您可以查看TestNG-Selenium quick tutorial或您想要的任何其他文档,其中有很多文档。简而言之,“良好/完整”测试至少应该是({3}}所描述的良好测试的质量 - 你实际上并没有编写单元测试,但它仍然适用):

  • 准备所有测试条件 - 例如,加载和启动驱动程序
  • 调用/访问测试部分 - 例如:加载页面,并检索表的内容
  • 验证结果是否正确 - 例如:确保表格包含预期数据
  • 清理,例如回滚已修改的数据(如果有),关闭连接等 - 例如,关闭驱动程序
相关问题