https://cypress.io logo
#best-practices
Title
# best-practices
a

astonishing-salesclerk-68668

04/06/2022, 6:14 PM
I think there's a bit of ambiguity in the community about what "Page Object" really means. As a holdover from Selenium, when you actually had a class to represent each page and the test had to use a
new MyPageObject
constructor to execute, I very much don't like. However, importing/exporting JSON objects in something like a
myPageTestIds.js
so you can call
myPageTestIds.locator
is great!
g

gifted-tomato-54665

04/06/2022, 6:22 PM
Do you have an example of this you could share?
a

astonishing-salesclerk-68668

04/06/2022, 6:26 PM
sure, just off the cuff and in pseduocode, let's say you have an eCommerce site. you want to test your shopping cart. you could create a file called
shoppingCartTestIds.js
that lives, really anywhere your team decides. Inside of it you might have
Copy code
export const shoppingCartIds = {
  checkoutButton: "locator",
  itemsInCartTable: "locator",
  paymentInformation: "locator",
}
then in your
testCart.spec
file you can
import shoppingCartIds from /path/to/shoppingCartTestIds.js
and use
cy.get(shoppingCartIds.checkoutButton).click()
etc
this isn't limited to just locators, either. any repeatable actions like maybe
completeCheckout()
can be a function with several
cy
actions. Of course in that scenario it would make more sense to name the file something like
shoppingCartPage.js
or whatever
g

gifted-tomato-54665

04/06/2022, 6:32 PM
Awesome. Thanks!
3 Views