Wednesday, 15 May 2024

Best practice for create selenium Automation test

Creating robust and flexible Selenium automation tests involves following best practices for test design, implementation, and maintenance. Here are some steps to achieve this:

1. **Identify clear test objectives**: Clearly define the purpose and scope of each test case to ensure that it aligns with the overall testing goals.

2. **Use descriptive and meaningful test names**: Give each test case a descriptive and meaningful name that reflects its purpose and expected behavior. This makes it easier to understand the test's intent and results.

3. **Modularize test code**: Divide your test code into small, reusable modules or functions that focus on specific actions or scenarios. This promotes code reuse, simplifies maintenance, and enhances readability.

4. **Implement robust error handling**: Anticipate and handle potential exceptions or errors gracefully in your test code. Use try-catch blocks or assertion methods to verify expected outcomes and handle unexpected conditions effectively.

5. **Parameterize test data**: Parameterize test data to make tests more flexible and adaptable to different scenarios. Use data-driven techniques to separate test logic from test data, allowing for easier maintenance and scalability.

6. **Use explicit waits**: Use explicit waits to ensure that tests wait for specific conditions to be met before proceeding. This helps avoid timing issues and makes tests more reliable, especially when dealing with dynamic web elements or network latency.

7. **Create meaningful assertions**: Write meaningful assertions that verify the expected behavior of the application under test. Use assertions to validate page content, element properties, or application state to ensure that tests accurately reflect user expectations.

8. **Implement page object model (POM)**: Organize your test code using the page object model, which encapsulates web page elements and their interactions into reusable classes. This improves code maintainability, readability, and scalability.

9. **Implement cross-browser testing**: Test your application across different web browsers and versions to ensure compatibility and consistency. Use Selenium's capabilities to automate tests on multiple browsers, and consider using cloud-based testing platforms for broader coverage.

10. **Continuous integration and testing**: Integrate Selenium tests into your continuous integration (CI) pipeline to automate test execution and ensure timely feedback on code changes. This helps catch bugs early and facilitates faster release cycles.

11. **Monitor and maintain tests**: Regularly review and update your Selenium tests to keep pace with changes in the application under test. Monitor test results for failures or regressions, and prioritize fixing flaky tests to maintain test reliability.

By following these best practices, you can create Selenium automation tests that are robust, flexible, maintainable, and provide reliable feedback on the quality of your web applications.

Wednesday, 8 November 2023

Java Stream Max

1) Find Maximum number in Array list

import java.util.ArrayList;
import java.util.List;

public class javaStreamExamples {
    List<Integer> list1=Arrays.asList(10,20,35,20,67,10)

    public void testStream(){
        Int max1=list1.stream().max(Integer:: compare).get();

System.out.println(max1);

    }

}

O/p
67


≠=================================

1) Find Minimum number in Array list

import java.util.ArrayList;
import java.util.List;

public class javaStreamExamples {
    List<Integer> list1=Arrays.asList(10,20,35,20,67,10)

    public void testStream(){
        Int min1=list1.stream().max(Comparator.reverseOrder()).get();

System.out.println(min1);

    }

}

O/p
67


≠=================================


Wednesday, 13 May 2020

Lambda Expression in JAVA



-> The Lambda expression is used to provide the implementation of an interface which has functional interface.

->  It saves a lot of code.

-> In case of lambda expression, we don't need to define the method again for providing the implementation. Here, we just write the implementation code.

-> Java lambda expression is treated as a function, so compiler does not create .class file.



Functional Interface

-> Lambda expression provides implementation of functional interface.

-> An interface which has only one abstract method is called functional interface.

-> Java provides an anotation @FunctionalInterface, which is used to declare an interface as functional interface.


Syntax

(argument-list) -> {body}


Java lambda expression is consisted of three components.

1) Argument-list: It can be empty or non-empty as well.

2) Arrow-token: It is used to link arguments-list and body of expression.

3) Body: It contains expressions and statements for lambda expression.



Example :  Addition of two numbers using lamda expression

@FunctionalInterface
interface Calc{

   int add(int n1,int n2);

}




public class Calculation{
 
 
    public static void main(String[] args) { 

     
       Calc addition=(n1,n2)->{

          return n1+n2;
       };
   
 
       System.out.println("Addition "+addition.add(10,20)); 


       
 }


}


Output

Addition 30

Comparator Interface in java


-> Comparator used to sorting the object element by using multiple sorting sequence.

-> We can sort the elements on the basis of any data member, for example, rollno, name, age or anything else.

-> The comparator interface provide two methods like compare(Object obj1,Object obj2) and equals(Object obj).



1) compare(Object obj1,Object obj2) function

Its compares first object(obj1) and second object(obj2) and return int value

Syntax : public int compare(Object obj1,Object obj2)


positive integer = if the current object is greater than the specified object.
negative integer = if the current object is less than the specified object.
zero = if the current object is equal to the specified object.



2) equals(Object obj1) function

Its used to compare current object with specified object

Syntax : public boolean equals(Object obj1)




Example : Sorting by name and Emp id by using comparator

import java.util.*; 
import java.io.*;

// User - Defined Class

class Employee { 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 
} 
}


class sortByAge implements Comparator{
                         
public int compare(Object o1,Object o2){ 
Employee s1=(Employee)o1; 
Employee s2=(Employee)o2; 

if(s1.empage==s2.empage) 
return 0; 
else if(s1.empage>s2.empage) 
return 1; 
else 
return -1; 
} 
   
}
 

class sortByName implements Comperator{
   
public int compare(Object o1,Object o2){ 
Employee s1=(Employee)o1; 
Employee s2=(Employee)o2; 

return s1.empname.compareTo(s2.empname);
   
}   
}


// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
 
System.out.println("Sorting By Age"); 
Collections.sort(al, new sortByAge()); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 
}


System.out.println("Sorting By Name");
Collections.sort(al, new sortByName()); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 
} 

} 
} 


Output

Sorting By Age

4 Surya 21
1 Ajith 23
6 Vijay 27


Sorting By Name

1 Ajith 23
4 Surya 21
6 Vijay 27

Comparable interface in java



-> Comparable is an interface of comparing the objects with other objects os the same type. This is also called "natural ordering"

-> by using compare interface, we can sort the elements based on single data member only.

-> It provides single method name as compareTo(object) and provides single sorting sequence only.

-> The interface found in java.lang.package


compareTo(Object obj) method

The method used to compare the current object with specified object. Please find the below return values

positive integer = if the current object is greater than the specified object.
negative integer = if the current object is less than the specified object.
zero = if the current object is equal to the specified object.



Example 1: Sort by Integer Value (Ascending Order) by using Comparable

import java.util.*; 

// User - Defined Class

class Employee implements Comparable<Employee>{ 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 
} 
 
public int compareTo(Employee st){ 
if(empage==st.empage) 
return 0; 
else if(empage>st.empage) 
return 1; 
else 
return -1; 
} 
}



// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
Collections.sort(al); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 
} 
} 
} 




Output

4 Surya 21
1 Ajith 23
6 Vijay 27


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


Example 2: Sort by Integer Value (Decending Order) by using Comparable


import java.util.*; 

// User - Defined Class

class Employee implements Comparable<Employee>{ 
int empid; 
String empname; 
int empage; 
Employee(int empid,String empname,int empage){ 
this.empid=empid; 
this.empname=empname; 
this.empage=empage; 
} 
 
public int compareTo(Employee st){ 
if(empage==st.empage) 
return 0; 
else if(empage<st.empage) 
return 1; 
else 
return -1; 
} 
}



// Main Class



public class TestSort1{ 
public static void main(String args[]){ 
ArrayList<Employee> al=new ArrayList<Employee>(); 
al.add(new Employee(1,"Ajith",23)); 
al.add(new Employee(6,"Vijay",27)); 
al.add(new Employee(4,"Surya",21)); 
 
Collections.sort(al); 
for(Employee st:al){ 
System.out.println(st.empid+" "+st.empname+" "+st.empage); 
} 
} 
} 




Output


6 Vijay 27
1 Ajith 23
4 Surya 21


Monday, 30 March 2020

Difference between Web driver, Remote Web Driver and Chrome Driver


Webdriver

Web driver is interface.
Global for all browsers
The interface provide common methods signature for all browser.
Ex : findElement(), get(),switchTo()

Remote Webdriver

Remote Webdriver is concrete class  implements Webdriver.
The implementation of webdriver method is available  in Remote Webdriver Class.
The Class provide additional  method  which is not available in webdriver. Provide method to run selenium test in remote machine.
ex : getSessionID()


Chrome driver

Chrome Driver is class extends Remote Webdriver.
Only specific to Chrome browser.
The class have implementation for only chrome browser. 

Difference between String, String Builder and String Buffer

Immutable : Cannot change value
Mutable : Can change the Value


String :

 String is Immutable.
 If we update any existing String then it will create String object. It won't override existing value.

Ex : 
String S1="Testing"
print (S1)

String concat(String s1){
S1 = S1+"Java"
}

O/P

S1: Testing


String Builder

String Builder is mutable.
we can change value. if any thing update then it will override existing object itself.

Ex : 
StringBuilder S1="Testing"
print (S1)

String concat(String s1){
S1 = S1.append("Java")
}

O/P

S1: Testing Java


String Buffer

Both String Buffer and String builder same. Except one concept.
String Buffer is thread safe. we can refer the String buffer object via multi thread.

String Buffer is mutable. we can change value. if any thing update then it will override existing object itself.

Ex : 
StringBuffer S1="Testing"
print (S1)

String concat(String s1){
S1 = S1.append("Java")
}

O/P

S1 : Testing Java





When to use

String : if String object value contant entire program.
String Builder : If String object value need to change at any time.
String Buffer : If String object value need to change at any time. It can be access same value via multithread.