我有一个需要在所有测试中使用的测试方法,所以如何在硒的每个测试方法中使用它

时间:2018-08-06 04:02:30

标签: java selenium testng

我有两个用户凭证,必须在一个Test类中使用,但是使用不同的Test方法。某些测试需要使用x登录详细信息运行,而某些测试需要使用y登录详细信息运行,所有这些都在一个套件中。和使用数据提供程序,我正在使用这些凭据并从另一个类中导入,那么如何根据我在@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();
}

这是我的UTIL课:

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

1 个答案:

答案 0 :(得分:0)

对于不同的登录凭据,您可以在TestNG中使用该参数。

您有两个测试,并且正在传递名为 userType = QA,DEV 的参数  [请参阅TestNG.xml]

当您提供质量检查时,它将输入质量检查凭据;当您输入DEV时,它将通过DEV凭据。

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>

创建 test.properties 文件

QA.username=test
QA.password=pass

DEV.username=testone
DEV.password=testpass

用于读取属性文件值的代码

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

在@Test注释上,在xml文件上设置userType,然后使用上述属性文件逻辑检索值。

@Test
@Parameters({"userType"})

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

您还可以根据自己的方便性和灵活性将登录操作保留在 @BeforeClass 中。