问题描述
我是新的,我在学习webdriver和java :)
我有初学者问题。
我创建了类whit senKeys()
, click()
等) - 我使用 pagefactory
。
b $ b LoginPage
...
我的主类 AddNewLeadTest
是一个类,其中i从具有定位器的类初始化方法,我这样做:
HomePage hp = new HomePage(driver);
hp.loginButton.click()
我想这样做: / p>
HomePage.loginButton.click()
更快,我不必在 AddNewLeadTest
中创建新对象 xxxPage
class。
我应该如何编写class / method来简化创建新对象?
想在我的项目中使用(在C#中),但我不知道如何在Java中使用它。见下文。
public static class Pages
{
private static T GetPage< T>()其中T:new()
{
var page = new T()
PageFactory.InitElements(Browser.Driver,page);
return page;
}
public static AboutPage关于
{
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>(); }
}
在此示例中,调用方法如下:
public void CanRegisterNewAccount()
{
Pages.Register.Goto();
Pages.Register.RegisterNewUser();
}
因此它非常简单明了。
> / * ----------------------------------------- ------------------------------- * /
首页gotologin = new首页(驱动程序);
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([email protected]);
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();
...
...
...
$ b b
我从
我认为这是一个解决方案,我应该使用我的测试
我显示U现在这个测试的所有部分
Pages.class
public static class Pages
{
private static T GetPage< T>()其中T:new()
{
var page = new T
PageFactory.InitElements(Browser.Driver,page);
return page;
}
** public static LoginPage Login
{
get {return GetPage< LoginPage>(); }
} **
public static AboutPage关于
{
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();
}
和AddNewLeadTest.class
public void CanGoToLoginPage()
{
Pages.Home.Goto();
}
这是真正糟糕的解决方案吗
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.
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("[email protected]");
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=DO8KVe00kcUI 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??
这篇关于创建类对象的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!