在带有Java的Selenium中使用@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");

}

DataProviderClass


public class DataProviderList {


    @DataProvider(name="authentication")

    public static Object[][] authentication()

    {

        return new Object[][] {

            {"abc", "123"}, 

            {"xyz", "456"},

            };

    }


}


MYYA
浏览 98回答 1
1回答

尚方宝剑之说

它不起作用,因为您使用批注对构造函数进行了批注,然后使用了继承。@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; }}这将导致将参数从 DataProvider 传递到您的类,并将其另存为实例变量。@FactoryInitializeDriver然后,您可以使用这些变量,就像在您的方法中一样:@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");}编辑:该方法只会执行一次,因为TestNG将测试视为单个测试用例!如果要在每次测试之前登录,则需要使用@BeforeTest@Factory@BeforeMethod
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java