Friday, August 19, 2022

learning npm

 1. what is --y in npm command?

will simply generate an empty npm project without going through an interactive process.

Wednesday, April 20, 2022

How to Build and Execute Selenium projects using Gradle?

Gradle is a build automation system that is fully open-source and uses the concepts of Apache Maven and Apache Ant. Either single or multiple "build.gradle" file describes the Gradle project task. At least one build file must be located in the root folder of the project. Each build file defines a project and its tasks.

  • Gradle allows us to write the build script with Java programing language.

  • It is easy to use and maintain projects and their dependencies.

  • It provides high-performance and scalable builds.

  • Having better dependency management like ReplacedBy rules

The gradle integration process is quite easier.

To add dependencies to a project, state the dependency configuration like the dependencies block of the build.gradle file.

Installing Gradle in macOS

Step 1: Install via Homebrew package manager. Open a terminal in   Mac and execute the below command 



Step 2: Once installation is complete, check the installed Gradle version using"gradle -v" command.


Note: Installation of Gradle in Windows is pretty straightforward. You just need to download, unzip, and set the path in the System Path.

Creating Gradle project in IntelliJ IDEA


Gradle comes along with Latest IntelliJ IDEA IDE, so we dont need any specific installation. 

Step 1: From the IntelliJ IDEA Home page, click "New Project" -> select "Gradle" from the left panel. 



Step 2: Select the Java check box. Click Next and mention the Project name ( FirstSeleniumGradleProject). Mention GroupId as required.


Once created the new project structure and default build.gradle file looks like this 



Step 3: Add Selenium and TestNG dependency for the project.  Fetch the required dependency from Maven public repository, navigate to gradle tab and copy the statement in the below format and paste it directly into the build.gradle file. 


We need to add Selenium -Java, TestNG and WebDriverManager dependencies to start with our est cases.



I am going to use the TestNG framework, so I  can remove default JUnit dependencies and useJUnit() test.


Step 4: Add useTestNG test .When specifying useTestNG(), Gradle scans for all the methods annotated with @Test and executes them.

Step 5: Once you have added all the dependencies, click “Load Gradle Changes“ from the gradle file.

Step 6: Now, the Project is ready to write our Selenium test case. Right-click src/test/java and click New->Java Class and enter the name as LTSimpleFormTest. In this class, we validate the Simple Input Form from Selenium Playground.


This test pass the values to the text box, click the "Get Checked Value"button and validate the message under "Your Message" section .



Code Snippet


 

import io.github.bonigarcia.wdm.WebDriverManager;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.annotations.Test;

 

   public class LTSimpleFormTest {

       @Test //TestNG Annotation

       public void validateCheckBoxStatus (){

           /**WebDriverManager is an open Source API to ease the Browser Initialisation .

            We can skip driver exe path definition**/

           WebDriverManager.chromedriver().setup();

 

           //Chrome Browser Initialized

           WebDriver driver= new ChromeDriver();

 

           //Launch the URL

           driver.get("https://www.lambdatest.com/selenium-playground/simple-form-demo");

           //get the WebElement of textInput

           WebElement userTestBox= driver.findElement(By.id("user-message"));

           ///send values to textbox

           userTestBox.sendKeys("Welcome To LambdaTest");

           WebElement getValueButton= driver.findElement(By.id("showInput"));

           getValueButton.click();

           WebElement checktextArea= driver.findElement(By.id("message"));

         //validate the fetched text

         Assert.assertEquals(checktextArea.getText(),"Welcome To LambdaTest","Message is    different");

       }

   }


                                                                  GitHub  URL


Code Walkthrough




WebDriverManager is an open-source project from bonigarcia, which helps to skip the process of setting the driver executable path.




WebDriver instance of ChromeDriver object launches the Chrome Driver and get() method open the mentioned URL in the Chrome session .


The text box is identified by id "üser-message" and sendKeys method of driver object pass values as"Welcome To LambdaTest" to the selected textBox element.






The button identified by id "showInput" clicked by click() method.



The Text area element is identified by its id, and the getText() method fetches the text value of the element.


The assetEqals method Assert Class verifies whether the fetched text is the same as "Welcome To LambdaTest " which is actually entered in the text field by the previous step is same or not, if not it fails the test case with "Message is different "error message.

Executing Gradle project

The Selenium Maven project can be executed in multiple ways.

1. Execute as TestNG class File

Right-click the LTSimpleFormTest file -> Run LTSimpleFormTest file . After successful execution, you can see testNG reports under the target folder. 

After successful execution, Gradle generates its own reports under the build folder

 

Open the index. html file in the browser, you can see the results 

If you need a report from TestNG, you must add listeners and a report folder in build.gradle file before start execution.

2. Use Gradle  file to execute TestClass

In IDE terminal, type   "gradle clean test". It execute all the test cases under src/test/java package and generate results .

If you need to execute only one test method from the test class , for example here only validateCheckBoxStatus in LTSimpleFormTest class then  use

 "gradle test --tests LTSimpleFormTest.validateCheckBoxStatus"

3. Execute Testsuite.xml via Gradle

In times, you need to have multiple test cases in place and pass common parameters to the test cases and include and exclude test cases based on current test execution. All can be achieved by creating suite xml file . 

TestNG Suire file  looks like 

 

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

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite thread-count="1" parallel="none" name="GradleSuite">

   <test  name="GradleTest">

       <classes>

         <class name="LTSimpleFormTest">

             <methods>

                 <exclude name="checkSkipped"></exclude>           

             </methods>

         </class>

       </classes>

   </test> <!-- Test -->

</suite> <!-- Suite -->

 

Now add this suite xml file to build.gradle under test

test{

 useTestNG() {

     // To generate reports by TestNG library

   //  useDefaultListeners = true

   //  outputDirectory = file("$projectDir/target")

     suites file("$projectDir/src/"+"testSuite.xml")

 }

}


After adding the suit XML file, execute "gradle clean test" command from terminal trigger the suite file and generate reports under build folder.