我有4页具有非常相似的元素和功能。但是,元素都有不同的ID,具体取决于您所在的页面。目前,我将它们构建如下(例如A类和B类)。我想重构这个,因为所有方法在两个类之间是相同的,但我不知道如何做,因为元素有不同的ID(我使用Page Factory,但我也愿意不使用它。)
非常感谢任何见解!
public class A : BaseClass
{
[FindsBy(How = How.Id, Using = "ALoginId"]
public IWebElement Login {get; set;}
[FindsBy(How = How.Id, Using = "APassword"]
public IWebElement Password {get; set;}
[FindsBy(How = How.Id, Using = "ALoginBtn"]
public IWebElement LoginBtn {get; set;}
public void Login()
{
Login.SendKeys("username");
Password.SendKeys("password");
LoginBtn.Click();
}
}
public class B : BaseClass
{
[FindsBy(How = How.Id, Using = "BLoginId"]
public IWebElement Login {get; set;}
[FindsBy(How = How.Id, Using = "BPassword"]
public IWebElement Password {get; set;}
[FindsBy(How = How.Id, Using = "BLoginBtn"]
public IWebElement LoginBtn {get; set;}
public void Login()
{
Login.SendKeys("username");
Password.SendKeys("password");
LoginBtn.Click();
}
}
答案 0 :(得分:0)
如果有类似其他字段,例如name,class,则可以使用它而不是id。
此外,您可以使用此方法:
public class A : BaseClass
{
private String loginWindow;
[FindsBy(How = How.Id, Using = "ALoginId"]
public IWebElement Login {get; set;}
[FindsBy(How = How.Id, Using = "APassword"]
public IWebElement Password {get; set;}
[FindsBy(How = How.Id, Using = "ALoginBtn"]
public IWebElement LoginBtn {get; set;}
[FindsBy(How = How.Id, Using = "BLoginId"]
public IWebElement Loginb {get; set;}
[FindsBy(How = How.Id, Using = "BPassword"]
public IWebElement Passwordb {get; set;}
[FindsBy(How = How.Id, Using = "BLoginBtn"]
public IWebElement LoginBtnb {get; set;}
public A(Srting loginwindow){
this.loginWindow; = loginwindow;
}
public void Login()
{
if(loginwindow.equals("A")) {
Login.SendKeys("username");
Password.SendKeys("password");
LoginBtn.Click();
}
elseif(loginwindow.equals("B")){
Loginb.SendKeys("username");
Passwordb.SendKeys("password");
LoginBtnb.Click();
}
else{
throws new Execption("Please provide correct login Window name")
}
}
}
答案 1 :(得分:0)
见下面的例子..
我的测试是
//MAximize the Screen
driver.manage().window().maximize();
//Go to Gmail Login Page
SignInPage SignInPage = WebUtils.GoToSignInPage(driver);
//Sign in to Login page -Send Username
SignInPage.SendkeysMethodForSignInPAge(driver, By.cssSelector("input[id='Email']") , "kishanpatelllll.8@gmail.com" );
//Click on Next
SignInPage.ClickToLogin(driver, By.cssSelector("input[id='next']"));
//Wait for password field to be visible
SignInPage.WaitForElementTobeVisible(driver, By.cssSelector("input[id='Passwd'][type='password']"));
所以当我调用方法SendkeysMethodForSignInPAge时,我不会在@Test中写它。
请参阅SendkeysMethodForSignInPAge方法:
public class SignInPage {
public void SendkeysMethodForSignInPAge(WebDriver driver, By by, String s) {
WebUtils.Sendkeys(driver,by,s);
}
}
所以sendkeys是常用的方法,它将在任何地方使用,因此它将在类calle webutils中。
public class WebUtils {
public static void Sendkeys(WebDriver driver, By by, String s) {
WebElement Element = driver.findElement(by);
Element.clear();
Element.sendKeys(s);
}
}
我创建了一个新类,并在那里定义了它。这是基本流程。希望你能谈谈这个。回复我,如果你仍然卡住了。快乐学习: - )