It’s not enough to just write some code: you need to make sure that you do it right. Below you will find a few of the recommended ways of using Codeception, from where you can actually start growing and learning even more! Here’s where you should start.
Let’s talk about how to mention page elements in your code. Adding them right off the batch, by their CSS path is, of course, the most direct way of doing it. That’s simple, but not quite correct. You need to use something called PageObjects.
What is a PageObject?
A PageObject is a class in which you can save your element locators, for each page individually, so that you can later call them easier in your tests.
How do you create PageObject?
That’s simple. Just run the following command in your main folder:
` php codecept.phar generate:pageobject home`
It will generate a new file under tests/_pages. That will be a class file, called ‘homePage.php’. This is a PHP file in which you can write down your selectors, like this:
You will find the URL of the current page in each PageObject. Add the URL of the page you’re testing as its value. (‘/’ takes you to the homepage. You only need to specify the path in URLs).
Then, when you want to use an element from this page in a test, just type the PageObject name and the name of the locator, like this:
You should use PageObjects instead of specifying the CSS paths. This gives more flexibility to your code, more reusability, easier maintenance and increased readability. It is one of the best practices when using Codeception, along with using StepObjects.
What is a StepObject?
StepObjects allow you to store multiple lines of code. The difference between Step and Page objects is that StepObjects define functions for you, and PageObjects just define the elements of a page. They will contribute to making your code even more readable and easier to maintain.
How can you create and use a StepObject?
Just run this command:
` php codecept.phar generate:stepobject acceptance test`
Once you do, you will be prompted to name the actions in this StepObject. You can name them or just skip this step. At this point, let’s name one of the actions as ‘openLogin’.
The command has generated a class in acceptance/_steps/testSteps.php. It looks like this:
Under $I = $this;, you can start typing in the actions.
When you want to use these actions in a test, you will just need to instantiate the TestSteps class instead of AcceptanceTester, and then use the action like you would use a regular method:
Writing locators for your elements.
If you are interested in the best ways of writing locators, you should visit this article.
You should also remember to visit the WebDriver module documentation from the Codeception team, which offers a good overview on how you can “name” your locators.
How can you test if the path that you picked for your element is correct? Just open up a console in a browser (usually with f12), and in the console tab, type in $(‘path to your element’); and press enter. If your element is identified, it will show up underneath what you wrote. If not, you will get an error a simple notification.
There is a possibility that you will get several similar elements on a single page, so using a simple ‘input[type=submit]’ identifier might not find the button you’re looking for. Try to choose the most unique property of that element.
If you encounter an element with an id or class that has a random dynamic string in it, like < div id=”_2438938795_user”>, you can find it as ’div[$id=”_user”]’.
Also, take note of the fact that your WebDriver might need a restart from time to time. Just paste this URL in any browser, press enter and it will stop (http://127.0.0.1:4444/selenium-server/driver/?cmd=shutDownSeleniumServer).
Thank you for reading up on the series! Feel free to ask me any question. Stay tuned to the Innobyte blog for more interesting articles!
Thank you for reading up on the series! Feel free to ask me any question, and I promise to come back to you everytime I find something interesting on automated testing. Until then, stay tuned to the Innobyte blog for more interesting articles!
Pingback: Blue Coaster33
Hi Alexandra,
Do you know how to work with PageObjects in codeception with a basepage where other PageObjects inherit common fields and buttons from? I find it very hard to find examples for this. Thank you,
Loes