简体   繁体   中英

How can I get parameters into this test run using org.testng.TestNG?

I am testing a web client application using Java 8 Update 151 & Selenium 3.8.1. and TestNG. I have to start one test from within an already running test.

Here is my code for that:

ITestNGListener testListenerAdapter = null;
TestNG testNG = new TestNG();
testNG.addListener(testListenerAdapter);
testNG.setTestClasses(new Class[] { tests.login.LoginTest.class });
testNG.run();

My tests run using a bunch of parameters defined in the testng.xml file, but this does not pick those parameters up, which is causing the test to fail.

This is the top of my testng.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="tests">
    <parameter name="browser" value="firefox" />
    <parameter name="url" value="URL goes here!"/>
    <parameter name="printToFile" value="false" />
    <parameter name="trace" value="false" />
    <parameter name="opt" value="true" />
    <parameter name="customer" value="Demo" />
    <parameter name="network" value="NSG Designated Streets (Type 1/2)" />
    <parameter name="buildNo" value="312" />
    <listeners>
         <listener class-name="listeners.TestNGCustomReportListener" />
    </listeners>
    <test name="firefoxTest">
        <parameter name="browser" value="firefox" />
        <parameter name="url" value="URL goes here!" />
        <parameter name="printToFile" value="false" />
        <parameter name="trace" value="false" />
        <parameter name="opt" value="true" />
        <parameter name="customer" value="Demo" />
        <parameter name="network" value="NSG Designated Streets (Type 1/2)" />

I tried adding @Parameters at the top of the test, as shown here, but that did not work. No parameter was picked up.

public class AutoLogOutTest extends CrossBrowserTest {
@Parameters("url")
@Test(groups = { "all", "login", "simple" })
public void autoLogOut(@Optional("") String url) {

This is described here, but does not work as this author suggests it should: http://toolsqa.com/selenium-webdriver/testng-parameters-data-provider/

For some reason testng 6.4 and up no longer supports parameter inside the method: Is there a way to specify parameters for included methods in TestNG suite.xml?

How can I get my parameters into this test?

First of all, you are not using DataProvider anywhere in the Test you mentioned. Second you need to choose, either run the XML or the class file, you cannot run both. If you want to use XML parameters, you need to use the XML file, not the LoginTest.class.

You can use the XML file like this:

 XmlSuite suite = new XmlSuite();
 suite.setName("TmpSuite");
 XmlTest test = new XmlTest(suite);
 test.setName("TmpTest");
 List<XmlClass> classes = new ArrayList<XmlClass>();
 classes.add(new XmlClass("test.failures.Child"));
 test.setXmlClasses(classes) ;

And then you can pass this XmlSuite to TestNG:

List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.run();

The class is not aware about the XML file the way you start TestNG.

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