0

I have two user credentials which I have to use in one Test class but different Test method. some Test need to be run with x login details and some need to be run with y login details, all are in one suite. and Using data provider I am using these credentials and importing from another class, so how Can I use as per my requirements in @Test...

@Title("Verify Toast Message when supplier trying to submit Quotation without answering any questions.")
@Test(dataProvider = "supplierLogin",dataProviderClass = LoginCredentials.class)
public void verifyToastMessageSupplierSide(String supplierEmail, String supplierPassword) throws Exception{
    Pages.LoginPage().loginButton();
    Pages.LoginPage().EmailField(supplierEmail);
    Pages.LoginPage().PasswordField(supplierPassword);
    Pages.LoginPage().clickLoginButtonwithcredentials();
    Thread.sleep(5000);
    Pages.LoggedInHomeScreen().clickCreatedRFQ();
    Thread.sleep(5000);
    Pages.LoggedInHomeScreen().clickSubmitQuote();
    String toastMessageVerify = Pages.LoggedInHomeScreen().toastMsgVerify();
    System.out.println("Toast Message Waring is: " +toastMessageVerify);
    Thread.sleep(5000);
    Assert.assertEquals(toastMessageVerify,"Some terms are not answered. Please check your quotation.");
} 

@Title("Verify Submit Quote When Supplier Answered All Commercial Terms")
@Test(dataProvider = "supplierLogin",dataProviderClass = LoginCredentials.class)
public void verifySubmitQuotesAfterAnsweringAllTerms(String supplierEmail, String supplierPassword) throws Exception{
    Pages.LoginPage().loginButton();
    Pages.LoginPage().EmailField(supplierEmail);
    Pages.LoginPage().PasswordField(supplierPassword);
    Pages.LoginPage().clickLoginButtonwithcredentials();
    Thread.sleep(5000);
    Pages.LoggedInHomeScreen().clickCreatedRFQ();
    Thread.sleep(5000);
    Pages.LoggedInHomeScreen().clickSubmitQuote();
}

This is my UTIL Class:

package com.pers_aip.Zetwerk;   

import java.io.*;
import java.util.Properties;

public class TestUtil {

    protected static final File file;
    protected static FileInputStream fileInput;
    protected static final Properties prop =  new Properties();

    static{
        file = new File("C:\\Users\\Himanshu\\Documents\\Zetwerk\\src\\test\\java\\com\\pers_aip\\Zetwerk\\LoggedInHomeScreenTest.properties");
        try {
            fileInput = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            System.out.println("Warning: Some Other exception");
        }
        try {
            prop.load(fileInput);
        } catch (IOException e) {
            System.out.println("Warning: Some Other exception");
        }
    }

    public static String getStringFromPropertyFile(String key){
        return prop.getProperty(key);
    }
}
<test name="Test">
    <parameter name="userType" value="buyer"/>
    <classes>
        <class name="com.pers_aip.Zetwerk.TestUtil" />
    </classes>
</test>
<test name="Dev">
    <parameter name="userType" value="supplier"/>
    <classes>
        <class name="com.pers_aip.Zetwerk.TestUtil" />
    </classes>
</test>

buyer.username= buyer3@gmail.com
buyer.password= buyer3@123

suppler.username= supplier3@gmail.com
supplier.password= supplier3@123
@Test
@Parameters({"userType"})
public void sampleTest(String userType) throws Exception {
    String user = TestUtil.getStringFromPropertyFile(userType + ".username");
    TestUtil.getStringFromPropertyFile(userType + ".password");
}
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66

1 Answers1

0

For different login credential you can use the parameter in TestNG.

You have two test and you are passing the parameters called userType = QA,DEV [Refer TestNG.xml]

When you give QA it will enter the QA credential and When you enter DEV it will pass the DEV credential.

TestNG.xml

<suite name="Suite">
    <test name="Test">
    <parameter name="userType" value="QA"/>
        <classes>
            <class name="automationFramework.TestngParameters" />
        </classes>
    </test>
    <test name="Dev">
    <parameter name="userType" value="DEV"/>
        <classes>
            <class name="automationFramework.TestngParameters" />
        </classes>
    </test>
</suite>

Create test.properties file

QA.username=test
QA.password=pass

DEV.username=testone
DEV.password=testpass

Code to read the Property file values

protected static final File file;
protected static FileInputStream fileInput;
protected static final Properties prop =  new Properties();

static{
    file = new File("type your property file location");
    try {
        fileInput = new FileInputStream(file);
    } catch (FileNotFoundException e) {
        throw new CustomException("File not found" +e);
    }
    try {
        prop.load(fileInput);
    } catch (IOException e) {
        throw new CustomException("IO Exception" +e);
    }
}

public static String getStringFromPropertyFile(String key){
    return prop.getProperty(key);
}

On the @Test Annotation set the userType on the xml file and retrieve the value using property file logic as above.

@Test
@Parameters({"userType"})

public void sampleTest(String userType) throws Exception {
    String user = TestUtils.getStringFromPropertyFile(userType + ".username");
    String pwd = TestUtils.getStringFromPropertyFile(userType + ".password");
}

You can also keep the login operation in @BeforeClass as per your convenient and flexibility.

Ashok kumar Ganesan
  • 1,098
  • 5
  • 20
  • 48
  • I tries with the code u have shared, but I am getting error. – Himanshu Midha Aug 06 '18 at 05:41
  • Test ignored. log4j:WARN No appenders could be found for logger (org.apache.commons.beanutils.converters.BooleanConverter). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. – Himanshu Midha Aug 06 '18 at 05:41
  • It looks like a warning message still you clear by referring this https://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j Did you face any other error. If so post that by editing your question. – Ashok kumar Ganesan Aug 06 '18 at 05:51
  • Let us take the discussion on the following [chat room](https://chat.stackoverflow.com/rooms/177463/automation) – Ashok kumar Ganesan Aug 06 '18 at 06:09
  • I am unable yo talk – Himanshu Midha Aug 06 '18 at 06:13
  • @Himanshu It is a public accessible only. Alright, you have to use TestNG parameter in (at)Test classes and you have to call the (at)Test class in TestNG.xml not the *TestUtil class* Example: if you class name is testone *com.pers_aip.Zetwerk.testone* – Ashok kumar Ganesan Aug 06 '18 at 06:17
  • Oh okay, Let me try once again – Himanshu Midha Aug 06 '18 at 06:24