使用Java在Selenium中使用@Factory注释后无法运行测试

问题描述 投票:1回答:1

这是我班级的结构:

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注释时没有任何反应..在SampleTest类中,这个构造函数自己创建..可能是这导致了问题。

public SampleTest(String userName, String uPassword) {
            super(userName, uPassword);
            // TODO Auto-generated constructor stub
        }

请指导如何使用@Factory运行测试

如果我用@test方案定义工厂注释然后每次我需要登录时还有一件事...我有很多测试用例并且想要执行登录一旦执行所有@test scenatios然后注销并再次使用另一组用户名和密码启动。 。@test每次开始时都不是。这种情况可能吗?再次感谢

java selenium testing selenium-webdriver testng-dataprovider
1个回答
1
投票

它不起作用,因为您使用@Factory批注对构造函数进行了注释,然后使用了继承。

为了保持继承等,你应该用SampleTest注释@Factory

像这样:


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

© www.soinside.com 2019 - 2024. All rights reserved.