This post is second in a series of six. Need to go back to the beginning?
Rewriting the automated test code we use at work to test against Dave Haeffner's mock site, The-Internet: Login Page.
Please note: I have been an automated tester only since March 2015. This is only my best guess on how to translate what we use at work. I am writing this blog to deepen my own understanding of automated testing. If there are any glaring errors about Java, OO, WebDriver, or anything else please let me know in the Comments section!
The-Internet: Common Utilities: methods, exceptions and logging
Part Two: Drafting the Common Utilities Library
The Selenium WebDriver API provides the basic methods to manipulate a brower to perform actions, such as navigating to a web page, but it doesn't have all the functionality we would need. What if we wanted to add in exception handling, such as if the web page was not found? What if we wanted to throw a customized error to a log file when the page was not found? What if, even upon a success, we wanted to write a message to the log?
A Common Utilities library, such as one they are using at the moment at my workplace, can be developed to handle these cases. Each Selenium API method is wrapped in try / catch / throw blocks for exception handling, built-in with logging functionality. When things go wrong, clear and concise error messages will save a lot of time debugging if the issue was with the server, with the code of the site, or the tests themselves.
If you want to skip ahead to see an example of a CommonUtils library, you can view one at: test/java/utils/CommonUtils.java, which closely resembles the one drafted by the automation test team at work.
For this project, we will test against The-Internet.
About The-Internet
Dave Haeffner wrote a test site called The-Internet ( https://the-internet.herokuapp.com/ ) that entry level automated testers could use to test against. For this blog post, I will be examining his Login Page example. If you want more information about Dave, you can view my blog entry about him.
Automated Test Suite code to test The-Internet
To deepen my understanding of automation test suites, over the next month or so I will be examining and rewriting code to test against The-Internet. Feel free to follow along, make suggestions, and ask questions about the code I am writing in the Comments section of this blog! I have only been writing code on-the-job since March, so there are bound to be errors of interpretation.
All code I develop will be uploaded to my GitHub account at https://github.com/tjmaher.
- WebDriver_TheInternet_Basics: Quick-and-dirty code to test the basics of The-Internet login page with SimpleManipulationWebElements.java
- WebDriver_TheInternet_Advanced: The beginnings of the test suite for The-Internet.
Creating Methods to Navigate to a Web Page
Navigating to The-Internet's Login page is pretty simple with WebDriver. All it takes is one driver.get statement in order to navigate to the login page:driver.get("http://the-internet.herokuapp.com/login");
But what if you wanted to add functionality to write to the log console every time you use WebDriver to navigate to a web page? This means you would have to enter two lines of code per driver.get statement.
System.out.println("Navigating to: http://the-internet.herokuapp.com/login"); driver.get("http://the-internet.herokuapp.com/login");
Let's say you also wanted to add even more:
- Logging functionality when an action is performed
- Catch any errors if the action failed
- Throw an error to the logs detailing if an error ever happened
- Document the Thread ID, useful when dealing with multithreaded systems that are running many Selenium WebDriver tests in parallel.
- Include any wait statements, if a page is known to be slow loading.
To satisfy all these new requirements, at work they came up with the following method for their CommonUtils library, navigateToURL:
public void navigateToURL(String URL) { System.out.println("Navigating to: " + URL); System.out.println("Thread id = " + Thread.currentThread().getId()); try { _driver.navigate().to(URL); } catch (Exception e) { System.out.println("URL did not load: " + URL); throw new TestException("URL did not load"); } }
To use this method, we can use this method in a test, passing in the URL as a String:
navigateToURL("http://the-internet.herokuapp.com/login");
Now imagine if you had a whole library where all the Selenium WebDriver functions are wrapped in try / catch / throw blocks, logging functionality is added, threads have identification numbers listed, and any optional wait statements are included.
Methods in the CommonUtils Library
These are the methods in the CommonUtility we can use:
- navigateToURL(String URL)
- getPageTitle()
- getElement(By selector)
- sendKeys(By selector, String value)
- clearField(WebElement element)
- click(By selector)
- waitForElementToBeClickable(By selector )
- waitForElementToBeVisible(By selector)
How to use the Methods:
Selectors can be found By Id or By CSS Selector. If we wanted to grab the Username textbox, the Password textbox, or the Login button, we could:
- Username: getElement(By.cssSelector("[name='username']")
- Password: getElement(By.cssSelector("[name='password']")
- Login Button: getElement(By.cssSelector(""[type='submit'][class='radius']"")
... And wherever these CommonUtils methods are used, we need to make sure to:
import utils.CommonUtils;NEXT: Storing Constanst: public final vs enums
Testing The-Internet:
- Part One: Sketching out the simple manipulation of a login page
- Part Two: Drafting Common Utilities
- Part Three: Storing Constanst: public final vs enums
- Part Four: Storing locators in enums
- Part Five: Page Object Model
- Part Six: Writing the Automated Test
- Source Code: GitHub, T.J. Maher
-T.J. Maher
Sr. QA Engineer, Fitbit
Boston, MA
// Automated tester for [ 3 ] month and counting!
Please note: 'Adventures in Automation' is a personal blog about automated testing. It is not an official blog of Fitbit.com.
2 comments:
"Adventures in Automation" likely refers to a creative work, blog, or project exploring the realm of automation, robotics, or artificial intelligence. This term may encompass narratives, experiences, or insights into the development, implementation, and impact of automated systems in various industries and contexts. "Adventures in Automation" may offer a platform for sharing knowledge, discussing challenges, and envisioning the future of automation technology.
abogado de planificación patrimonial
abogado de accidentes de camiones
abogado de flsa
A Common Utilities library, such as one they are using at the moment at my workplace, can be developed to handle these cases. Each Selenium API method is wrapped in try / catch / throw blocks for exception handling, built-in with logging functionality. When things go wrong, clear and concise error messages will save a lot of time debugging if the issue was with the server, with the code of the site, or the tests themselves
Printable wine label templates
Merry Christmas SVG
Cricut New Year sayings
Cutting machine Thanksgiving quotes
Post a Comment