2018-11-24

serenity cucumber selenium automation

BDD fundamentals

Behaviour-Driven Development (BDD) is a collaborative approach to software development that bridges the communication gap between business and IT. BDD helps teams communicate requirements with more precision, discover defects early and produce software that remains maintainable over time.

Writing acceptance criteria with Cucumber-JVM

In Cucumber, you express acceptance criteria in a natural, human-readable form.
For example, you want to write acceptance criteria for sending a mail
you could write like this:

Given I want to send a mail
When I login to to mail box. clicked compose button, entered the details and clicked send
Then I should see mail sent to the receiver.

Sometimes tables can be used to summarize several different examples of the same scenario.

Scenario Outline: sending a mail
    Given I have login to the mail box
    When I can enter the details '<Email>' and '<Subject>'
    Then I should see mail sent successfully
  Examples:
    | Email    | Subject  |
    | x@x.com  | test     |
    | x@y.com  | test     |
    | x@z.com  | test     |

These feature files can be placed in different locations, but you can reduce the amount of configuration you need to do with Serenity if you put them in the src/test/resources/features directory.

|----src
| |----test
| | |----resources
| | | |----features
| | | | |----mail
| | | | | |----sending_mail.feature

The Scenario Runner

package net.serenity_bdd.samples.etsy.features;

import cucumber.api.CucumberOptions;
import net.serenitybdd.cucumber.CucumberWithSerenity;
import org.junit.runner.RunWith;

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(features="src/test/resources/features")
public class AcceptanceTests {}

Step definitions

public class SendingMail {
    @Steps
    BuyerSteps buyer;

    @Given("I have login to the mail box")
    public void buyerWantsToBuy(String article) {
        buyer.opens_etsy_home_page();
    }

    @When("I can enter the details '(.*)' and '(.*)'")
    public void searchByKeyword(String keyword) {
        buyer.searches_for_items_containing(keyword);
    }

    @Then("I should see mail sent successfully")    public void resultsForACategoryAndKeywordInARegion(String keyword) {
        buyer.should_see_items_related_to(keyword);
    }
}



cucumber selenium automation for ag-Grid example

How to select row in xpath?


2018-11-02

What is addShutdownHook method in java?


  • addShutdownHook method registers a new virtual-machine shutdown hook.
  • A shutdown hook is a initialized but unstarted thread. 
  • When JVM starts its shutdown it will start all registered shutdown hooks in some unspecified order and let them run concurrently. 


When JVM (Java virtual machine)  shuts down >
When the last non-daemon thread finishes, or when the System.exit is called.

Once JVM’s shutdown has begun new shutdown hook cannot be registered neither  previously-registered hook can be de-registered. Any attempt made to do any of these operations causes an IllegalStateException.

Example:

public class AddShutDownHookTest extends Thread{
   public static void main(String[] args) throws InterruptedException {   
          System.out.println("main thread started");
         
          Runtime.getRuntime().addShutdownHook(new Thread(){
       public void run() {
          try{
                System.out.println("executing shutdown hook");
          }catch (Exception e){
                e.printStackTrace();
          }
          System.out.println("shutdown hook executed successfully");
       }
     });
         
          Thread.sleep(4000); //Optional delay
          System.out.println("main thread ended");
   }
}
/*OUTPUT
main thread started
main thread ended
executing shutdown hook
shutdown hook executed successfully
*/

What is preemptive scheduling and time slicing in java

In preemptive scheduling, the highest priority thread executes until it enters into the waiting or dead state.
In time slicing, a thread executes for a certain predefined time and then enters runnable pool. Than thread can enter running state when selected by thread scheduler.

2018-11-01

When to use ArrayList and when to use LinkedList in application?

ArrayList has constant time search operation O(1) .Hence, ArrayList is preferred when there are more get() or search operation .

Insertion , Deletion operations take constant time O(1) for LinkedList. Hence, LinkedList is preferred when there are more insertions or deletions involved in the application.

Lombok "val" example

"val" can use as the type of a local variable declaration instead of actually writing the type.

Java With out Lombok

public class ValExample {
  public void example() {
    ArrayList<String> example = new ArrayList<String>();
    example.add("Hello, World!");
    String foo = example.get(0);
  }
}

Java With Lombok

import java.util.ArrayList;
import java.util.HashMap;
import lombok.val;

public class ValExample {
  public void example() {
    val example = new ArrayList<String>();
    example.add("Hello, World!");
    val foo = example.get(0);
  }
}

Lombok @Builder example

The @Builder annotation produces complex builder APIs for your classes.

Java With Out Lombok


public class BuilderExample {
  private String name;
  private int age;
 
  BuilderExample(String name, int age) {
    this.name = name;
    this.age = age;
    this.occupations = occupations;
  }
  //setter and getter
}

With Lombok


import lombok.Builder;
import lombok.Singular;

@Builder
public class BuilderExample {
  private String name;
  private int age;
}

Lombok with java 8 example

BuilderExample.builder().name("Test").age(20).build();


Project Lombok Example

Lombok features

The Lombok javadoc is available, but we advise these pages.

val

Finally! Hassle-free final local variables.

var

Mutably! Hassle-free local variables.

@NonNull

or: How I learned to stop worrying and love the NullPointerException.

@Cleanup

Automatic resource management: Call your close() methods safely with no hassle.

Project lombok Installation in IDE

IntelliJ IDEA

The Jetbrains IntelliJ IDEA editor is compatible with lombok.
Add the Lombok IntelliJ plugin to add lombok support for IntelliJ:
  • Go to File > Settings > Plugins
  • Click on Browse repositories...
  • Search for Lombok Plugin
  • Click on Install plugin
  • Restart IntelliJ IDEA



Eclipse






Project Lombok dependencies



Lombok Gradle dependencies

lombok {
 version = '1.18.4'
 sha256 = ""
}


Maven Dependencies

<dependencies>
 <dependency>
  <groupId>org.projectlombok</groupId>
  <artifactId>lombok</artifactId>
  <version>1.18.4</version>
  <scope>provided</scope>
 </dependency>
</dependencies>