kanna
kanna

Reputation: 23

Paramerization with TestNG XML to run specific Test suite

Please excuse me, I am not good at English, But I will try to put this question in the best way.

Requirement: i have a HTML file, which contains certain controls for test users to run selenium tests.

Example : We have 4 modules like CAR,HOTEL,RAIL,FLIGHT and Environment details (DEV,QA,PROD). if user wants to run CAR module on DEV environment , he can select the same from HTML page.

once user selects and click on RUN button ,command file should run and based on the user selection, selenium tests should launch and run through TESTNG.xml

Am completely new for selenium, I don't know how configure TESTNG XML and to control test suite in the test classes.

In my case ,If I want to run FLIGHT Tests on PROD environment , what configuration I need to be done on TESTNG XML and how to call test set of FLIGHT.

Second case, suppose If I wan to run all 4 modules on PROD, what is the configuration for TESTNG XML and test suite of CAR,FLIGHT,HOTEL,RAIL tests?

all these i want to run in command prompt without opening eclipse.

Upvotes: 1

Views: 2871

Answers (2)

Devdun
Devdun

Reputation: 767

  1. First you have to verify parameters in TestNg XML file
  2. Then You have to configure them before test you can get details from config file reader
  3. You have to pass your parameter details via properties file to config file reader

Please find below example

This is the testng xml file which you want to pass parameters

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="false" thread-count="6" verbose="1">
    <!--Values Chrome Firefox HLChrome HLFirefox HTMLUnit phantomJS-->
    <parameter name="browser" value="Chrome"/>

    <!--Values QA_URL DEV_URL PROD_URL-->
    <parameter name="URL" value="QA_URL"/>

    <!--Values QA Dev Uat Prod -->
    <parameter name="Env" value="QA"/>

    <test name="Add Client">
        <classes>
            <class name="tests.client.AddUser_Valid"/>
        </classes>
    </test>
</suite>

This is the way that you can get parameter details

@BeforeTest
@Parameters({"browser","URL","Env"})
public void setupBrowserAndURL(String browser,String URL,String Env)  {
 if(Env.equalsIgnoreCase("QA")){
            if(URL.equalsIgnoreCase("QA_URL")){
                driver.get(configFileReader.applicationUrl_QA());
                commonOperations.waitUntilPageLoaded(driver);
}

You have to add config file reader to get details from properties file

public String applicationUrl_QA_URL() {
        String applicationUrl = properties.getProperty("applicationUrl_QA");
        if(applicationUrl != null) return applicationUrl;
        else throw new RuntimeException("url not specified in the Configuration.properties file.");
    }

    public String applicationUrl_Dev_URL() {
        String applicationUrl = properties.getProperty("applicationUrl_Demo");
        if(applicationUrl != null) return applicationUrl;
        else throw new RuntimeException("url not specified in the Configuration.properties file.");
    }

You have to add URL s in your properties file

applicationUrl_QA = http://xxxx.qa.xxxx.cloud applicationUrl__Demo =http://xxxx.demo.xxxx.cloud

Upvotes: 1

Greg
Greg

Reputation: 4045

Assuming you are wanting to use maven, here is one way to do it, there may be other/easier ways:

You create a variable for testEnv, and four suite files (one for each suite), like below in your pom.xml:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.4.2</version>
            <configuration>
                <systemPropertyVariables>
                    <environment>${testEnv}</environment>
                </systemPropertyVariables>
                <suiteXmlFiles>
                    <suiteXmlFile>${suiteFile1}</suiteXmlFile>
                    <suiteXmlFile>${suiteFile2}</suiteXmlFile>
                    <suiteXmlFile>${suiteFile3}</suiteXmlFile>
                    <suiteXmlFile>${suiteFile4}</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

then the RUN button can build/execute command line parameters as follows:

mvn -f pom.xml -DtestEnv=DEV -DsuiteFile1=src/test/testsuite/CAR.xml -DsuiteFile2=src/test/testsuite/HOTEL.xml clean install

Upvotes: 0

Related Questions