@Optional in TestNG Parameter is overriding the values that I'm passing in XML

648 views Asked by At

When I try to use multiple parameters using @Parameter, @Optional needs to pass the value only when I'm not providing the value for the parameter in testng XML.

But in the below scenario, no matter what I pass in the XML for the "Optional" parameter the values that I've given in @Optional is being taken into consideration.

XML that I'm using:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="Cross Browser Multi-Paramet - Suite">

    
    <test name="Cross Browser Multi-Paramet - Firefox Test">
    <parameter name="Browser" value="Firefox" />
    <parameter name="URL" value="https://www.oneplus.in/" />
    <classes>
        <class name="CrossBrowser.CrossBrowserMultipleParamter">
        </class>

    </classes>


</test> <!-- Cross Browser Multi-Paramet - Test -->
</suite> <!-- Cross Browser Multi-Paramet - Suite -->

And the code is:

package CrossBrowser;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossBrowserMultipleParamter {

public static WebDriver d;

@Test
@Parameters({ "Broswer", "URL" })
public static void cbmultpara(@Optional("Chrome") String browsername, String siteurl) throws Exception {

    if (browsername.equalsIgnoreCase("Firefox")) {
        System.out.println("Browser name is" + browsername);
        d = new FirefoxDriver();
    } else if (browsername.equalsIgnoreCase("Chrome")) {
        System.out.println("Browser name is" + browsername);
        d = new ChromeDriver();
    }
    d.manage().window().maximize();
    d.get(siteurl);
    System.out.println("Loaded the page sucessfully");
    d.quit();
}

}

Though I'm passing value as "Firefox" it is launching Chrome.

Can someone clarify?.

1

There are 1 answers

8
UnknownBeast On BEST ANSWER

In order load the parameters from the testng.xml properly, you need to add surefire plugin configuration as shown below in your pom.xml file. Please verify that and let me know if you still face the same issue.

Note: Please mark this answer as accepted, if it is helpful.

POM.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>testng</groupId>
    <artifactId>annotation</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>

        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.3.0</version>
            <scope>test</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.21.0</version>
                <configuration>
                    <!-- TestNG Suite XML files list for test execution -->
                    <suiteXmlFiles>
                        <suiteXmlFile>testng.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>