Selenium Java - Page Object Model Query

江枫思渺然 提交于 2020-07-22 21:40:53

问题


Imagine there is a page say http://google.com/AddUser and here you enter details for a record and click save. Once you do this the page redirects to

http://google.com/userList

where you can see list of users including the new record you just entered.

If we are going by page object model, the method to enter details and save record should exist on AddUser.java and the method to validate if the record was actually saved and displayed should be on UserList.java

If we consider addUser and userList are the corresponding objects for both classes it will be something like below :

addUser.enterDetailsSaveRecord();

userList.validateSavedRecord();

So in my Test case i would need to call 2 separate methods, one for the action and other to validate.

Both AddUser.java and UserList.java have BasePage.java as the superclass. Is there a way to club them both into a single method or is there something I'm going about in a wrong way and is there a better approach?

Thank you


回答1:


Using PageFactory you are having 2 PageObjects as AddUser.java and UserList.java. So assuming you are passing the appropriate arguments while invoking the methods, the following works for you:

addUser.enterDetailsSaveRecord();
userList.validateSavedRecord();

But a word from Best Practices, Assertions should be done in a seperate utility/package/class which is in similar line with @JeffC comment:

Best practice is to keep the validation code out of the page objects

Hence, you should be creating a seperate common utility/package/class which will handle all the Assertions. You can call the class containing the Assertions from your PageObject class as well.

So your entire Test Environment will contain 3 Packages. One package containing the main()/@Test class, one package containing the PageObjects e.g. AddUser.java and one Utility package with the class for containing the Assertions e.g validateSavedRecord().




回答2:


I don't see anything wrong with your approach either, although, my approach is usually to logically separate functional interaction with the application from testing functions. So, I would still have

addUser.enterDetailsSaveRecord();

but for userList I would use

UserItem foundUser = userList.findUser(targetUser);

where UserItem is a row in the table of users. My test would then verify that foundUser was correct.

Although this ends up with a few more lines of code, it results in the object model cleanly and simply modeling the object under test, and the testing code being found in the test itself.




回答3:


Your approach is correct. These methods should belong to different pages. Please update method as:

public UserList enterDetailsSaveRecord() {
// your code to save the details
return new UserList();
}

thus you can use it as:

addUser.enterDetailsSaveRecord().validateSavedRecord()


来源:https://stackoverflow.com/questions/46170139/selenium-java-page-object-model-query

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