简体   繁体   中英

Selenium Java - Page Object Model Query

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

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.

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 eg AddUser.java and one Utility package with the class for containing the Assertions eg validateSavedRecord() .

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM