javaautomated-teststestngtestng-eclipse

How can I use testNG to run an already created Java program in a test?


I have a Java program that is already created. I need to know the easiest way to run this entire program in testNG. Something like this:

@Test
public void executeSessionOne(){
    runJavaProgram();
}

Is this possible?

EDIT: I have a Java file called Main.java and another called Tools.java. I run the Main.java program and uses Selenium to test a webpage. Tools.java just has some functions that I need.

I also tried this:


public class RealTest {
    
    @Test    
    public void run1() throws MalformedURLException{           
        new Main();
    }
    
    @Test    
    public void run2() throws MalformedURLException{           
        new Main();
    }
    
    @Test    
    public void run3() throws MalformedURLException{           
        new Main();
    }
    
    @Test    
    public void run4() throws MalformedURLException{           
        new Main();
    }
}
    public Main() throws MalformedURLException {
        String arg [];
        arg = new String []{"300"};
        caps = new DesiredCapabilities();
        caps.setCapability("os", "Windows");
        caps.setCapability("os_version", "10");
        caps.setCapability("browser", "Chrome");
        caps.setCapability("browser_version", "80.0 beta");
        caps.setCapability("browserstack.local", "false");
        caps.setCapability("browserstack.selenium_version", "3.5.2");

        caps.setCapability("name", "selenium test");

        //driver = new RemoteWebDriver(new URL(URL), caps);
        
        chromeOptions = new ChromeOptions();
        String chromeDriverPath = "resources/chromedriver.exe";
        System.setProperty("webdriver.chrome.driver", chromeDriverPath);
        driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        
        try {
            before(arg);
            test();
            after();
        } catch (Exception e) {
            Waits.pause(e.getMessage());
            driver.quit();
        }
        
    }

but then the tests get combined and it doesn't work right.


Solution

  • It should work like this :

    public class test {
    public static void main(String[] args) {
    
        String a = "_SEQ_1";
        a = a.substring (5);
        System.out.println (a);
    
    }
    

    }

    @Test
    public void testMethod(){
        test testObj = new test();
        testObj.main(new String[]{});
    }