Tuesday, 31 July 2018

SE - 29 - WebDriver Timeouts

We have already heard about thread.sleep and other explicit waits but there are few waits which are provided by WebDrivers too :

1) implicitlyWait : Specifies the amount of time the driver should wait when searching for an element if it is not immediately present. It works on DOM level. When searching for a single element, the driver should poll the page until the element has been found, or this timeout expires before throwing a NoSuchElementException. When searching for multiple elements, the driver should poll the page until at least one element has been found or this timeout has expired.
Increasing the implicit wait timeout should be used judiciously as it will have an adverse effect on test run time, especially when used with slower location strategies like XPath.

2) setScriptTimeout : Sets the amount of time to wait for a page load to complete before throwing an error. Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. If the timeout is negative, then the script will be allowed to run indefinitely.

3) pageLoadTimeout : Sets the amount of time to wait for an asynchronous script to finish execution before throwing an error. Sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.


WebDriver driver = new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);



Sunday, 29 July 2018

SE - 28 - TestNG Parameterization through @Parameters Annotation

There are few important points to keep in mind for Parameterization through @Parameter annotation in TestNG:

Consider the following example :
Program :
package Package1;

import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Pack1class1 {

 @Test
 @Parameters({"Parameter1","Parameter2"})
 public void P1c1M1(String Parameter1 ,String Parameter2) {
  System.out.println("Package1_class1_Method1");
  System.out.println(Parameter1);
  System.out.println(Parameter2);
 }
 
}
testng.xml :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <parameter name="Parameter1" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm2"></parameter>

  <test thread-count="5" name="Test">
      <classes>
      <class name="Package1.Pack1class1"/>
     </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
-> We can give only one parameter value for one type of parameter. If we want to have multiple values then we can use data-providers. In the above example we have defined 2 parameters Parameter1 and Parameter2 with single value. The purpose of these parameters are just to give the value from testng.xml.


-> If we try to give more values by giving comma then script will consider it as one single string:
 <parameter name="Parameter1" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm2 , TestParam3"></parameter>
In this example we have tried to give two values in parameters , but TestNg will consider it as a single parameter and output will be TestParm2 , TestParam3
There is no way to give 2 parameter values.


-> Now in another try if we  give different values by creating new line in testng.xml fil as below :
 <parameter name="Parameter1" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm3"></parameter>
Here we can see that we are trying to give 2 different values to Parameter2 but while execution only the last value will be considered and output will be TestParm3


-> Through testng.xml file the parameters can only be given at Suite and test level.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <parameter name="Parameter1" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm1"></parameter>
  
  <test thread-count="5" name="Test">
    <parameter name="Parameter3" value="TestParm1"></parameter>
      <classes>
      <class name="Package1.Pack1class1"/>
     </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->


->In case if the parameter name is same in suite level and test level then test level parameter will get preference over suite level. So, in that case, all the classes inside that test level will share the overridden parameter, and other classes which are outside the test level will share suite level parameter.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <parameter name="Parameter1" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm1"></parameter>
  
  <test thread-count="5" name="Test">
    <parameter name="Parameter2" value="TestParm2"></parameter>
      <classes>
      <class name="Package1.Pack1class1"/>
     </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
So here the TesgNG will pick the value of the Parameter2 from the Test level that is TestParm2. The value at suite level will be ignored. The output of the above program will be :

Package1_class1_Method1
TestParm1
TestParm2


-> If the parameter has no value in testng.xml then we can give @Optional annotation and define he default value.
(i) Program with @Optional value but we have Parameters present in Testng.xml
package Package1;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Pack1class1 {
 @Test
 @Parameters({"Parameter1","Parameter2"})
 public void P1c1M1(@Optional ("ABC") String Parameter1 ,@Optional("XYZ")String Parameter2) {
  System.out.println("Package1_class1_Method1");
  System.out.println(Parameter1);
  System.out.println(Parameter2);
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <parameter name="Parameter1" value="TestParm1"></parameter>
  <parameter name="Parameter2" value="TestParm1"></parameter>
    <test thread-count="5" name="Test">
      <classes>
      <class name="Package1.Pack1class1"/>
     </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
Since we have valid values in testng.xml for the given parameters so output of above program will be :
Package1_class1_Method1
TestParm1
TestParm1

(ii) Program with @Optional value but we don't have Parameters present in Testng.xml
package Package1;
import org.testng.annotations.Optional;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class Pack1class1 {
 @Test
 @Parameters({"Parameter1","Parameter2"})
 public void P1c1M1(@Optional ("ABC") String Parameter1 ,@Optional("XYZ")String Parameter2) {
  System.out.println("Package1_class1_Method1");
  System.out.println(Parameter1);
  System.out.println(Parameter2);
 }
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
 <parameter name="Parameter1" value="TestParm1"></parameter>
    <test thread-count="5" name="Test">
      <classes>
      <class name="Package1.Pack1class1"/>
     </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->
Here we can see that there is just one parameter in testng.xml but we are passing 2 parameters in our program. In this case the optional value will be called for second parameter, Output will be :

Package1_class1_Method1
TestParm1
XYZ

-> Now there can be a situation when the parameter type in xml is string and in method we have given int.  It means there is type mismatch in the parameter type. In this case we get the following exception:

[Utils] [ERROR] [Error] java.lang.NumberFormatException: For input string: &quot;ABC&quot;

Monday, 23 July 2018

SE - 27 - Comparator and Comparable


Comparable :
i) Interface that needs to be implemented:  java.lang.Comparable
ii) A comparable object compares its own instance with other class object instance
iii) It has only one method , Syntax : int obj1.compareTo(obj2)
iv) Returns : Negative , if obj1< obj2
                     Zero ,  if obj1 = obj2
                     Positive ,  if obj1 > obj2
v) Default natural sorting order
vi) All wrapper and string classes implements comparable


Comparator:
i) Interface that needs to be implemented:  java.util.Comparator
ii) An object of comparator compares objects of two different class instances
iii) It contains 2 methods :
      Syntax : int compare(obj1, obj2) , Int equals(obj1 , obj2)
iv) Returns : Negative , if obj1< obj2
                     Zero ,  if obj1 = obj2
                     Positive ,  if obj1 > obj2
v) Customised sorting order
vi) Collator and ruleBased collator implements comparator.

Saturday, 21 July 2018

SE - 26 - Static Block

Static Block is used for static initialisation of class.

i) The code written in static block runs only once whenever a object of class is created for the first time .

ii) The code written in static class can even execute when we call a static variable of that class but it will be called only once.

iii) There can be multiple static blocks in a class. They will run in the sequence its defined. All of them will run only once in either of above two conditions.

class Test{
   static int num;
   static{
      System.out.println("Static Block  : 1");
  } 

  static{
      System.out.println("Static Block :  2");
  }


iv) If there are multiple static block with the different value of the static variable then the value will be overrided and the last value will be set. (Since the static members are shared across the project)

v) If we miss to write the word static while defining static block then the piece of code will be considered as constructor block and will be called.

vi) Super , this keywords are not supported in static block and checked exceptions are also not allowed



SE - 25 - Benefits of BDD(Cucumber)

Here are few aspects and benefits of using cucumber:

i) In BDD the end-to-end business scenarios(user stories) are automated

ii) Behaviour of application is focused rather than unit-testcases

iii) It combines business language with testing

iv)  Cucumber has 3-step separation of feature . step and world. It means code reusability with a great extent is supported. Let's see how :

When we write a feature file we write the gherkin scenario as :

Given :
When
Then :

When writing multiple scenarios in a feature few of the steps may be same. If  "Given" condition is same in multiple features we can use the same definition for multiple scenarios.

There is one more advantage, if any code is wrong we just need to modify that particular step only. If "Given" or "Then" or "When" is wrong we just need to modify that step only. 

Friday, 20 July 2018

SE - 24 - Default constructor in Java

Default constructor is automatically generated in JAVA when there are no constructors present or defined by user. Few important things about default constructor :

i) It is only created automatically by java if there are no defined constructors in the class

ii) It has no arguments or body

iii) It initialises the member data value with the default values. It initializes only the uninitilized variables. : Eg :

class DefaultConstTest {
   int i;
   DefaultConstTest t;
   boolean b;
   byte bt;
   float ft;
}
Since there are no constructors here so java will automatically run the default constructor and assign default value to its members :

Here the Value assigned will  be:

0
null
false
0
0.0

iv) If we say how the default constructor looks like , then simply it's a constructor with no arguments and body. It will look like :

DefaultConstTest() {}

v) If we create our own constructor then java will not create any default constructor

vi) The no-arg constructor and default constructor are not same. The no-arg constructor is defined by user while default constructor is created by JAVA

Wednesday, 18 July 2018

SE - 23 - Nested class in Java

Java supports two type of nesting :

1) Static nested class
2) inner nested class

Lets take a code example :

Here we have created a inner nested class and static nested class
public class mainclass {
 //inner nested class
 public class subclass{
  //method inside inner nested class
  public void method1() {
    
    System.out.println("inner nested class");
   }
  }
 //static nested class
 public static class substaticclass{
  //method inside static nested class
  public void method2() {
   
   System.out.println("nested static class");
  }
 } 
}

In the below code we are calling the methods created in the inner nested class and static nested class in above code :
public class classCall {

 @Test
 public void callm() {
  //calling a inner class
  mainclass x = new mainclass();
  mainclass.subclass z = x.new subclass();
  z.method1();
  
  //calling a static class
  mainclass.substaticclass c = new mainclass.substaticclass();
  c.method2();
 }
}

Output:
inner nested class
nested static class

So here we can see clearly that the members of static classes can be called directly by class name. For the inner class object we need to create reference of the outer class then call the members of the inner class.