0

Research : Selenium/Testng - IF statement not working when using parameter from testng.xml already gone through the above post , but could not find the answer.

Problem : Here's my TestNG.xml

<test name="abc">
    <classes>
        <class name="com.gunjan.automation.test.abcTesting">
            <parameter name="shareType" value="twitter, facebook"></parameter>
        </class>
    </classes>
</test>

Here's my snippet of Java file where I have put Test Methods

@Test(groups = {"web"}, dataProvider = "getData", priority = 7)
public void verifyShare(String shareType) {
    String data = shareType;
    if(data.equals("facebook"))
        System.out.println("its facebook");
    if(data.equals("twitter"))
        System.out.println("its twitter");
    System.out.println(shareType); }

Here's is the Data Provider method

@DataProvider(name = "getData")
public Object[][] getData(ITestContext context) {
    String paramter = context.getCurrentXmlTest().getClasses().get(0).getLocalParameters().get("shareType");

    String[] paramArray = paramter.split(",");
    Object[][] finalReturn = new Object[paramArray.length][1];
    for(int i = 0; i < paramArray.length; i++)
    {
        finalReturn[i][0] = paramArray[i];
    }
    return finalReturn; }

Since the DataProvider returns an array of object[], the Test method verifyShare, should print the following.

its twitter
twitter
its facebook
facebook

but the actual output I get is

its twitter
twitter
facebook

its facebook is missing.

that is, it skips the if condition, the second time it executes the test method verifyShare.

Also if I add three values in TestNG.xml like value = "twitter, facebook, whatsapp", the if condition is skipped for facebook & WhatsApp values.

Any help is most appreciated

1 Answers1

0

Got it. The problem was silly, I wasn't using Trim(), on the Paramter. as a result the value was " facebook" instead of "facebook".

:D :P