DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Latest Articles - DZone

article thumbnail
Selenium: Design Patterns
Selenium WebDriver is widely used as a first-choice framework when it comes to testing web applications. In this article I would like to introduce you to two design patterns, that work great with selenium tests. In this post we will dig into Page Object pattern and combine it with Page Factory. As always, whole code for project is available on my github. Page Object Pattern Page Object is a concept that helps to reduce the number of duplicated code, and helps with test maintenance, resulting from changes in the structure and functionality of the web application. The main idea behind page object is to place the code relating to the functionality of subpages in separate classes. In a very simplified way, if your web application includes pages: home about contact We should create three separate page object classes: Home.java About.java Contact.java Each class should contain only those methods that support functionality for the corresponding subpage, and define selectors only for this subpage. We should also remember, that public methods in page object class are only those, that represents user’s workflow. One important point is that each public method in page object class should return object of page that user is on. For example, if a button on the page does not gets you to different subpage, this method should return this. Moreover, if a button is a link to another page, the method should return page object class of this subpage. Here is a code snippet of this approach using an example from previous article, where we wrote tests for Facebook login: public class LoginPage { private static By userEmailLoginInput = By.id("email"); private static By userPasswordLoginInput = By.id("pass"); private static By loginSubmitBtn = By.id("u_0_n"); private WebDriver driver; public LoginPage(WebDriver driver) { this.driver = driver; driver.get("http://facebook.com"); } public LoginPage enterUserLogin(String login) { WebElement emailBox = driver.findElement(userEmailLoginInput); emailBox.click(); emailBox.sendKeys(login); return this; } public LoginPage enterUserPassword(String password) { WebElement passwordBox = driver.findElement(userPasswordLoginInput); passwordBox.click(); passwordBox.sendKeys(password); return this; } public HomePage submitLoginCredentials() { WebElement submitBtn = driver.findElement(loginSubmitBtn); submitBtn.click(); return new HomePage(driver); } } In above example, methods enterUserLogin() and enterUserPassword() don’t transfer user to another subpage, but perform activities on the login page, so return type is object of LoginPage class (this). On the other hand, submitLoginCredentials() method moves user to home page (or to the page informing about the login failure), so it returns home page class object. In real life example, in HomePage.class we would have had methods that perform actions on the home page, but since it’s only an example code demonstrating pattern usage, we have only checkIfLoginSucceed() method here: public class HomePage { private WebDriver driver; public HomePage(WebDriver driver) { this.driver = driver; } public boolean checkIfLoginSucceed() { return driver.getPageSource().contains("fbxWelcomeBoxName"); } } …and test looks as follows: @Test public void shouldNotLoginWithIncorrectCreds() { LoginPage loginPage = new LoginPage(driver); loginPage.enterUserLogin("[email protected]"); loginPage.enterUserPassword("wrongPassword"); HomePage homePage = loginPage.submitLoginCredentials(); assert (!homePage.checkIfLoginSucceed()); } Page Factory pattern The main idea behind this pattern is to support page object classes, and to allow better page selectors management. Page Factory provide us with a set of annotations, which work great with selectors and enhance code readability. To understand this, let’s look at a standard initialization webelement through the selector: private static By email = By.id(“email”); WebElement emailBox = driver.findElement(email); Unfortunately, the readability leaves much to be desired. The problem is also that variable with selector and web element object require a separate initialization. Along with Page Factory, this is greatly simplified: private WebElement email; That’s all! Page Factory search page source code for element with id=”email” by default and assigns it to declared webelement. Of course, this minimalism can introduce some confusion, and therefore I recommend the use of @FindBy annotation: @FindBy(id = “email”) private WebElement userEmailLoginInput; We can also search selectors by other attributes, such as xpath, name, className, etc. Everything we have to do to use this pattern, is to initialize PageFactory in the page object class constructor: PageFactory.initElements(driver, this); Our LoginPage.class with page factory pattern will look like this: public class LoginPage { @FindBy(id = "email") private WebElement userEmailLoginInput; @FindBy(id = "pass") private WebElement userPasswordLoginInput; @FindBy(id = "u_0_n") private WebElement loginSubmitBtn; private WebDriver driver; public LoginPage(WebDriver driver) { this.driver = driver; driver.get("http://facebook.com"); PageFactory.initElements(driver, this); } public LoginPage enterUserLogin(String login) { userEmailLoginInput.click(); userEmailLoginInput.sendKeys(login); return this; } public LoginPage enterUserPassword(String password) { userPasswordLoginInput.click(); userPasswordLoginInput.sendKeys(password); return this; } public HomePage submitLoginCredentials() { loginSubmitBtn.click(); return new HomePage(driver); } } Important thing to notice is that every time we call a method on web element, page factory search for our element all over again through page sources. If page isn’t AJAX-based, we can use page factory cache to search an element only with page factory initialization, and then retrieve it from cache: @FindBy(id = “email”) @CacheLookup private WebElement userEmailLoginInput; Summary As in every aspect of software development, design patterns in test automations help us to develop tests faster and more efficient. Page Object and Page Factory are two simple patterns, that significantly improve maintenance and readability of selenium tests. For complete sources of examples in this article please visit my github. If you have any questions, feel free to leave a comment!
September 5, 2015
by Łukasz Rosłonek
· 10,312 Views · 5 Likes
article thumbnail
Going to Mars: Building a DIY Eclipse IDE for ARM Embedded Microcontrollers
Using the latest Eclipse IDE to work with ARM Cortex M based devices
September 5, 2015
by Erich Styger
· 4,418 Views · 1 Like
article thumbnail
Using JAX-RS With Spring Boot Instead of MVC
It’s easy to integrate JAX-RS into Spring applications, but why would you do this? Spring MVC should be enough, right?
September 4, 2015
by Lieven Doclo
· 103,857 Views · 13 Likes
article thumbnail
My 18 Favorite Quotes on Agile, DevOps, and Continuous Delivery
A list of fun and, oftentimes, true quotes about DevOps and software development.
September 4, 2015
by Yaniv Yehuda
· 25,690 Views · 4 Likes
article thumbnail
Creating a ListView Parallax Effect With a Sticky Header in Android
This Android tutorial will help you create a parallax animation that sticks to the top of a list when scrolled.
September 4, 2015
by Nilanchala Panigrahy
· 50,137 Views · 1 Like
article thumbnail
Get Last Record in Each MySQL Group
In this tutorial we will look at how you can use MySQL at getting the last record in a Group By of records.
September 4, 2015
by Paul Underwood
· 106,764 Views · 5 Likes
article thumbnail
Retrieving Table Metadata from SQL Server Catalog Views
SQL Server Catalog Views can be queried easily, and being able to retrieve this metadata can be very useful in various situations.
September 3, 2015
by Daniel D'agostino
· 32,495 Views · 2 Likes
article thumbnail
Using GNU Profiling (gprof) With ARM Cortex-M
Guide on how to use GNU gprof for profiling embedded applications on ARM Cortex-M microcontrollers, including setup, implementation, and analysis of profiling data.
September 3, 2015
by Erich Styger
· 9,791 Views · 2 Likes
article thumbnail
Verification and Validation in Automated Testing
A definition of verification and validation in regards to automated testing, and a guide to using them in your workflow.
September 2, 2015
by Denis Goodwin
· 14,295 Views · 6 Likes
article thumbnail
GNU Static Stack Usage Analysis
Stack overflows can be really detrimental. Learn how to analyze those GNU static stacks and properly analyze them in this great tutorial.
September 2, 2015
by Erich Styger
· 12,047 Views · 1 Like
article thumbnail
MVVM Databinding Cheat Sheet
This cheat sheet summarizes the basic usage and the purpose of each ZK MVVM binding command.
September 1, 2015
by Tendy Su
· 7,043 Views · 5 Likes
article thumbnail
Diagramming Microservices With the C4 Model
Learn how to diagram a microservices architecture with the C4 model with these tips.
September 1, 2015
by Simon Brown
· 16,141 Views · 4 Likes
article thumbnail
15 Ways to Write Beautiful Code
A list of what to keep in mind when coding to make sure it stays clean and functional.
September 1, 2015
by Dave Bush
· 18,219 Views · 3 Likes
article thumbnail
Spocklight: Optimize Run Order Test Methods
Spock is smart, and we can tell Spock to re-run failing methods before successful methods, with the fastest ones first.
August 31, 2015
by Hubert Klein Ikkink
· 3,433 Views · 2 Likes
article thumbnail
The Art of Visualising Software Architecture
Simon Brown talks about the basis for his new book on software architectures and the difficulty in visualizing architectural models.
August 31, 2015
by Simon Brown
· 4,777 Views · 3 Likes
article thumbnail
Batch Programming with GDB: Segger J-Link and P&E Multilink
Use the command line to do some efficient batch programming for your firmware boards
August 31, 2015
by Erich Styger
· 4,786 Views · 1 Like
article thumbnail
Is it Worth it to Use 'Optional' in Collections?
There's a practical reason to use Optional in collections. It's just other developers who ruin it for you.
August 28, 2015
by Peter Verhas DZone Core CORE
· 19,329 Views · 2 Likes
article thumbnail
SolrCloud Rebalance API
An innovative approach that helps with an effective index and a dynamic config management system for massive multi-tenant search infrastructure in SolrCloud.
August 28, 2015
by Radu Gheorghe
· 5,020 Views · 2 Likes
article thumbnail
Configuring Spring Boot for MySQL
Spring Boot has support for MySQL and a number of other popular relational databases.
August 28, 2015
by John Thompson
· 170,095 Views · 8 Likes
article thumbnail
Redirect Logging Output to Standard Error with Logback
Learn how to redirect a logging output to a standard error in Logback.
August 27, 2015
by Hubert Klein Ikkink
· 5,961 Views · 3 Likes
  • Previous
  • ...
  • 1466
  • 1467
  • 1468
  • 1469
  • 1470
  • 1471
  • 1472
  • 1473
  • 1474
  • 1475
  • ...
  • Next
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook
×