Selenium Log4j & Selenium Grid
Selenium Log4j & Selenium Grid
Simona Pitam
Introduction
Printing messages to the console is an integral part of the development testing and the debugging of a Java
program. If developers are working on a Server side application, where they cannot see what’s going on inside the
server, then their only visibility tool is a log file.
Without logs, developers cannot do any debugging or see what’s going on inside the application. Though, Java has
pretty handy System.out.println() methods to print something on console, which can also be routed to log file but not
sufficient for a real-world Java application.
If developers are running a Java program in Linux or Unix based systems, Log4j or SLF4j or any other logging
framework offers a lot more features, flexibility, and improvement on message quality, which is not possible
using the System.out.println() statements.
Simona Pitam
What is Log4j
Log4j2 is the updated version of the popular and influential Log4j library, which is simple, flexible, and fast Java-
based logging framework. It is thread-safe and supports internationalization. We mainly have 3 components to work
with Log4j:
debug(Object message) It is used to print the message with the level public void debug(Object message)
org.apache.logging.log4j.Level.DEBUG
. It is the lowest restricted logging level.
info(Object message) It is used to print the message with the level public void info(Object message)
org.apache.logging.log4j.Level.INFO. It
is more restricted than the DEBUG logging
level and developers should log messages
which are for an informative purpose.
Simona Pitam
warn(Object message) It is used to print the message with the level public void warn(Object message)
org.apache.logging.log4j.Level.WARN.
It is more restricted than the INFO logging
level and is used to log the warning sort of
messages i.e. Connection lost between
Client and Server, Database Connection
lost etc.
Log4j Logger Class
fatal(Object message) It is used to print the message with the level public void fatal(Object message)
org.apache.logging.log4j.Level.FATAL.
trace(Object message) It is used to print the message with the level public void trace(Object message
org.apache.logging.log4j.Level.TRACE.
Simona Pitam
Priority Log Level
Trace < Debug < Info < Warn < Error < Fatal
Where :
❏ org.apache.logging.log4j.Level.FATAL
Simona Pitam
Log4j2 Appender Interface
Appender is an interface which is primarily responsible for printing the logging messages to the different
destinations such as console, files, sockets, database etc. In Log4j2 we have different types of Appender
implementation classes:
Simona Pitam
Log4j2 Appender Interface
Simona Pitam
Log4j Layout Class
Layout component specifies the format in which the log statements are written into the destination repository by the
Appender.
Simona Pitam
Log4j Layout Class
Simona Pitam
Why prefer Log4j2 over System.out.println?
Below are some of the reasons, which are enough to understand the limitation of using System.out.println():
Any logging framework allows developers to log debugging information to a log level which can be used as filtering
criteria, i.e. one can disable the message belongs to a particular log level. For e.g., Developers would be more
concerned to see the WARN messages than DEBUG messages in the production environment
Logging framework can produce better outputs and metadata which helps to troubleshoot and debug. For
e.g., Log4j2 allows to print formatted output by specifying a formatting pattern i.e. by using PatternLayout one can
include a timestamp, class name etc
Simona Pitam
Log4j2 Best Practices
● Carefully choose which kind of message should go to each level of logging in Java. It becomes extremely
important if developers are writing server application in Java and the only way to see what is happening is
the Log4j2 logs. If developers log too much information, the application performance will be affected. At
the same time if developers don’t log important information like the incoming messages or the outgoing
messages in Java logs, then it would become extremely difficult to identify the root cause of the issue
● Using either Log4j2 or java.util.logging for setting up the logging framework in Java. As a developer, I would
recommend using the Log4j2 because it is very flexible. It allows changing the logging level in Java without
restarting the application. To do this, developers can have Log4j2 Watchdog which continuously looks for
log4j2.xml in a particular directory. If found, it loads it and reset the logging framework in Java.
Simona Pitam
Log4j2 Best Practices
● By using the log4j2.xml, developers can have different Logger configuration for the different Java classes.
Developers can have some classes in INFO mode, some in WARN mode or ERRORmode
● Another important point to remember is the format of Java logging. Log4j2 logger allows the developer to
include the Thread Name and the fully qualified Java Class Name while printing logs. It would be
impossible to find sequence of events if the application code is executed by multiple threads without
Simona Pitam
LOG4J XML FILE
https://www.apache.org/dyn/closer.lua/logging/log4j/2.11.0/apache-log4j-2.11.0-bin.tar.gz
Configure Path
Selenium Grid is a tool that distributes the tests across multiple physical or virtual machines so that we
can execute scripts in parallel (simultaneously). It dramatically accelerates the testing process across
browsers and across platforms by giving us quick and accurate feedback.
Selenium Grid allows us to execute multiple instances of WebDriver or Selenium Remote Control tests in
parallel which uses the same code base, hence the code need NOT be present on the system they
execute. The selenium-server-standalone package includes Hub, WebDriver, and Selenium RC to execute
the scripts in grid.
Simona Pitam
Selenium Grid
● Hub − The hub can also be understood as a server which acts as the central point where the tests
would be triggered. A Selenium Grid has only one Hub and it is launched on a single machine once.
● Node − Nodes are the Selenium instances that are attached to the Hub which execute the tests. There
can be one or more nodes in a grid which can be of any OS and can contain any of the Selenium
supported browsers.
Simona Pitam
Architecture of Selenium Grid
Simona Pitam
Working With Grid
● Test Execution
● Result Analysis
Simona Pitam
Configuring the HUB
Simona Pitam
Configuring the Hub
Simona Pitam
Command for running the HUB command on Mac/Linux
The -role flag is used to set
that particular host machine
as the Hub. When you hit
#sudo java -jar selenium-server-standalone-3.8.1.jar -role hub enter, you will get the below
output on your screen.
! We are running the following command from the folder where there is the following file:
#selenium-server-standalone-3.8.1.jar
up and running
Command for running the HUB command on Windows
The -role flag is used to set
that particular host machine
as the Hub. When you hit
#sudo java -jar selenium-server-standalone-3.8.1.jar -role hub enter, you will get the below
output on your screen.
Simona Pitam
Configure the Nodes
Step 1 − Logon to the node (where you would like to execute the scripts) and place the 'selenium-
server-standalone-2.42.2' in a folder. We need to point to the selenium-server-standalone JAR while
launching the nodes & run the following command.
WHERE:
http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub
After executing the command, come back to the Hub. Navigate to the URL -
http://10.30.217.157:4444 and the Hub would now display the node attached to it.
Simona Pitam
Prompt is Displayed
Simona Pitam
Launching Browser
Step 4 − Now let us launch the Internet Explorer Node. For launching the IE Node, we need to have the
Internet Explorer driver downloaded on the node machine.
Simona Pitam
Selenium Grid
Simona Pitam
Launch IE using the following command
Where,
D:\IEDriverServer.exe = The location of the downloaded the IE Driver(on the Node Machine)
http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub Simona Pitam
Step 7 − After executing the command, come back to the Hub. Navigate to the URL -
http://10.30.217.157:4444 and the Hub would now display the IE node attached to it.
Let us now launch Chrome Node. For launching the Chrome Node, we need to Simona Pitam
have the Chrome driver downloaded on the node machine.
Simona Pitam
Continue….
Download the driver based on the type of your OS. We will execute it on Windows environment,
hence we will download the Windows Chrome Driver. After you have downloaded, unzip the exe file
and place it in a folder which has to be referred while launching chrome nodes.
Simona Pitam
Continue ...
Where,
D:\chromedriver.exe = The location of the downloaded the chrome Driver(on the Node Machine)
The execution of this script relies on two important library packages. They areDesiredCapabilities object and
RemoteWebDriver object.
DesiredCapabilities is used to set the browser type and the OS of our Node. To import DesiredCapabilities
import org.openqa.selenium.remote.DesiredCapabilities;
Simona Pitam
Building A Selenium Grid
RemoteWebDriver is used to select the Node on which we want to execute the test. To import
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.RemoteWebDriver;
I have imported these two packages along with TestNG annotations package for my script as shown in the
below code snippet. I have imported TestNG annotations because I have executed this as a TestNG test for
which a detailed report will be generated. I would recommend you to import the same set of packages for your
Simona Pitam
code.
Building Selenium Grid
When you maneuver over the logos of any browser present in the Selenium Hub console, you will get the
details like the browserName and the platform of the Node you want to automate. The image also indicates
how many instances of the browsers I can open in my Node. I can configure a maximum of 5 chrome, 5
Firefox and 1 Internet Explorer instances. These details are present in the below screenshot.
Simona Pitam
Code Explanation
I have divided the entire test script into three test annotations. @BeforeTest, @Test and @AfterTest. In
@BeforeTest, I have used the DesiredCapablities and RemoteWebDriver objects to configure my Node. In
@Test, I have asked the Node to navigate to Edureka’s home page and assert the title of the page. In
When your test passes execution, you will get the below output.
Simona Pitam
Results of Running TestNg Tests
When your test passes execution, you will get the below output.
===============================================
Default test
Tests run: 1, Failures: 0, Skips: 0
===============================================
Simona Pitam
===============================================
Default suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Results of TestNG Running
Since this is executed as a TestNG test, you will get a detailed test report similar to