0

Would like to inquire on how to properly override or replace the value of a declared parameter in the Test Suite XML file.

POM.xml:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <systemProperties>
                    <property>
                        <name>mode.debug</name>
                        <value>false</value>
                    </property>                 
                    <property>
                        <name>browser</name>
                        <value>chrome</value>
                    </property>
                </systemProperties>
            </configuration>
        </plugin>
    </plugins>

POM.xml inside the Project 1:

    <plugin>
        <groupId>org.testng</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
        <dependencies>
            <dependency>
                <groupId>org.apache.maven.surefire</groupId>
                <artifactId>surefire-testng</artifactId>
                <version>3.0.0-M5</version>
            </dependency>
        </dependencies>
        <configuration>
            <suiteXmlFiles>
                <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
        </configuration>
    </plugin>

The Test Suite is located in "src\main\java\plans\Test\TestSuite.xml"

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite">
    <parameter name="browsername" value="chrome" />
    <parameter name="headless" value="false" />
    <parameter name="reportBase64toThumbnail" value="false" />
    <parameter name="logCharPerLine" value="110" />
    <parameter name="reportImageQuality" value="med" />
    <parameter name="implicitlyWaitTime" value="1" />
    <parameter name="pageLoadTimeout" value="300" />
    <parameter name="switchUser" value="false" />
    <parameter name="displayXPath" value="false" />
    <parameter name="OverrideApp" value="APP NAME" />
    <parameter name="USER_NAME" value="USER 1" />
    <parameter name="baseurl" value="https://test.com..." />
    <parameter name="envname" value="TEST_ENV" />
    <test name="TEST_NAME">
        <classes>
            <class name="modules.test.scripts.SAMPLE_SCRIPT" />
        </classes>
    </test>
</suite>

Current steps to run:

mvn --projects commons,project1 --also-make clean install -U -DskipTests
cd project1
mvn clean test -fae -Dsurefire.suiteXmlFiles="src\main\java\plans\Test\TestSuite.xml"

In our currentin-house tool if we execute the same Test Suite XML on a different environment, we just provide override name and value.

baseurl = https://test2.com
envname= TEST2_ENV

While exploring Maven and TestNG, I already tried adding -Dbaseurl="https://test2.com" in the command line but the baseurl wasn't replaced or overriden.

Would like to seek further advise for this kind of approach. :)

Update: After checking the maven's debug logs, the baseurl is replaced in "Setting system property" and not in Test Suite's Parameter value.

Paul Co
  • 447
  • 2
  • 9
  • https://maven.apache.org/surefire/maven-surefire-plugin/examples/testng.html#specifying-test-parameters – xerx593 Apr 17 '23 at 11:03
  • Alternative-/-ddition-ly you could "filter your (test) resources" (e.g. testng xml): https://stackoverflow.com/q/24176399/592355 – xerx593 Apr 17 '23 at 11:06

3 Answers3

0

Adding -DParameterName=Value do work but to actually reflect it in the code, I adjusted my getParameter function to check System.getProperty first before getting the Parameter value.

This way, it kind of overriding the value by checking the System Properties first before the Parameter value in Test Suite.

public static String getParameter(String strParam) {
    String tempValue = System.getProperty(strParam);
    if (checkIfNull(tempValue))
        tempValue = getXMLParameter(strParam);
    return tempValue;
}

But if there are a much better fix, please don't hesitate to answer.

Paul Co
  • 447
  • 2
  • 9
0

You can set them as environment variables and read using System.getenv(key). Keep the defaults in a file and override (yaml/property) if System.getenv(key) returns a value. Create a Reader class which reads the file at initialisation, inits your application wide variables, then overrides values from env variables.

niharika_neo
  • 8,441
  • 1
  • 19
  • 31
0

Instead of hard coding the values

<parameter name="baseurl" value="https://test.com..." />
<parameter name="envname" value="TEST_ENV" />

You can replace with values that are passed while running it through cmd

<parameter name="baseurl" value="${baseUrl}." />
<parameter name="envname" value="${envName}" />

While running the test through cmd pass the values -DbaseUrl=https://test.com