![](/img/trans.png)
[英]Cannot run parallel tests with @Factory testng annotation on java
[英]Not able to run tests after using @Factory annotation in Selenium with Java
這是我班級的結構:
package com.gex.base.helper;
public class InitializeDriver extends BrowserFactory
{
HashMap<String, String> authenticationMap;
@Factory(dataProvider="authentication", dataProviderClass=DataProviderList.class)
public InitializeDriver(String userName, String uPassword)
{
authenticationMap=new HashMap<String, String>();
authenticationMap.put("UserName", userName);
authenticationMap.put("Password", uPassword);
}
@BeforeTest
public void Gexlogin()
{
LoginPF objLogin=PageFactory.initElements(BrowserFactory.driver, LoginPF.class);
System.out.println("Logging into GEx");
objLogin.loginToDGEx(authenticationMap.get("UserName"), authenticationMap.get("Password"));
System.out.println("Successfully Logged into GEx");
}
@AfterTest
public void directLogout(){
// logout from application
LogoutPF objLogoutTest = PageFactory.initElements(BrowserFactory.driver, LogoutPF.class);
objLogoutTest.LogOffGEx();
extent.flush();
driver.close();
}
}
LoginToGEx 是另一個類中的函數,如:
public void loginToGEx(String strUsername, String strPassword)
{
username.sendKeys(strUsername)
password.sendKeys(strPassword);
loginButton.click();
System.out.println("Successfully Logged into GEx");
}
數據提供者類
public class DataProviderList {
@DataProvider(name="authentication")
public static Object[][] authentication()
{
return new Object[][] {
{"abc", "123"},
{"xyz", "456"},
};
}
}
在另一個類中,@Test 擴展了 InitializeDriver 類。
public class SampleTest extends InitializeDriver {
public SampleTest(String userName, String uPassword) {
super(userName, uPassword);
// TODO Auto-generated constructor stub
}
@Test
public void CreateNewEngTest() throws InterruptedException
{
test=extent.createTest("Eng Test","Create Eng Test");
-Code (which is working fine before using @Factory)-----------------------
Testng.xml 結構:
<suite name="Sample Project" verbose="1" >
<test name="Sample Test" group-by-instances="true" preserve-order="true">
<classes>
<class name="com.gex.base.testscripts.SampleTest" />
</classes>
</test>
</suite>
我的問題是:在將 @Factory 與 dataprovider 一起使用之前--我的測試運行良好,但是當我使用 @Factory 注釋時,什么也沒有發生。
public SampleTest(String userName, String uPassword) {
super(userName, uPassword);
// TODO Auto-generated constructor stub
}
請指導如何使用@Factory 運行測試
還有另外一件事,如果我用我的@test 場景定義工廠注釋,那么每次我需要登錄..我有很多測試用例,並且想在執行所有 @test 場景后運行登錄,然后注銷並使用另一組用戶名和密碼重新開始.. .不是每次@test 開始時。 這種情況可能嗎? 再次感謝
它不起作用,因為您使用@Factory
注釋對構造函數進行了注釋,然后使用了繼承。
為了保持繼承等,您應該使用@Factory
注釋SampleTest
類
像這樣:
public class SampleTest extends InitializeWebDriver {
private String userName, password;
@Factory(dataProvider="authentication", dataProviderClass=DataProviderList.class)
public SampleTest(String userName, String password) {
super(userName, password)
}
}
public class InitializeDriver extends BrowserFactory {
private String userName, password;
public InitializeDriver(String userName, String uPassword)
{
this.userName = userName;
this.password = password;
}
}
這將導致@Factory
將參數從 DataProvider 傳遞到您的InitializeDriver
類並將其保存為實例變量。
然后你可以像在@BeforeTest
方法中一樣使用這些變量:
@BeforeMethod
public void Gexlogin() {
LoginPF objLogin=PageFactory.initElements(BrowserFactory.driver, LoginPF.class);
System.out.println("Logging into GEx");
objLogin.loginToDGEx(userName, password); //changed to instance variables
System.out.println("Successfully Logged into GEx");
}
編輯: @BeforeTest
方法只會執行一次,因為 TestNG 將@Factory
測試視為單個測試用例! 如果要在每次測試前登錄,則需要使用@BeforeMethod
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.