简体   繁体   中英

I have one test method that need to use in all the test, so how to use that in every test method in selenium

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");
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM