Friday, February 7, 2020

Cucumber Framework Designing Process-2


When we press Shift & Click on any Statement of feature file it will automatically pointing to  StepDef file Method.
This is Facilitated by Eclipse Cucumber Plugin.


If you don't have Eclipse-Cucumber Plugin then in that case we will not get.

We Can still run TestNG without having TestNG Plugin installed in top of your Eclipse.

Plugin & Eclipse is a Separate thing what we should bother about is a Maven Dependency.


Step:9 In Stepdef Class File There are method which are not implemented yet. We can Identify it by There is Exception in each method as cucumber.api.PendingException();
 While Writing code i am removing that default Content.

Now We will implement our Test Cases.

If I write anything in double quotes of statement of feature file it is considered as parameterised And for it we will implement TC according to it, as see below:

We can see here that we can parameterise it because we can change browser name Firefox or any other browser as well. When we passing any browser TC will be executed on it.
I could run the same Driver Object for different browsers

We are using interface for defining a class that is called Polymorphism.

If some one ask you do you have used polymorphism in you project we can say yes we can use it everyday in driver format. 

WebDriver Driver 
It is taking different forms at run time. like some it become ChromeDriver() some times Firefox. 
It is also an example of up-casting(lower class object to higher class Object e.g. byte to int ).

feature File Statement:

@tag1
  Scenario: Title of your scenario
    Given I open a "chrome" browser

StepDef Class File

TC:
@Given("I open a {string} browser")
public void i_open_a_chrome_browser(String browser) {

if(browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
}else if (browser.equalsIgnoreCase("firefox")) {

//driver = new FireFox();
}

driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
}



Step : 10

Now I want to navigate to url We need to  driver instance everywhere in so declare it as global to that class. 

Actual problem comes when this driver(WebDriver driver) have to be shared in a method kept in Separate class. We need to use Dependency injection and pico-container for it.

For now we declare instance variable at the top(WebDriver driver = null) & mark it as null 
And mark URL as String and mark it as url in method

See below code for it:

feature File Statement:
And I navigate to URL "http://demo.cs-cart.com/" 

StepDef TC: 

@Given("I navigate to URL {string}")
public void i_navigate_to_URL(String url) {
   
driver.get(url);
}


Step 11:We can write all other code in this manner for all other statements.

Step 12: Find out our Project Folder & paste chrome driver exe in it. So no need to set System properties. Normally its bad practice to put the path in system variables. Always good practice not to put it system variables. because it is considered as Hard Coding.

Step 13:

driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);

Good practice to put time unit in miliseconds 

Step 14:
Now We can execute it by using RunTest Class. If DryRun is marked as True it will not Execute. Mark it as false because I don't want to run it as dryRun I want to run it as Actual Run.

Now Run as Right click on RunTest.java -> RunAs-> Junit

Null pointer Exception Occurred because of object which we are invoking is null

what is wrong:

driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
if(browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
}else if (browser.equalsIgnoreCase("firefox")) {
//driver = new FireFox();
}
driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
}


what is Right:

if(browser.equalsIgnoreCase("chrome")) {
driver = new ChromeDriver();
}else if (browser.equalsIgnoreCase("firefox")) {

//driver = new FireFox();
}

driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
}

Step 15:

Make sure we have added url in feature file statement it again hard coded or bad practice to write it here but for now we put here:
And I navigate to URL "http://demo.cs-cart.com/"

Once it is executed Test case will pass it not execute further but it is showing pending exception because we have not implemented other statements.

Step 16:

Refresh our project once and Go to Target Section of Project we can see Cucumber Report Folder, Inside it we will see some files these file comes because In the RunTest Class files Plugin Section I have written plugin="html:target/cucumber-reports",

             So it has automatically create folder as Cucumber reports. Thats how Cucumber generates its reports.

Cucumber can generate HTML report, Json Report, XML reports

Why Json report is important becaose when we run project on Jenkin then jenkin will use that xml file to generate your report.

Also if we want to create your reporter mechanism then you can use that json or html or xml file to Generate the report.

We can open our report from index.html or web browser as well by just copy path & paste it in web bar.

Step 17:

In StepDefs file on the Top I will add specialized method called hooks.
In Cucumber we have only two hooks

1. Before 2. After

So we can write it as once we click on @BeforeMethod you will get two before Select second one which is cucumber.api.java

@BeforeMethod
public void beforeMethodSeUp() {
}


check onc correct api package added or not

Step 18:
Now add Scenario
Scenario is a very specialized interface in cucumber, Which has few methods which we can use to do certain things in the code.
Scenario interface can be use to write the logs, Which can be use to fetch the information of the step as the run time.It can also be use to introduce Screen shots in you reports.

When we write s.    We can see what methods of it. but primary purpose is to write  format is s.write()

Also this Scenario Object we can use only inside as a argument of a Before hook & what cucumber is saying when we run this I will(Cucumber will) instantiate it, create a live object of it and it will inject it  So the cucumber will create runtime object of it as well as inject it in your code. This is again example of native dependency. Similarly we have concept of ITestcontext. It is an Interface instant in TestNG which can be Injected. that is also called as Native Dependency Injection. Here it is done in the form of Scenario Object.

We can use it for writing Test cases and take the Screen shot.

When we execute it :



We can see Report as:



Or we can see it on web browser also with whatever we have printed in it.

public class StepDefs {
WebDriver driver = null;
Scenario scn;
@Before
public void beforeMethodSeUp(Scenario s) {
this.scn =s;
 }


This is how we inject you text in you report.
chrome Browser id opened
User Navigated to urlhttp://demo.cs-cart.com/







No comments:

Post a Comment