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

  • 本文关键字:测试方法 测试 一种 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");
}

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

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

当您提供QA时,它将输入QA凭证,当您输入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>

创建测试属性文件

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 Annotation上,在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中。

最新更新