Selenium is a powerful open-source framework for automating web browsers for testing web applications.
This repo contains test cases for testing HTML pages, elements and navigations using Selenium with Java binding and TestNG.
This example is using a local HTML document with various elements and styled with CSS and JavaScript.
- Install Java Development Kit.
- Install an Integrated Development Environment (IDE): this example was created in IntelliJ IDEA.
- Install a testing framework such as TestNG which is used in this project.
- Create a new Java project in your IDE and select the build system: this project is using Maven.
- Add Selenium and TestNG dependencies into
pom.xmlfile. - Start creating your test classes and Page Object Model (POM) classes.
TestNG is a Java testing framework that makes it easier to write, organize, and run automated tests. TestNG annotations are special markers in Java code that control how your tests and suites are executed. Below are some of the examples used in this project:
@Testmarks a method as a test@BeforeTestand@AfterTestmark methods that will be executed before and after running all tests within a class@BeforeMethodand@AfterMethodwill be executed before and after each test
UI automation testing rely on well-defined elements in applications. Selenium offers various locators for this purpose,
including By.id(), By.name(), By.xpath(), By.cssSelector(), etc.
You can use "Inspect" or "View Page Source" option in a browser to determine the ids or names of all elements.
The Page Object Model (POM) is a design pattern for creating maintainable, reusable and scalable test automation code. The best practices for POM:
- Represent each page or a window as a separate class.
- Represent common menus, toolbars and alerts as separate classes as well.
- Define each element once.
- Define user interactions methods - keep them small for better reusability.
- Do not put any business logic into methods: methods should report if an element is visible and test case should determine if this is expected.
- Use meaningful names for elements, methods and test cases.
- Organize your code into an intuitive folder structure and separate POM, data and test suites.
- Add logs into every step.
- Prioritize readability and clarity over slick one-liners.
You can run your tests directly from the IDE or in a command line using mvn test.
For bigger project, you can create testng.xml in the root directory and define sets of suites. The execution can also be from the IDE or a command line as mentioned above.
This is an example of how execution results look in IDE:
