Search This Blog

Thursday, May 27, 2021

Test Cafe

 What is Test Cafe

  • Allows you to write tests using TypeScript or JavaScript
  • End-to-end free and open source automation tool which is used to test web applications
  • Works on all popular environments such as Windows, MacOS, and Linux
  • Easy to install feature in a single command
  • Does not depend on Selenium or other testing software

How to Install

npm install -g testcafe


Sample Program

import { Selector } from 'testcafe';

fixture `Getting Started`// declare the fixture

    .page `https://devexpress.github.io/testcafe/example`;  // specify the start page

//then create a test and place your code there

test('My first test', async t => {

    await t

        .wait(2000)

        .typeText('#developer-name', 'John Smith')

        .click('#submit-button')

        // Use the assertion to check if the actual header text is equal to the expected one

        .expect(Selector('#article-header').innerText).eql('Thank you, John Smith!')

        .wait(2000);

});


How to Run

testcafe chrome test1.js


Fiture: TestCafe tests are organized into categories called fixuture. Fixture can be compared with Module where test cases related to similar functionalities are maintained.

Declare Fixture

fixture ( fixtureName )

fixture `fixtureName`


Test: test case/functionality to be verified

test ( 'testName', fn(t))


Test Controller: object t exposes the test API's methods. Thus it is passed to each function.


Page can be written in two ways

1. fixture `fixtureName`

    .page `PageUrl`;

2. test

    .page `PageUrl`

('testName', fn(t));


Sunday, May 16, 2021

SQL Queries Interview Questions

Q1. Second Highest salary of employee

select

distinct top 1 *

from

employees

where

Salary < (Select MAX(Salary) from employees)

order by Salary desc


select

distinct top 1 *

from

employees

where

Salary < (Select distinct top 1 salary from employees order by Salary desc)

order by Salary desc


Select

top 1 *

from (

Select distinct top 2 * 

from 

employees

order by Salary desc

) innerQuery

order by salary asc


Select emp.*

from (

Select *, row_number() OVER (Order by salary desc

) as rn from employees) innerQuery

inner join employees emp on emp.id = innerQuery.id

where innerQuery.rn =2


Q2. Select Third Highest Data

select

distinct top 1 *

from

(Select distinct top 3 * from employees order by salary desc) innerQuery

order by salary asc

Q3. Select Duplicate Data

Select * 

from employees

where salary in 

(

Select salary from employees group by salary having count(salary) > 1

)

order by salary desc


select employees.*

from

(

select max(emp1.id) empId

from employees emp1

inner join employees emp2

on emp1.salary = emp2.salary

and emp1.id <> emp2.id

group by emp1.salary

) innerQuery

inner join employees

on innerQuery.empId = employees.id

order by employees.id


Q4. Delete Duplicate Data


Delete

from employees

where id in (

Select max(id) from employees group by salary having count(salary) > 1

)


Delete employees

from

(

select max(emp1.id) empId

from employees emp1

inner join employees emp2

on emp1.salary = emp2.salary

and emp1.id <> emp2.id

group by emp1.salary

) innerQuery

inner join employees

on innerQuery.empId = employees.id


Q5. Fetch monthly salary if annual salary is given

Select *, salary/12 as monthlySalary from employees


Q6. How to fetch top 3 highest salary


select * from 

(Select *, row_number() over (order by salary desc) rn from employees) innerQuery

where innerQuery.rn <=3


select top 3 * from employees order by salary desc


Q7. Select employee details having odd id

select * from employees where id%2=1


Q8. Select employee details having even id

select * from employees where id%2=0


Q9. How Can I create table with same structure of table without data

select * into NEW_TABLE from EXISTING_TABLE where 1=2


Q10. How Can I create table with same structure of table with data

select * into NEW_TABLE from EXISTING_TABLE


Q11. Display last 50% records from Employee table

select * from

(Select *,row_number() over(order by id) rn from employees) innerQuery

where innerQuery.rn >= (select count(id)/2 from employees)


Q12. How do I fetch only common records between 2 tables

select * from employees

intersect

select * from employees_new


Q13. Find Query to get information of Employee where Employee is not assigned to the department

select * from employees where deptid not in (select id from department)


select * from employees

except

select emp.* from employees emp

inner join department dep

on emp.deptid = dep.Id


select emp.* from employees emp

left join department dep

on emp.deptid = dep.Id

where dep.Id is null


Q14. How to get distinct records from the table without using distinct keyword.

select * from employees where id in (select max(id) from employees group by FirstName, LastName)


Q15. Select all records from Employee table whose name is ‘Jitendra’ and ‘Ram’

select * from employees where FirstName in ('Jitendra', 'Ram')


Q16. How to fetch all the records from Employee whose joining year is  2017

select * from employees where substring(convert(varchar,DOJ,103),7,4)='2017'


select * from employees where DOJ like '2017%'


Q17. What is SQL Query to find maximum salary of each department?

select max(emp.Salary), dep.DepartmentName from employees emp

inner join department dep

on emp.deptid = dep.Id

group by dep.DepartmentName


select max(emp.Salary), emp.deptid from employees emp group by emp.deptid


Q18. How Do you find all Employees with its managers?

select emp.FirstName + ' ' + emp.LastName as EmpName, manager.FirstName + ' ' + manager.LastName as ManagerName

from employees emp, employees manager

where emp.ManagerId = manager.id


select emp.FirstName + ' ' + emp.LastName as EmpName, manager.FirstName + ' ' + manager.LastName as ManagerName

from employees emp

inner join employees manager

on emp.ManagerId = manager.id


Q19. How to find count of duplicate rows

select FirstName, count(FirstName) cnt from employees group by FirstName, LastName, Location having count(FirstName) > 1


Q20. How to Find the Joining date of Employee in YYYY-DAY-Date format.

select SUBSTRING(CONVERT(VARCHAR, DOJ, 121), 1, 4) + '-' + SUBSTRING(CONVERT(VARCHAR, DOJ, 100), 1, 3) + '-' + SUBSTRING(CONVERT(VARCHAR, DOJ, 103), 1,2) from employees


Q20. Display current date in different format

select CONVERT(VARCHAR, GETDATE(), 100) '100'

select CONVERT(VARCHAR, GETDATE(), 101) '101'

select CONVERT(VARCHAR, GETDATE(), 102) '102'

select CONVERT(VARCHAR, GETDATE(), 103) '103'

select CONVERT(VARCHAR, GETDATE(), 104) '104'

select CONVERT(VARCHAR, GETDATE(), 105) '105'

select CONVERT(VARCHAR, GETDATE(), 106) '106'

select CONVERT(VARCHAR, GETDATE(), 107) '107'

select CONVERT(VARCHAR, GETDATE(), 108) '108'

select CONVERT(VARCHAR, GETDATE(), 109) '109'

select CONVERT(VARCHAR, GETDATE(), 110) '110'

select CONVERT(VARCHAR, GETDATE(), 111) '111'

select CONVERT(VARCHAR, GETDATE(), 112) '112'

select CONVERT(VARCHAR, GETDATE(), 113) '113'

select CONVERT(VARCHAR, GETDATE(), 114) '114'

select CONVERT(VARCHAR, GETDATE(), 120) '120'

select CONVERT(VARCHAR, GETDATE(), 121) '121'

select CONVERT(VARCHAR, GETDATE(), 126) '126'

select CONVERT(VARCHAR, GETDATE(), 127) '127'

select CONVERT(VARCHAR, GETDATE(), 130) '130'

select CONVERT(VARCHAR, GETDATE(), 131) '131'

Output

May 16 2021 10:41PM

05/16/2021

2021.05.16

16/05/2021

16.05.2021

16-05-2021

16 May 2021

May 16, 2021

22:41:11

May 16 2021 10:41:11:850PM

05-16-2021

2021/05/16

20210516

16 May 2021 22:41:11:850

22:41:11:850

2021-05-16 22:41:11

2021-05-16 22:41:11.850

2021-05-16T22:41:11.850

2021-05-16T22:41:11.850

 5 ???? 1442 10:41:11:850PM

 5/10/1442 10:41:11:850PM









Oops Interview Question - 3

 52. What is the difference between a Hash Table and a Dictionary?

HashtableDictionary
A Hashtable is a non-generic collection.A Dictionary is a generic collection.
Hashtable is defined under System.Collections namespace.Dictionary is defined under System.Collections.Generic namespace.
In Hashtable, you can store key/value pairs of the same type or of the different type.In Dictionary, you can store key/value pairs of same type.
In Hashtable, there is no need to specify the type of the key and value.In Dictionary, you must specify the type of key and value.
The data retrieval is slower than Dictionary due to boxing/ unboxing.The data retrieval is faster than Hashtable due to no boxing/ unboxing.
In Hashtable, if you try to access a key that doesn’t present in the given Hashtable, then it will give null values.In Dictionary, if you try to access a key that doesn’t present in the given Dictionary, then it will give error.
It is thread safe.It is also thread safe but only for public static members.
It doesn’t maintain the order of stored values.It always maintain the order of stored values.


Example Hash Table

using System;
using System.Collections;
  
class GFG {
  
    // Main method
    static public void Main()
    {
  
        // Create a hashtable
        // Using Hashtable class
        Hashtable my_hashtable = new Hashtable();
  
        // Adding key/value pair in the hashtable
        // Using Add() method
        my_hashtable.Add("A1", "Welcome");
        my_hashtable.Add("A2", "to");
        my_hashtable.Add("A3", "GeeksforGeeks");
  
        foreach(DictionaryEntry element in my_hashtable)
        {
            Console.WriteLine("Key:- {0} and Value:- {1} ",
                               element.Key, element.Value);
        }
    }
}

Output:

Key:- A3 and Value:- GeeksforGeeks 
Key:- A2 and Value:- to 
Key:- A1 and Value:- Welcome 

Example Dictionary

using System;
using System.Collections.Generic;
  
class GFG {
  
    // Main Method
    static public void Main()
    {
  
        // Creating a dictionary
        // using Dictionary<TKey, TValue> class
        Dictionary<string, string> My_dict = 
                    new Dictionary<string, string>();
  
        // Adding key/value pairs in the Dictionary
        // Using Add() method
        My_dict.Add("a.01", "C");
        My_dict.Add("a.02", "C++");
        My_dict.Add("a.03", "C#");
  
        foreach(KeyValuePair<string, string> element in My_dict)
        {
            Console.WriteLine("Key:- {0} and Value:- {1}"
                              element.Key, element.Value);
        }
    }
}

Output:

Key:- a.01 and Value:- C
Key:- a.02 and Value:- C++
Key:- a.03 and Value:- C#

53. What are the state management techniques used in .Net?

HTTP Protocol is a Stateless protocol HTTP/HTTPs doesn't remember what website or URL we visited, or in other words we can say it doesn't hold the state of a previous website that we visited before closing our browser, that is called stateless. So how to maintain the state for request. This is called state management.

State Management Types

  • Client-side Management
  • Server-side Management

Client-side Techniques

Client-Side State Management, the state related information will directly get stored on the client-side. That specific information will travel back and communicate with every request generated by the user then afterwards provides responses after server-side communication.

  • View
  • Hidden
  • Cookies
  • Control State
  • Query Strings

Server-side Techniques

In Server-Side State Management all the information is stored in the user memory. Due to this functionality there is more secure domains at the server side in comparison to Client-Side State Management.

  • Session State
  • Application State

View State

  • It is page-level State Management
  • Used for holding data temporarily
  • Can store any type of data
  • Property dependent

Hidden Fields

  • Contains a small amount of memory
  • Direct functionality access

Cookies

  • Store information temporarily
  • It's just a simple small sized text file
  • Can be changed depending on requirements
  • User Preferred
  • Requires only a few bytes or KBs of space for creating cookies

Query String/Parameter

  • It is generally used for holding values
  • Works temporarily
  • Switches info from one to another page
  • Increase performance
  • Uses real and virtual path values for URL routing

Session State

The Session basically stores the values as a dictionary collection in key/value pairs. It completely utilizes server resources to store the data. It is a secure way of storing data, since the data will never be passed to the client.

For each and every user, a separate Session is created, and each and every Session has its Unique ID. This ID is being stored in the client's machine using cookies. If there are multiple users who are accessing a web application, then for each user a separate Session is created. If a single user logs in and logs out the Session is killed, then if the same user again logs into the application, then a new Session ID is being created for the same user.

The Session has a default timeout value (20 minutes). We can also set the timeout value for a session in the web.config file.


54. What is Data Abstraction?

Data abstraction is the process of hiding certain details and showing only essential information to the user. Abstraction can be achieved with either abstract classes or interfaces

The abstract keyword is used for classes and methods


55. What is Abstract Class?

  • Abstract class is a restricted class that cannot be used to create objects (cannot be instantiated).
  • To access it, it must be inherited from another class).
  • This class must contain at least one abstract method.
  • The Abstract classes are typically used to define a base class in the class hierarchy.
  • Abstract classes are either partially implemented or not implemented at all.
  • You can take advantage of abstract classes to design components and specify some level of common functionality that must be implemented by derived classes.
  • An abstract class can have both abstract and regular methods.

Example

abstract class Animal 

{

  public abstract void animalSound();

  public void sleep() 

  {

    Console.WriteLine("Zzz");

  }

}


Animal myObj = new Animal(); // Will generate an error (Cannot create an instance of the abstract class or interface 'Animal')


56. What is Abstract Method?

  • Abstract method can only be used in an abstract class, and it does not have a body.
  • The body is provided by the derived class (inherited from).


57. What is Interface?

  • An interface is a completely "abstract class", which can only contain abstract methods and properties (with empty bodies)
  • By default, members of an interface are abstract and public.
  • Interfaces can contain properties and methods, but not fields.
  • Interface methods do not have a body - the body is provided by the "implement" class
  • On implementation of an interface, you must override all of its methods
  • An interface cannot contain a constructor (as it cannot be used to create objects)
  • An interface is a group of related methods with empty bodies. 
  • C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces, because the class can implement multiple interfaces. 

Example

interface Animal 

{

  void animalSound(); // interface method (does not have a body)

  void run(); // interface method (does not have a body)

}


58. What is the difference between Abstract class and Interface?

Abstract ClassInterface
It contains both declaration and definition part.It contains only a declaration part.
Multiple inheritance is not achieved by abstract class.Multiple inheritance is achieved by interface.
It contain constructor.It does not contain constructor.
It can contain static members.It does not contain static members.
Abstract class members can have access modifiers.Interface members can’t have access modifiers.
Abstract class members can be private by default which can be change.Interface members can be public by default which can not be change.
Abstract class can inherit from another abstract class or another interface.Interface can inherit from another interface only and cannot inherit from an abstract class.
A class can only use one abstract class.A class can use multiple interface.
If many implementations are of the same kind and use common behavior, then it is superior to use abstract class.If many implementations only share methods, then it is superior to use Interface.
Abstract class can contain methods, fields, constants, etc.Interface can only contain methods .
It can be fully, partially or not implemented.It should be fully implemented.
How to create Abstract Classes and Interfaces?

public abstract class Customer  

{

}

public interface ICustomer  

{  

}


How to create Abstract Classes and Interfaces along with fields? 

public abstract class Customer  

{  

    int i1;  

}  

public interface ICustomer  

{  

    int i2;//Not possible  


How to create Abstract Classes and Interfaces along with members? 

public abstract class Customer  

{  

    public abstract void Print1();  

}  

public interface ICustomer  

{  

    void Print2();  

}


How ismember implementation possible in Abstract Classes and Interfaces? 

public abstract class Customer  

{  

    public void Print1()  

    {  

    }  

}  

public interface ICustomer  

{  

    void Print2()//Not possible  

    {  

    }  

}  


How to implement abstract or interface members in inherited class? 

public abstract class Customer  

{  

    public abstract void Print1();  

}  

public interface ICustomer  

{  

    void Print2();  

}  

public class MainClass : Customer, ICustomer  

{  

    public override void Print1()  

    {  

    }  

    public void Print2()  

    {  

    }  


How to implement inherit from another Abstract Class or Interface to Abstract Class or Interface? 

public abstract class Customer1  

{  

}  

public abstract class Customer2 : Customer1  

{  

}  

public abstract class Customer3 : Customer1, ICustomer1  

{  

}  

public interface ICustomer1  

{  

}  

public interface ICustomer2 : ICustomer1  

{  

}  

public interface ICustomer3 : Customer1 //Not possible  

{