使用Seleinum PageObjects自动执行登录过程 [英] Automating Login Process Using Seleinum PageObjects
问题描述
我是Selenium的新手,我开始自动执行登录过程。为此,我使用了PageObjects。我有一个 LoginPage
,它具有这样的方法:
I'm very much new to Selenium and I started automating the login process. I used PageObjects for this purpose. I have a LoginPage
which has a method like this:
public MyHomePage login(String username,String password)
{
username.sendKeys(username);
password.sendKeys(password);
signInButton.click();
return PageFactory.initElements(driver,MyHomePage.class);
}
使用正确的用户名和密码,效果很好。但是我不想重复相同的代码来处理无效案件。当然对于无效情况,返回的对象将是相同的 LoginPage
。我希望我可以这样编写 login
方法。
This works well with correct username and password. But I dont want to repeat the same code for handling invalid cases. Surely for invalid cases, the object that is returned will be the same LoginPage
. I wish I could code my login
method that way.
有什么最佳实践方法吗?由于登录非常普遍,因此应针对我的情况采用更好的方法。
Any best practice way to so? Since login is very common , there should be a better approach for my case.
还是我必须为无效的情况编写单独的代码?
Or is it I have to write separate code for invalid case?
推荐答案
我会将进行登录的部分重新解释为登录和登录失败之间常见的另一种方法,例如:
I would refact the part that does the login to another method which can be common between login and login unsuccessfully as such:
private void login(String username,String password){
username.sendKeys(username);
password.sendKeys(password);
signInButton.click();
}
public MyHomePage login(String username,String password)
{
login(username, password);
return PageFactory.initElements(driver,MyHomePage.class);
}
public LoginPage loginUnsuccessfully(String username,String password)
{
login(username, password);
return this;
}
这篇关于使用Seleinum PageObjects自动执行登录过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!