Last active
August 29, 2015 14:06
-
-
Save daluu/8e4c43d653c07eb0c57a to your computer and use it in GitHub Desktop.
Selenium page object model to model cart items as objects for testing code snippet examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// snippet written for and relates to: | |
// http://autumnator.wordpress.com/2014/09/23/selenium-page-objects-beyond-pages-like-a-cart-object/ | |
/** | |
* Cart item implementation as a data structure | |
* containing a related set of WebElements to work with | |
* | |
* These cart items reside in and can be extracted from | |
* shopping cart page object to then be worked with. | |
**/ | |
public class CartItem{ | |
public WebElement nameLabel; | |
public WebElement quantityField; | |
public WebElement removeButton; | |
//...any additional cart item related elements | |
CartItem(WebElement nameLabel, WebElement quantityField, WebElement removeButton, ...){ | |
//or make as varargs constructor? | |
this.nameLabel = nameLabel; | |
this.quantityField = quantityField; | |
this.removeButton = removeButton; | |
//...any additional elements to init/assign | |
//WebElements extracted via CartPage.getCartItems() and instantiated into this data structure | |
} | |
} | |
/* with above (class) object data structure implementation, | |
* you could model the test interaction between cart page | |
* and cart items as follows | |
*/ | |
CartItem[] cartItems = cartPage.getCartItems(); | |
System.out.println(cartItems[0].nameLabel.getText()); | |
cartItems[0].quantityField.sendKeys("5"); //set quantity to 5 for the cart item | |
//may need to make some other call(s) to trigger the update to take effect or not | |
//may also need to first clear the quantityField before sendKeys of course | |
cartItems[0].removeButton.click(); //remove the cart item | |
/** | |
* Cart item implementation a la page object model (POM) | |
* These cart items reside in and can be extracted from | |
* shopping cart page object to then be worked with. | |
**/ | |
public class CartItem{ | |
private WebElement nameLabel; | |
private WebElement quantityField; | |
private WebElement removeButton; | |
//... | |
CartItem(WebElement nameLabel, WebElement quantityField, WebElement removeButton, ...){ | |
//or make as varargs constructor? | |
this.nameLabel = nameLabel; | |
this.quantityField = quantityField; | |
this.removeButton = removeButton; | |
//...any additional elements to init/assign | |
//WebElements extracted via CartPage.getCartItems() and instantiated into this data structure | |
} | |
public String getName(){ | |
return nameLabel.getText(); | |
} | |
public void updateQuantity(int quantity){ | |
quantityField.clear(); | |
quantityField.sendKeys(Integer.toString(quantity)); | |
//plus any code to trigger update if page doesn't auto update on field value change | |
} | |
public void remove(){ | |
removeButton.click(); | |
} | |
//...any additional cart item high level methods that define what you can do with cart item | |
//similar to page object methods | |
} | |
/* with above cart item class implementation a la POM, | |
* you could model the test interaction between cart page | |
* and cart items as follows... | |
*/ | |
CartItem[] cartItems = cartPage.getCartItems(); | |
System.out.println(cartItems[0].getName()); | |
cartItems[0].updateQuantity(5); | |
cartItems[0].remove(); | |
/* NOTE: that in these examples, I use a simple array of cart items. | |
* But it could have been an ArrayList or other similar slightly more | |
* complex data type. I jsut went simple route to present example. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment