簡體   English   中英

我有一個需要在所有測試中使用的測試方法,所以如何在硒的每個測試方法中使用它

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

我有兩個用戶憑據,必須在一個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");
}

對於不同的登錄憑據,可以使用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中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM