猿问

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

这是我的班级的结构:


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"},

            };

    }


}

我的问题是:在将 @Factory 与 dataprovider 一起使用之前——我的测试运行良好,但是当我使用 @Factory 注释时,什么也没有发生。在 SampleTest 类中,这个构造函数是自己创建的。这可能是导致问题的原因。


public SampleTest(String userName, String uPassword) {

            super(userName, uPassword);

            // TODO Auto-generated constructor stub

        }

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


如果我用我的@test 场景定义工厂注释,那么每次我需要登录时,还有一件事。我有很多测试用例,想在执行所有@test 场景后运行登录,然后注销并使用另一组用户名和密码重新开始。 .不是每次@test 开始时。这种情况可能吗?再次感谢


慕妹3146593
浏览 160回答 1
1回答

慕村225694

它不起作用,因为您使用注释对构造函数进行了@Factory注释,然后使用了继承。为了保持继承等,你应该SampleTest用@Factory像这样:public class SampleTest extends InitializeWebDriver {&nbsp; &nbsp; private String userName, password;&nbsp; &nbsp; @Factory(dataProvider="authentication", dataProviderClass=DataProviderList.class)&nbsp; &nbsp; public SampleTest(String userName, String password) {&nbsp; &nbsp; &nbsp; &nbsp; super(userName, password)&nbsp; &nbsp; }}public class InitializeDriver extends BrowserFactory {&nbsp; &nbsp; private String userName, password;&nbsp; &nbsp; public InitializeDriver(String userName, String uPassword)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.userName = userName;&nbsp; &nbsp; &nbsp; &nbsp; this.password = password;&nbsp; &nbsp; }}这将导致@Factory将参数从 DataProvider 传递给您的InitializeDriver类并将其保存为实例变量。然后你可以在你的@BeforeTest方法中使用这些变量:@BeforeMethodpublic void Gexlogin() {&nbsp; &nbsp; &nbsp;LoginPF objLogin=PageFactory.initElements(BrowserFactory.driver, LoginPF.class);&nbsp; &nbsp; &nbsp;System.out.println("Logging into GEx");&nbsp; &nbsp; &nbsp;objLogin.loginToDGEx(userName, password); //changed to instance variables&nbsp; &nbsp; &nbsp;System.out.println("Successfully Logged into GEx");}编辑:该@BeforeTest方法只会执行一次,因为 TestNG 将@Factory测试视为单个测试用例!如果要在每次测试前登录,则需要使用@BeforeMethod
随时随地看视频慕课网APP

相关分类

Java
我要回答