0
votes

I'm trying to run TestNG with Selenium in Eclipse.

When the Class file is Run As a TestNG Test, I'm getting the Tests Run = 0.

What could the issue be?

I have the testNg plugin & jar files configured.

testng.xml file:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="WikiTestSuite" verbose="3" parallel="methods">
    <test name="WikiTestCase">
        <classes>
            <class name="com.selenium.WebDriverTest" />
        </classes>
    </test>
</suite>

Classes:

WebDriverTest class:

package com.selenium;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test
public class WebDriverTest extends BrowserInstance{

    private void WebDrive() {

        // navigate to specified browser
        wDriver.get("http://www.wikipedia.com");

        // assert wikipedia is opened
        Assert.assertEquals(wDriver.getTitle(), "http://www.wikipedia.com");
        System.out.println("Page title is correct. PASS!");
    }

}

BrowserInstance class:

package com.selenium;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

public class BrowserInstance {
    // initialize webdriver object reference
    WebDriver wDriver;

    @BeforeMethod
    public void launchBrowser() {

        wDriver = new FirefoxDriver();
        System.out.println("In launchBrowser method");
    }

    @AfterMethod
    public void closeBrowser() {
        wDriver.close();
        wDriver.quit();
        System.out.println("In closeBrowser method");
    }
}

Note:

When I run the testing.xml file as a TestNG Suite, the results are:

[TestRunner] Running the tests in 'WikiTestCase' with parallel mode:methods
[RunInfo] Adding method selector: org.testng.internal.XmlMethodSelector@7426dbec priority: 10
[TestClass] Creating TestClass for [ClassImpl class=com.selenium.WebDriverTest]
[TestNG] Running:
  /Users/developer/Documents/workspace/SeleniumTest1/lib/testng.xml

[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test WikiTestCase on 1  classes,  included groups:[] excluded groups:[]
===== Test class
com.selenium.WebDriverTest
  @BeforeMethod BrowserInstance.launchBrowser()[pri:0, instance:com.selenium.WebDriverTest@4979935d]
  @AfterMethod BrowserInstance.closeBrowser()[pri:0, instance:com.selenium.WebDriverTest@4979935d]
======
===== Invoked methods
=====
Creating /Users/developer/Documents/workspace/SeleniumTest1/test-output/WikiTestSuite/WikiTestCase.html
Creating /Users/developer/Documents/workspace/SeleniumTest1/test-output/WikiTestSuite/WikiTestCase.xml

===============================================
    WikiTestCase
    Tests run: 0, Failures: 0, Skips: 0
===============================================


===============================================
WikiTestSuite
Total tests run: 0, Failures: 0, Skips: 0
===============================================

When I run the .java file as a TestNG Test, the results are:

[TestNG] Running:
  /private/var/folders/q6/xqd3z8fx69z05pbclbx_bj740000gp/T/testng-eclipse--953413131/testng-customsuite.xml


===============================================
    Default test
    Tests run: 0, Failures: 0, Skips: 0
===============================================


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

In both cases, the result is "Tests Run = 0".

Any help will be much appreciated.

Thanks!

EDIT: I found the solution: I used a 'private' method in my .java file.

I was able to rectify the bug but I'm now wondering why TestNG does not run a private method.

Any input is welcome.

Thanks!

3
When you say “I used a 'private' method” do you mean that was the solution? Or that caused the problem? If you have a different question you should update your question.Dour High Arch
private means it is only visible inside of the class. TestNG is external from the class and will only run public methods.Dan Snell

3 Answers

0
votes

You should add "@Test" on test (WebDrive) method. also I am not sure if private Test run. So cross check with public

0
votes

I checked, even the default access modifier does not work, you have to specify as public for testng to run your method.

-3
votes

You need to add @Test annotation to "private void WebDrive()" method or change the access modifier to public i.e from private void WebDrive(). to public void WebDrive()