Selenium Project Setup :
Prerequisite for Converting a Selenium Project in to Maven Project:
- Elipse IDE
- Java SE 8
- TestNg Framework
- Windows 10 64bits/Mac
- Chrome, Firefox, IE driver.
Set Up Eclipse and Selenium :
Install Java JDK SE 8 for Windows x64 :
Go to the Java SE Development Kit page and download the Windows x64 version. After finishing downloading, install Java JDK and configure the Java system variables.
Install Eclipse IDE for Java Developers :
Go to the Eclipse IDE Downloads page and download the 64-bit version for Java Developers. Unzip the Eclipse package in a folder of your choice. We encourage you to use "c:\eclipse".
Download Chrome Driver/Firefox Driver/IE Driver :
In order to automate test cases using Google Chrome/Firefox web browser, you should download Chrome/Firefox Driver. Go to the Chrome Driver download/Firefox Driver downloads page and select the latest version. After downloading the package, unzip it once again in a folder of your choice.Put it on "broswersetup" folder in the project.
Note : On the basis of your machine download browser driver, if windows machine(.exe) file and also for Mac download.
Maven Setup:
Step 1: To setup Maven, download the maven’s latest version form Apache depending upon different OS.
Step 2: Unzip the folder and save it on the local disk.
Step 3: Create environment variable for MAVEN_HOME. Follow the below step:
Navigate to System Properties ->Advanced System Settings>Environment Variable ->System Variable ->New ->Add path of Maven folder
Step 4: Edit path variable and provide the bin folder path.
Step 5: Now verify the maven installation using command prompt and don’t forget to setup JAVA_HOME.
Use mvn –version to verify maven version and output comes like below.
Install maven IDE in Eclipse:
Maven provides IDE to integrate with eclipse. I am using eclipse Juno here.
Navigate to Help->Eclipse Marketplace-> Search maven ->Maven Integration for Eclipse ->INSTALL
After installation, you have to restart eclipse.
Then right-click on pom.xml and verify all the options are available like below.
Import Maven project:
- Download project source code and unzip it into a folder location. Lets assume that we have the Maven project on D:\abc\Workspace.
- From Eclipse, Project Explorer -> Right click -> Select Import Menu and Import.
- Expand Maven menu And click Existing Maven Projects.
- Click next and browse the location where you have the Maven project source code. (browse the project location in your machine)
- Click next, Eclipse will recognize the Maven project and it will show you a list of all possible Maven projects located there.
- Just select the project and click next.
- Eclipse will show you a Maven Build message. Just click finish.
- The Maven project is successfully imported into Eclipse IDE.
Build the Project:
The project can be built by both using IDE and command prompt.
Using IDE you have to right click on POM-Run As-Maven Build(Give name as test in Goals).
- Enter goals like clean install etc. and click Run.
- Same can be done using a command prompt. Navigate to project folder where pom.xml lies.
- And use below commands to clean, compile and install
- For clean: mvn clean
- For compile: mvn compile
- For Install: mvn install
- Below is the info which is displayed when you clean any project and shows “BUILD SUCCESS”.
Project Description :
CommonUtilies Package :
1. BasePage.java In this class we created General methods which can be used any of the projects and for reading elements we are using Fluent Wait.
Fluent Wait :
When using the FluentWait instance, we can specify:
- The frequency with which FluentWait has to check the conditions defined.
- Ignore specific types of exception waiting such as NoSuchElementExceptions while searching for an element on the page.
- The maximum amount of time to wait for a condition.
2. Data_Provider.java :
Data Provider methods which can receive two types of parameters methods. This is useful if you want to use the same Data Provider but want different data sets returned, based on the test method being invoked or the test suite/test type it belongs to.You can specify just the method parameter or both the parameters. Based on the parameters used, TestNg will set the parameter values before invoking the Data Provider method.
3. ExcelReaderExpected.java :
In this class we Reading the Excel file to input the data in text field or matching the Results.
4. GenericsMethods.java :
In this class we are reading the Properties.file(key value pairs format) and implemented Robot class to upload file.
5. JSONReader.java :
In this class we are reading the .json file for UI web pages elements and page url.
6. Listner.java :
In this class we integrate Listener class, Listeners are required to generate logs or customize TestNG reports in Selenium Webdriver.
- There are many types of listeners and can be used as per requirements.Listeners are interfaces used in selenium web driver script
- Demonstrated the use of Listener in Selenium
- Implemented the Listeners for multiple classes
7. Log4jReport.java :
What is log4j ?
Log4j is an open source logging framework. With log4j – logging behavior can be controlled by editing a configuration file only without touching the application binary and can be used to store the Selenium Automation flow logs. It equips the user with detailed context for application failures.
log4j has 3 major components:
Loggers – It is used for logging information. To use loggers we need to take care of things mentioned below:
- Create object of logger class: Logger class is a Java-based utility which has all the generic methods in it to use log4j (To use Logger class we need to Import org.apache.log4j.Logger)
- Define the log level: We can define the log levels in multiple forms and levels available are:
- All – This level of logging will log everything, it is intended to turn on all logging.
- DEBUG – It saves the debugging information and is most helpful to debug an application.
- INFO – It prints informational message that highlights the progress of the application.
- WARN – It designates potentially harmful situations.
- ERROR – It designates error events that might still allow the application to continue running.
- FATAL – It designates very severe error events that will presumably lead the application to crash
- OFF – It is intended to turn off logging.
Appenders – In log4j, an output destination is called an appender. It allows the destination where the logs would get saved. It supports the following types of appenders:-
- ConsoleAppender – It logs to some standard output.
- File appender – It prints logs to some file at a particular destination.
- Rolling file appender – It is used to for a log file with maximum size.
Layouts – It is used to format the logging information in different style.
- All – This level of logging will log everything, it is intended to turn on all logging.
- DEBUG – It saves the debugging information and is most helpful to debug an application.
- INFO – It prints informational message that highlights the progress of the application.
- WARN – It designates potentially harmful situations.
- ERROR – It designates error events that might still allow the application to continue running.
- FATAL – It designates very severe error events that will presumably lead the application to crash
- OFF – It is intended to turn off logging.
8. ProjectSetup.java :
In this class we setup the project ,on which browser we need to run the code and url set up.
Note For windows machine we need to give this path for browser driver:
- Chromedriver : System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+ "/broswersetup/chromedriver");
- Frefoxdriver:System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/broswersetup/geckodriver.exe");
9. ProfileSetUp.properties:
In this properties file ,we set up the url and in which browser we want to run the code.
10. HomePage.java :
In this class , we are creating methods for the UI elements present in the home page and reading the elements from the .json file.
Notice the following.
- TestNG does not require you to have a main() method.
- Methods need not be static.
- We used the @Test annotation. @Test is used to tell that the method under it is a test case. In this case, we have set the verifyHomepageTitle() method to be our test case, so we placed an '@Test' annotation above it.
- Since we use annotations in TestNG, we needed to import the package org.testng.annotations.*.
- We used the Assert class. The Assert class is used to conduct verification operations in TestNG. To use it, we used the SoftAssertions for customise reporting.
You may have multiple test cases (therefore, multiple @Test annotations) in a single TestNG file. This will be tackled in more detail later in the section "Annotations used in TestNG."
Notice the following.
- TestNG does not require you to have a main() method.
- Methods need not be static.
- We used the @Test annotation. @Test is used to tell that the method under it is a test case. In this case, we have set the verifyHomepageTitle() method to be our test case, so we placed an '@Test' annotation above it.
- Since we use annotations in TestNG, we needed to import the package org.testng.annotations.*.
- We used the Assert class. The Assert class is used to conduct verification operations in TestNG. To use it, we used the SoftAssertions for customise reporting.
You may have multiple test cases (therefore, multiple @Test annotations) in a single TestNG file. This will be tackled in more detail later in the section "Annotations used in TestNG."
Running the Test
Checking reports created by TestNG
Generating HTML Reports
TestNG has the ability to generate reports in HTML format.
Step 1: After running our FirstTestNGFile that we created in the previous section, right-click the project name (FirstTestNGProject) in the Project Explorer window then click on the "Refresh" option.
Step 2: Notice that a "test-output" folder was created. Expand it and look for an index.html file. This HTML file is a report of the results of the most recent test run.
Step 3: Double-click on that index.html file to open it within Eclipse's built-in web browser. You can refresh this page any time after you rerun your test by simply pressing F5 just like in ordinary web browsers.
For Complete Code : https://github.com/arunksahoo/AutomationFramework
For understang of Framework Designing : https://www.linkedin.com/pulse/hybrid-framework-designing-usingpom-data-driven-arun-sahoo/
or https://seleniumautomation-by-arun.blogspot.com
TestNG has the ability to generate reports in HTML format.
Step 1: After running our FirstTestNGFile that we created in the previous section, right-click the project name (FirstTestNGProject) in the Project Explorer window then click on the "Refresh" option.
Step 2: Notice that a "test-output" folder was created. Expand it and look for an index.html file. This HTML file is a report of the results of the most recent test run.
Step 3: Double-click on that index.html file to open it within Eclipse's built-in web browser. You can refresh this page any time after you rerun your test by simply pressing F5 just like in ordinary web browsers.
For understang of Framework Designing : https://www.linkedin.com/pulse/hybrid-framework-designing-usingpom-data-driven-arun-sahoo/
or https://seleniumautomation-by-arun.blogspot.com