Thursday, June 20, 2024

Maven Basics for Automation Testing-2

 

Maven Project Creation:

Creating a Maven Project:

  1. Through Eclipse (Preferred):

    • File > New > Other > Search for 'maven' > Select 'Maven Project' > Next > Select 'Create simple project' > Next.
    • Enter GroupId (e.g., com.hcl) and ArtifactId (e.g., SampleMavenProject) > Finish.
  2. Through Command Prompt:

    • Use the following command to create a Maven project. Change the groupId and artifactId as needed.
      sh
      mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

    Example 1: Create Maven Project "MavenProjectThroughCommand"

    sh
    mvn archetype:generate -DgroupId=com.birla -DartifactId=MavenProjectThroughCommand -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

    Example 2: Create Maven Project "mavenProjectCmd2"

    sh
    mvn archetype:generate -DgroupId=com.birla -DartifactId=mavenProjectCmd2 -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

    Homework:

    • Create mavenProjectCmd3
    • Create mavenProjectCmd4

Check Maven Project Created in Specific Location:

  • Ensure the Maven project is created at the specified location: C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace

Verify Maven Project Creation:

  • Navigate to the project folder location:
    sh
    C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace
    • Check for the created project (e.g., MavenProjectThroughCommand) and its folder structure.
    • Verify the presence of POM.xml file.

Import Existing Maven Project in Eclipse:

  • Steps to import:
    • Right-click on the Project window > Import > Search for Maven Project (Existing Maven Projects) > Specify the root directory (e.g., C:\brahma\Practise\SelniumPractiseNew\Apr32023WorkSpace\SampleMavenProjectThruCommand2) > Select the pom.xml file > Finish.

Homework:

  • Refer to pom.xml to check for default JUnit dependencies.
  • Examine Maven project structure.
  • Add Maven dependencies in POM.xml for Apache POI, Log4j, and Extent Reports.

POM.xml File

The pom.xml file is where you define all dependencies, which Maven will download and add to your project automatically.

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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.hcl</groupId> <artifactId>SampleMavenProject</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java --> <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.18.0</version> </dependency> </dependencies> </project>

Adding Dependencies:

  1. Selenium:

    xml
    <dependency> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> <version>4.18.0</version> </dependency>
  2. Commons-IO:

    xml
    <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.13.0</version> </dependency>
  3. TestNG:

    xml
    <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>7.0.0</version> <scope>test</scope> </dependency>
  4. Apache POI:

    xml
    <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>5.0.0</version> </dependency>
  5. WebDriverManager:

    xml
    <dependency> <groupId>io.github.bonigarcia</groupId> <artifactId>webdrivermanager</artifactId> <version>4.4.3</version> </dependency>
  6. Extent Reports:

    xml
    <dependency> <groupId>com.aventstack</groupId> <artifactId>extentreports</artifactId> <version>4.1.0</version> </dependency>
  7. Log4j:

    xml
    <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>

Verifying Dependency Installation:

  • Check the "Maven Dependencies" folder in the project to ensure that the required JAR files have been added.
  • Check the local repository (e.g., C:\Users\Lenovo\.m2\repository) to verify the downloaded JAR files.

Integrating Maven with Eclipse:

  • Maven integrates easily with Eclipse through the installation of the Maven plugin.
  • Maven plugins, like the Maven Surefire Plugin, can be used to run testng.xml files and maintain a common project folder structure.

By using Maven, you streamline the process of managing dependencies and building your Java projects, ensuring a standardized approach and reducing manual efforts.

Creating a Maven Test:

  1. Create a Test Class:

    • Name the class ending with "Test".
    java
    package com.accenture; import static org.junit.Assert.assertTrue; import org.junit.Test; /** * Unit test for simple App. */ public class AppTest { /** * Rigorous Test :-) */ @Test public void shouldAnswerWithTrue() { System.out.println("My first Maven test"); assertTrue(true); } }
  2. Run Maven Test:

    • Right-click on the project > Run As > Maven Test.
    • Maven will run all test classes in src/test/java where the class name ends with "Test".
o/p:
---
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.birla:MavenProjectThroughCommand >----------------
[INFO] Building MavenProjectThroughCommand 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ MavenProjectThroughCommand ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\target\classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ MavenProjectThroughCommand ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ MavenProjectThroughCommand ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.birla.AppTest
running maven test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.111 s - in com.birla.AppTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.150 s
[INFO] Finished at: 2024-06-19T08:21:15+05:30
[INFO] ------------------------------------------------------------------------


-----------------------
Create class = MobileTest :
----------------------------

package com.birla;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class MobileTest 
{
@Test
    public void testA()
    {
  System.out.println("Calling Mobile Test");
           assertTrue( true );
    }


}



Run Mave test :
---------------
Right click on project >  run as "Maven tests"
When we run as maven tests >  it goes to "src/test/java"  folder > it runs all classes where class name ends with "Test"

o/p:
-----
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/configuration/org.eclipse.osgi/6/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/configuration/org.eclipse.osgi/6/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.birla:MavenProjectThroughCommand >----------------
[INFO] Building MavenProjectThroughCommand 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ MavenProjectThroughCommand ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ MavenProjectThroughCommand ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ MavenProjectThroughCommand ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.birla.AppTest
running maven test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.097 s - in com.birla.AppTest
[INFO] Running com.birla.MobileTest
Calling Mobile Test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.birla.MobileTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.137 s
[INFO] Finished at: 2024-06-19T08:25:32+05:30
[INFO] ------------------------------------------------------------------------


-----------------------------
Create class = AndroidTest :
-----------------------
package com.birla;

import static org.junit.Assert.assertTrue;

import org.junit.Test;

public class AndroidTest
{
@Test
    public void testB()
    {
  System.out.println("Calling Android Test");
        assertTrue( true );
    }

}


--------------------------------------
Run Maven tests :
------------------------
Right click on project >  run as "Maven tests"
When we run as maven tests >  it goes to "src/test/java"  folder > it runs all classes where class name ends with "Test"

o/p:
----
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/configuration/org.eclipse.osgi/6/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/configuration/org.eclipse.osgi/6/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.birla:MavenProjectThroughCommand >----------------
[INFO] Building MavenProjectThroughCommand 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ MavenProjectThroughCommand ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ MavenProjectThroughCommand ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 3 source files to C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\target\test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ MavenProjectThroughCommand ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.birla.AndroidTest
Calling Android Test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.173 s - in com.birla.AndroidTest
[INFO] Running com.birla.AppTest
running maven test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0 s - in com.birla.AppTest
[INFO] Running com.birla.MobileTest
Calling Mobile Test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.002 s - in com.birla.MobileTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.626 s
[INFO] Finished at: 2024-06-19T08:29:43+05:30
[INFO] ------------------------------------------------------------------------


-------------------------------
Create class name  without Test : ios
--------------------------------
package com.birla;

import org.junit.Test;

 class ios //   this class will not get executed as Class name does end with Test
//class IOSTest  //   gets executed
// class IOS // this class will not get executed as Class name does end with 'Test'

// class IOStest //   does not get executed as class name does not end with "Test" 
// always Give Test,   but not 'test'
// class IOSTEST ////   does not get executed as class name does not end with "Test
 
{
@Test
public void testC()
{
System.out.println("hi IOS Test");


}




--------------------------------------
Run Maven tests :
------------------------
Right click on project >  run as "Maven tests"
When we run as maven tests >  it goes to "src/test/java"  folder > it runs all classes where class name ends with "Test"

console o/p:
--------------
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/configuration/org.eclipse.osgi/6/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/plugins/org.eclipse.m2e.maven.runtime.slf4j.simple_1.16.0.20200610-1735/jars/slf4j-simple-1.7.5.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [file:/C:/brahma/seleniumSoftwares/eclipse-jee-2021-03-M2-win32-x86_64/eclipse/configuration/org.eclipse.osgi/6/0/.cp/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.SimpleLoggerFactory]
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< com.birla:MavenProjectThroughCommand >----------------
[INFO] Building MavenProjectThroughCommand 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:compile (default-compile) @ MavenProjectThroughCommand ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ MavenProjectThroughCommand ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\brahma\Practise\SelniumPractiseNew\March52024MyWorkspace\MavenProjectThroughCommand\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.8.0:testCompile (default-testCompile) @ MavenProjectThroughCommand ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.22.1:test (default-test) @ MavenProjectThroughCommand ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running com.birla.AndroidTest
Calling Android Test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.148 s - in com.birla.AndroidTest
[INFO] Running com.birla.AppTest
running maven test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.007 s - in com.birla.AppTest
[INFO] Running com.birla.MobileTest
Calling Mobile Test
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.001 s - in com.birla.MobileTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  4.838 s
[INFO] Finished at: 2024-06-19T08:40:26+05:30
[INFO] ------------------------------------------------------------------------

***************************************************

HW Create Maven test classes in "src/test/java"  and run as maven test  as same as previous program?


No comments:

Post a Comment

git commands MCQ

 Here are some multiple-choice questions (MCQs) on Git commands relevant for Selenium: 1. Which Git command is used to clone a remote reposi...