Class which create object of classes

白昼怎懂夜的黑 提交于 2019-12-10 10:48:27

问题


I'm new and I'm learning webdriver and java:) I have beginner question.

I created classes whit locators(findBy) and methods working with this locators (senKeys(), click(), etc.) - I use pagefactory.

HomePage
LoginPage
...

My main class AddNewLeadTest is a class where i initialize method from classes with locators, and I do it like this:

HomePage hp = new HomePage(driver);
hp.loginButton.click() 

I would like do to this like this:

HomePage.loginButton.click()

It's faster and I will not have to create in AddNewLeadTest new object for everyone xxxPage class.

How should I write class/method to simplify creating new object?

I found example I would like to use in my project(in C#), but I don't know how to use it in Java. See below.

public static class Pages
{
    private static T GetPage<T>() where T : new()
    {
        var page = new T();
        PageFactory.InitElements(Browser.Driver, page);
        return page;
    }

public static AboutPage About
{
    get { return GetPage<AboutPage>(); }
}

public static TopNavigationPage TopNavigation
{
    get { return GetPage<TopNavigationPage>(); }
}

public static HomePage Home
{
    get { return GetPage<HomePage>(); }
}

public static ContactPage Contact
{
    get { return GetPage<ContactPage>(); }
}

public static RegisterPage Register
{
    get { return GetPage<RegisterPage>(); }
}

public static LoginPage Login
{
    get { return GetPage<LoginPage>(); }
}

public static ManageAccountPage ManageAccount
{
    get { return GetPage<ManageAccountPage>(); }
}

In this example calling method is like this:

public void CanRegisterNewAccount()
        {
            Pages.Register.Goto();
            Pages.Register.RegisterNewUser();    
        }

So its very simple and clear.


回答1:


So how can I write it another way, i think it dont look good..

/*------------------------------------------------------------------------*/

    HomePage gotologin = new HomePage(driver);

    gotologin.gotoLoginPage();

    /*------------------------------------------------------------------------*/
    LoginPage login = new LoginPage(driver);

    login.loginUserToBase("login","pass");

    /*------------------------------------------------------------------------*/

    addLeadPage newLead = new addLeadPage(driver);

    newLead.gotoLeadsPage();

    newLead.addNewLead();

    newLead.enterLeadName("Tolek");

    newLead.enterLeadLastName("Banan");

    newLead.enterLeadTitle("Boss");

    newLead.enterLeadEmail("tolekbanan@gmail.com");

    newLead.enterLeadMobile("123456789");

    newLead.enterLeadWorkPhone("9876541");

    newLead.enterLeadStreet("Kowalskiego");

    newLead.enterLeadCity("Kraków");

    newLead.enterLeadCode("12-123");

    newLead.enterLeadRegion("Małopolska");

    newLead.enterLeadTag("testBase");

    newLead.clickSubmit();

    /*------------------------------------------------------------------------*/

    LeadPage checkStatus = new LeadPage(driver);

    LeadPage.checkUsrStat();

    ...
    ...
    ...

I was inpired by method from my before post from https://www.youtube.com/watch?v=DO8KVe00kcU I thought that is a solution i should use im my tests

I show U now all part of this test.

Pages.class

public static class Pages
{
    private static T GetPage<T>() where T : new()
    {
        var page = new T();
        PageFactory.InitElements(Browser.Driver, page);
        return page;
    }

  **  public static LoginPage Login
    {
        get { return GetPage<LoginPage>(); }
    }**



    public static AboutPage About
    {
        get { return GetPage<AboutPage>(); }
    }

    public static TopNavigationPage TopNavigation
    {
        get { return GetPage<TopNavigationPage>(); }
    }

...

}

LoginPage.class

 public class LoginPage
{

    public void Goto()
    {
        Pages.TopNavigation.LogIn();
    }
}

TopNavigation.class

    [FindsBy(How = How.LinkText, Using = "Log in")]
        private IWebElement logInLink;


 public void LogIn()
        {
            logInLink.Click();
        }

and AddNewLeadTest.class

public void CanGoToLoginPage()
        {
            Pages.Home.Goto();
        }

Is it really bad solution??



来源:https://stackoverflow.com/questions/32902027/class-which-create-object-of-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!