Tuesday, March 21, 2023

DB connection using c#

 public static string GetDataBaseTableColumnValue(string query, string column)

        {

            string ColumnValue = "";

            SqlCommand command;

            SqlDataReader reader;

            resultDt = new DataTable();


            string sql = null, _connectionString = null;

            

             _connectionString = @"Data Source=SB;Initial Catalog=DBNAME;User ID=GMR;Password=gmr@123";


            

            /*

                        if (AppConfigReader.Env.ToLower() == "betavalue")

                        {


                            _connectionString = @"Data Source=FXecute.Cons.GC.IT.SQL;Initial Catalog=FXecute;Integrated Security=True;";

                        }

                        else if (AppConfigReader.Env.ToLower() == "devvalue")

                        {

                            _connectionString = @"Data Source=FXecute.Cons.GC.IT.SQL;Initial Catalog=FXecute;Integrated Security=True;";


                        }

                        else if (AppConfigReader.Env.ToLower() == "demovalue")

                        {

                            _connectionString = @"Data Source=Fxecute.dev.gc.it.sql;Initial Catalog=FXecute;Integrated Security=True;";

                        }

            */


            using (SqlConnection connection = new SqlConnection(_connectionString))

            {

                try

                {

                    connection.Open();


                    if (!string.IsNullOrEmpty(column))

                    {

                        sql = query;

                    }

                    else

                    {

                        //ReportGenerator.LogFail("Column is not valid");

                    }


                    command = new SqlCommand(sql, connection);


                    using (reader = command.ExecuteReader())

                    {

                        resultDt.Load(reader);

                    }

                }

                catch (Exception)

                {

                   

                }

                finally

                {

                    connection.Close();

                }

            }


            try

            {

                if (resultDt.Rows.Count != 0)

                {

                    foreach (DataRow Tablevalue in resultDt.Rows)

                    {

                        ColumnValue = Tablevalue[column].ToString().Trim();

                        break;

                    }

                }

            }

            catch (Exception)

            {

                //ReportGenerator.LogFail("Insufficient Data present in the DataBase");

            }

            //ReportGenerator.Loginfo("Executed Query:" + query);

            if (ColumnValue == "")

            {

               // ReportGenerator.Loginfo("Fetching " + column + " from the DataBase: " + "Value is not yet generated");

            }

            else

            {

               // ReportGenerator.Loginfo("Fetching " + column + " from the DataBase: " + ColumnValue);

            }

            return ColumnValue;

        }


Friday, March 3, 2023

Apache POI In Selenium

 

Read Data 



import org.openqa.selenium.WebDriver;

import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelTest {
public static void main (String [] args) throws IOException{
//Path of the excel file
FileInputStream fs = new FileInputStream("D:\\DemoFile.xlsx");
//Creating a workbook
XSSFWorkbook workbook = new XSSFWorkbook(fs);
XSSFSheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(sheet.getRow(0).getCell(0));
Row row1 = sheet.getRow(1);
Cell cell1 = row1.getCell(1);
System.out.println(sheet.getRow(0).getCell(1));
Row row2 = sheet.getRow(1);
Cell cell2 = row2.getCell(1);
System.out.println(sheet.getRow(1).getCell(0));
Row row3 = sheet.getRow(1);
Cell cell3 = row3.getCell(1);
System.out.println(sheet.getRow(1).getCell(1));
//String cellval = cell.getStringCellValue();
//System.out.println(cellval);
}
}



Write Data 

The code below is used to write data into an Excel file in Selenium.

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Writei {
public static void main(String[] args) throws IOException {
String path = "D://DemoFile.xlsx";
FileInputStream fs = new FileInputStream(path);
Workbook wb = new XSSFWorkbook(fs);
Sheet sheet1 = wb.getSheetAt(0);
int lastRow = sheet1.getLastRowNum();
for(int i=0; i<=lastRow; i++){
Row row = sheet1.getRow(i);
Cell cell = row.createCell(2);

cell.setCellValue("WriteintoExcel");

}

FileOutputStream fos = new FileOutputStream(path);
wb.write(fos);
fos.close();
}

}

Excel data read in Java using JXL JAR

 package com.pack;

import java.io.FileInputStream; import java.io.IOException; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; public class ReadExcelFile { public void readExcel() throws BiffException, IOException { String FilePath = "D:\\sampledoc.xls"; FileInputStream fs = new FileInputStream(FilePath); Workbook wb = Workbook.getWorkbook(fs); // TO get the access to the sheet Sheet sh = wb.getSheet("Sheet1"); // To get the number of rows present in sheet int totalNoOfRows = sh.getRows(); // To get the number of columns present in sheet int totalNoOfCols = sh.getColumns(); for (int row = 0; row < totalNoOfRows; row++) { for (int col = 0; col < totalNoOfCols; col++) { System.out.print(sh.getCell(col, row).getContents() + "\t"); } System.out.println(); } } public static void main(String args[]) throws BiffException, IOException { ReadExcelFile DT = new ReadExcelFile(); DT.readExcel(); } }


import org.json.JSONArray;
import org.json.JSONObject;

public class JSONTest{

    public static void main(String[] args) {

        var user = new JSONObject();

        user.put("name", "John Doe");
        user.put("occupation", "gardener");
        user.put("siblings", Integer.valueOf(2));
        user.put("height", Double.valueOf(172.35));
        user.put("married", Boolean.TRUE);

        var cols = new JSONArray();
        cols.put("red");
        cols.put("blue");
        cols.put("navy");

        user.put("favCols", cols);

        var userJson = user.toString();

        System.out.println(userJson);
    }
}

Thursday, March 2, 2023

rest sharp using Newton soft.josn

using Newtonsoft.Json;
using RestSharp;

namespace SampleRestSharpTest
{
    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }

    public class SampleAPITest
    {
        private RestClient client;
        private readonly string BASE_URL = "https://jsonplaceholder.typicode.com";

        public SampleAPITest()
        {
            client = new RestClient(BASE_URL);
        }

        public void TestPostUser()
        {
            RestRequest request = new RestRequest("/users", Method.POST);
            User newUser = new User { Id = 1, Name = "John Doe", Email = "johndoe@test.com" };
            string serializedUser = JsonConvert.SerializeObject(newUser);
            request.AddParameter("application/json", serializedUser, ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            Assert.AreEqual(201, (int)response.StatusCode);
            User createdUser = JsonConvert.DeserializeObject<User>(response.Content);
            Assert.AreEqual("John Doe", createdUser.Name);
        }
    }
}

Wednesday, March 1, 2023

Cypress Install

 Cypress installation 


To install Cypress, you can follow these steps:


Install Node.js: Cypress requires Node.js to be installed on your machine. You can download and install Node.js from their official website: https://nodejs.org/en/.


Create a new project: Create a new directory for your Cypress project, navigate to that directory in the terminal or command prompt, and run the following command:



npm init

This will create a new package.json file in your project directory.


Install Cypress: Run the following command in your project directory:



npm install cypress --save-dev

This will install Cypress and add it as a devDependency to your package.json file.


Open Cypress: Once the installation is complete, you can open Cypress by running the following command in your project directory:



npx cypress open

This will open the Cypress Test Runner, where you can write and run your tests.


That's it! You now have Cypress installed and ready to use in your project