0% found this document useful (0 votes)
5 views8 pages

Lab Programs 3,4,5

The document provides a comprehensive guide on creating a Maven project, configuring the POM file, and managing dependencies, specifically for using the Gson library to display a Java class in JSON format. It also covers packaging the project as a JAR file using the maven-jar-plugin, deploying a simple website to GitHub Pages, and setting up Selenium and TestNG for automated testing of the website's title. Detailed steps and code snippets are included for each task to facilitate understanding and implementation.

Uploaded by

navyanagraj0903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views8 pages

Lab Programs 3,4,5

The document provides a comprehensive guide on creating a Maven project, configuring the POM file, and managing dependencies, specifically for using the Gson library to display a Java class in JSON format. It also covers packaging the project as a JAR file using the maven-jar-plugin, deploying a simple website to GitHub Pages, and setting up Selenium and TestNG for automated testing of the website's title. Detailed steps and code snippets are included for each task to facilitate understanding and implementation.

Uploaded by

navyanagraj0903
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Topics covered: Creating a Maven Project, Understanding the POM File,

Dependency Management
Program-3:
Create a Java program to create a class called Person with properties
name and age. Display the person details in json format using Gson
libraray. Show how to configure the pom file in the Maven project to
automatically download the Gson dependencies .
Step 1: Create a New Maven Project:
 Open IntelliJ IDEA.
 Go to File > New > Project .
 Select Maven from the project types.
 Set the project name and location, then click Finish .
Step 3: Open the Main.java file under the directory MAVEN_DEMO\src\
main\java\com.example and write the following code
/* Main.java */
package com.example;
import com.google.gson.Gson;
public class Main
{
public static void main(String args[])
{
Gson gson= new Gson();
String json= gson.toJson(new Person("john",30) );
System.out.println(json);
}
}
class Person
{
private String name;
private int age;
public Person(String name, int age)
{
this.name=name;
this.age=age;
}
}

Step-2: Set Up the pom.xml File:


The pom.xml file is where you define dependencies, plugins, and other
configurations for your Maven project.
Example of a basic pom.xml :
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>simple-project</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!-- Add your dependencies here -->
</dependencies>
</project>

Step-3: Add Dependencies for Gson:


Add Gson dependency to the section of the pom.xml file. To accomplish
this, go to www.mvnrepositorry.com and look up "Gson." The code
should then be copied and pasted into the dependencies section.
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.9</version>
</dependency>
</dependencies>

Step-4: Run the Main.main to see the output as follows


{“name”:john, “age”:30}
Topics covered: Plugin management in maven
Program-4:
a) Illustrate the use of maven-jar-plugin to package the project into a jar
file. Showing how to run a main class and show simple output.
b) Develop a basic webpage and demonstrate how to use the relevant
plug-in to deploy it on GitHub pages.
a) Illustrate the use of maven-jar-plugin to package the project into a jar
file. Showing how to run a main class and show simple output.

Steps to Package the Project as a JAR and Run a Main Class


Step 1: Add maven-jar-plugin to pom.xml
To package your Maven project as a JAR file and specify the main class,
we need to configure the maven-jar-plugin in the pom.xml . Add the
following configuration to your pom.xml :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<!-- Specify the main class to be executed
-->
<archive>
<manifestEntries>
<Main-Class>com.example.Main</Main-
Class>
</manifestEntries>
</archive>
</configuration>
</plugin>
</build>
</plugins>

This will tell Maven to include the Main-Class in the JAR manifest and
specify the main class that should be executed when the JAR is run.
Step-2: Create a Main Class
In your src/main/java/com.example/Main class, write this code.
package com.example;
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello, this is a simple output from the main

class!");
}
}
This class contains a simple main method that prints output when run.

Step 3: Package the Project into a JAR:


After configuring the plugin and creating the MainClass , run the
following Maven command to build the project and package it into a JAR
file:
mvn clean package
This will clean any previous builds, compile the source code, and
package it into a JAR file located in the target directory (e.g.,
target/your-project-name.jar ).
Step 4: Run the JAR File:
Once the JAR is created, you can run it with the following command:
java -jar target/your-project-name.jar
This will execute the main method from your MainClass and print the
message:
Hello, this is a simple output from the main class!
b) Develop a basic webpage and demonstrate how to use the relevant
plug-in to deploy it on GitHub pages.
Step-1: Create a Simple Website (HTML, CSS, and Logo)
In the src/main/resources folder, create an index.html file, a style.css
file, and place the logo.png image
/* index.html */
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>My Simple Website</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<img src="logo.png" alt="Logo">
</header>
<h1>Welcome to My Simple Website</h1>
</body>
</html>

/* style.css */
body
{
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
}
header img
{
width: 100px;
}

Step 3: Upload the Website to GitHub:


 Initialize a Git repository in your project folder:
git init
 Add your files and commit them:
git add .
git commit -m "Initial commit"
 Create a GitHub repository and push the local project to GitHub:
git remote add origin <your-repository-url>
git push -u origin master
Step 3: Deployment
To deploy your Maven project to GitHub Pages using the /docs folder (
** having all files inside root folder/dir not recommended ), you
can follow these simple steps. This method is easy and doesn't require
switching branches—just
use the /docs folder of your main branch.
 Modify Maven Configuration to Copy Static Files to /docs
Folder: First, you need to ensure that Maven places your static
files ( index.html , style.css , logo.png ) into the /docs folder
instead of the target directory.To do this, configure the Maven
Resources Plugin in your pom.xml to copy the files directly into
/docs :
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<phase>prepare-package</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.basedir}/docs</outputDirectory>
<!-- Deploy to /docs folder -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include> <!-- Copy all files in src/main/resources -->
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
In this configuration:
The maven-resources-plugin copies all files from
src/main/resources to the /docs
folder in the root of your project(not the target directory).This is
done during the
prepare-package phase, just before Maven prepares the package.
 Build the Project: Run the following Maven command to build your project and
copy the resources to the /docs folder:
mvn clean install
After this, your index.html , style.css , and logo.png files should now be inside the docs
folder in the root of your project.
 Push Changes to GitHub: Now that the files are in the /docs folder, they are ready
to be served by GitHub Pages.
Follow these steps:
Stage the changes (the updated /docs folder):
git add docs
git commit -m "Deploy site to GitHub Pages"
Push to GitHub:
git push origin master
 Enable GitHub Pages: After pushing to the main branch, follow these steps to
enable GitHub Pages:
o Go to your GitHub repository.
o Navigate to Settings > Pages (on the left sidebar).
o Under the Source section, select the main branch and /docs folder as the source.
o Click Save.
 Access Your Website: Your static website is now hosted on GitHub Pages! You
can access it at
https://<your-github-username>.github.io/<your-repository-name>/
Topics covered: Configuration of pom.xml of maven project with
selenium and TestNG for automation testing
Program-5:
Write the java class Testing the title of your website using Selenium,
Java, and TestNG:
To test the title of your website using Selenium, Java, and TestNG,
follow these steps. This will include the installation of necessary
dependencies, creating test scripts, and running tests.
Step 1: Set Up Selenium and TestNG Dependencies
In your Maven project created in program 4b, add the necessary
dependencies for Selenium WebDriver and TestNG to the pom.xml
file:
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java
-->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.28.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
 Selenium WebDriver: This is used for browser automation.
 TestNG: This is a testing framework used to run Selenium tests.
Step 2: Create Selenium Test Class Using TestNG
Next, create a test class in your src/test/java directory. You can name
WebsiteTitleTest.java.

/* WebsiteTitleTest.java */
package org.test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
public class WebPageTest
{
private static WebDriver driver;
@BeforeTest
public void openBrowser() throws InterruptedException
{
driver = new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
driver.get("https://sarvarbegum-coder.github.io/LAB_1/");
}
@Test
public void titleValidationTest()
{
String actualTitle = driver.getTitle();
String expectedTitle = "My simple website";
Assert.assertEquals(actualTitle, expectedTitle);
assertTrue(true, "Title should contain 'simple'");
}
@AfterTest
public void closeBrowser() throws InterruptedException
{
Thread.sleep(1000);
driver.quit();
}
}
Step 3: Run the Test Using TestNG
Option 1: Run TestNG from IntelliJ IDEA
1. Right-click the WebpageTest.java file.
2. Select Run WebpageTest.
IntelliJ IDEA will execute the TestNG test and show the results in the
output console.
Option 2: Run TestNG via Command Line
If you want to run the tests from the command line, use the following
Maven command:
mvn test

You might also like