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?.