Sunteți pe pagina 1din 42

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:

● Logger: It is used to log the messages.


● Appender: It is used to publish the logging information to the destination like a file, database, console etc.
● Layout: It is used to format logging information in different styles.
Simona Pitam
Log4j Logger Class

Desc Description Method

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

DESC Description Method


error(Object message) It is used to print the message with the level public void error(Object message)
org.apache.logging.log4j.Level.ERROR
. It is more restricted than the WARN
logging level and is used to log errors and
exceptions.

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

❏ org.apache.logging.log4j.Level.Trace the lowest.

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.

In Log4j2 we have different types of Layout implementation classes:

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

having a thread name on it

Simona Pitam
LOG4J XML FILE

<?xml version="1.0" encoding="UTF-8"?>


<Configuration status="WARN">
<Appenders>
<Console name="ConsoleAppender" target="SYSTEM_OUT">
<PatternLayout
pattern="%d [%t] %-5level %logger{36} - %msg%n%throwable"/>
</Console>
</Appenders>
<Loggers>
<Root level="ERROR">
<AppenderRef ref="ConsoleAppender"/>
</Root>
</Loggers> Simona Pitam
</Configuration>
Install Log4J

Go to the following link:

https://www.apache.org/dyn/closer.lua/logging/log4j/2.11.0/apache-log4j-2.11.0-bin.tar.gz

Download the following tar.gz

Extract the following tar.gz

Configure Path

Add External jar files Simona Pitam


Selenium Grid

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

Selenium Grid has a Hub and a Node.

● 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

below are the major steps involved in this process −

● Configuring the Hub

● Configuring the Nodes

● Develop the Script and Prepare the XML File

● Test Execution

● Result Analysis

Let us discuss each of these steps in detail.

Simona Pitam
Configuring the HUB

Step 1 − Download the latest Selenium Server standalone JAR file

from http://docs.seleniumhq.org/download/. Download it by clicking

on the version as shown below.

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

After running the following command

You will see the following prompt:


2018-04-18 18:15:55.027:INFO:osjs.session:main:
Scavenging every 660000ms
2018-04-18 18:15:55.047:INFO:osjsh.ContextHandler:main:
Started
o.s.j.s.ServletContextHandler@5082d622{/,null,AVAILABLE}
2018-04-18
18:15:55.081:INFO:osjs.AbstractConnector:main: Started
Simona Pitam
ServerConnector@51133c06{HTTP/1.1,[http/1.1]}{0.0.0.0:44
44}
2018-04-18 18:15:55.082:INFO:osjs.Server:main: Started
@1981ms
18:15:55.082 INFO - Nodes should register to
http://10.0.0.4:4444/grid/register/
Selenium Grid hub is
18:15:55.082 INFO -

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.

#sudo java -jar selenium-server-standalone-3.1.jar -role node -hub


http://10.0.0.4:4444/grid/register/
java -jar D:\JAR\selenium-server-standalone-2.42.2.jar
-role node -hub http://10.30.217.157:4444/grid/register
Simona Pitam
-browser browserName = firefox -port 5555
Configure Nodes

WHERE:

D:\JAR\selenium-server-standalone-2.42.2.jar = Location of the Selenium Server Standalone Jar


File(on the Node Machine)

http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub

browserName = firefox (Parameter to specify the Browser name on Nodes)

5555 = Port on which Firefox Node would be up and running.


Simona Pitam
Configuring Nodes

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.

Step 5 − To download the Internet Explorer driver, navigate to http://docs.seleniumhq.org/download/


and download the appropriate file based on the architecture of your OS. After you have downloaded,
unzip the exe file and place in it a folder which has to be referred while launching IE nodes.

Simona Pitam
Selenium Grid

Simona Pitam
Launch IE using the following command

C:\>java -Dwebdriver.ie.driver = D:\IEDriverServer.exe


-jar D:\JAR\selenium-server-standalone-2.42.2.jar
-role webdriver -hub http://10.30.217.157:4444/grid/register
-browser browserName = ie,platform = WINDOWS -port 5558

Where,

D:\IEDriverServer.exe = The location of the downloaded the IE Driver(on the Node Machine)

D:\JAR\selenium-server-standalone-2.42.2.jar = Location of the Selenium Server Standalone Jar


File(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

browserName = ie (Parameter to specify the Browser name on Nodes)

5558 = Port on which IE Node would be up and running.


Grid Console

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.

To download the Chrome Driver, navigate to


http://docs.seleniumhq.org/download/ and then navigate to Third Party
Browser Drivers area and click on the version number '2.10' as shown below
Third Party Drivers

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 ...

Launch Chrome using the following command.


C:\>java -Dwebdriver.chrome.driver = D:\chromedriver.exe
-jar D:\JAR\selenium-server-standalone-2.42.2.jar
-role webdriver -hub http://10.30.217.157:4444/grid/register
-browser browserName = chrome, platform = WINDOWS -port 5557

Where,

D:\chromedriver.exe = The location of the downloaded the chrome Driver(on the Node Machine)

D:\JAR\selenium-server-standalone-2.42.2.jar = Location of the Selenium Server Standalone Jar


File(on the Node Machine) Simona Pitam
http://10.30.217.157:4444 = IP Address of the Hub and 4444 is the port of the Hub

browserName = chrome (Parameter to specify the Browser name on Nodes)

5557 = Port on which chrome Node would be up and running.


Continue ….
Continue ...

Develop the Script and Prepare the XML File


Step 1 − We will develop a test using TestNG. In the following example, we will launch each one of those
browsers using remote webDriver. It can pass on their capabilities to the driver so that the driver has all
information to execute on Nodes.
Simona Pitam

The Browser Parameter would be passed from the "XML" file.


Building A Selenium Grid

I have run my script in Eclipse IDE.

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

object, use the below code.

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

RemoteWebDriver object, use the below lines of code.

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

@AfterTest, I have asked the Node to quit the browser instance.

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.

May 18, 2017 3:09:07 PM org.openqa.selenium.remote.ProtocolHandshake createSession


INFO: Detected dialect: OSS
PASSED: simpleTest

===============================================
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

the below screenshot.

S-ar putea să vă placă și