简体   繁体   中英

My selenium tests aren't running from either cmd or Jenkins when executed via testng Framework. Results: Total tests run: 0, Failures: 0, Skips: 0

New to Selenium and automation.

First of all my tests are running fine within Eclipse when I run those with Junit. But if I kick off the test via Jenkins or CLI I get the result: Total tests run: 0, Failures: 0, Skips: 0

Now I could be getting the structure of the test wrong or I maybe doing something totally wrong. I have looked all over and all the solutions online don't seem to fix my issue.

My test is just a simple login test that is all.

I think I may have the frameworks mixed up or using them together in the wrong way. I am using Testng to kick off my selenium tests I think . Again I put my hands up here and admit I'm new to this so I'm hoping someone can tell me where I'm going wrong. I do notice in my actual selenium test file I'm not using any Testng functions or anything but just the selenium web driver. I have listed my code files below and results I'm getting from testing.

Again apologies if I'm missing a simple step or I'm trying something that isn't possible, I just need some help or a point in the right direction from an expert. Thanks for any help in advance.

test

I have remove actual URL and creds for security reasons.

    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> 

Results from CLI or Jenkins Test

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


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

Finished: SUCCESS

Again I do realize I'm not using any Testng in my actual test, but the aim of my test is to just kick off selenium to run the test. Maybe I'm going about it in the wrong way.

The selenium test is valid and I can launch from within Eclipse.

Thanks for any help in advance!

Although this is perhaps better suited as a comment, there's a couple of things to clarify because import org.junit.Test, Before, After, etc indicates that you've written JUnit tests, but you're mentioning TestNG, so it depends on what you really want to do:

  • write JUnit tests and execute them via TestNG : while somewhat strange (who am I to judge?!), according to the docs it is possible by making sure that JUnit is available in the classpath during execution, and setting the junit property to true, eg <test name=name="Learning Jenkins" junit="true">

  • write and execute TestNG tests : replace JUnit imports with the TestNG counterparts , eg org.testng.annotations.Test

Again I do realize I'm not using any Testng in my actual test, but the aim of my test is to just kick off selenium to run the test. Maybe I'm going about it in the wrong way.

For the time being you're using JUnit, not TestNG :-). Any way, with fail(verificationErrorString) you're actually using it, although your test is probably incomplete just by verifying there are no error messages. You should add some assertions to check that whatever data is present on the page you loaded, is correct. You can go through the TestNG-Selenium quick tutorial , or any other docs you'd like, there's tons of them out there. In short, a "good/complete" test should, at least (qualities of a good test described here - you're not actually writing unit tests, but it still applies):

  • prepare all conditions for testing - eg, load and start driver
  • call/access the tested part - eg: load the page, and retrieve the content of a table
  • verify that the results are correct - eg: make sure the table contains expected data
  • clean up, such as rollback modified data if any, close connections, etc - eg, close driver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM