0

I want to get WebDriver Instance (Object) from the Hooks Class in @Before Annotation setup() method. And I want to do it in such a way that I am able to run my scenarios in parallel. I want to run two feature file parallel (login.feature and register.feature) and I am able to open two chrome browser at a time but from them one browser as not doing anything and from second browser run the test case. Can anyone please help me out.

Here is my Login.feature file

Feature: Login to the Application

@RegressionTest
Scenario: Check Login positive cases
Given I go to Home page
When I click on account detail
Then I click on login link
Then I enter valid Email
Then I enter valid Password
Then I click on "<Login>" Button
Then I verify Login Successfully

Here is my Register.feature file

Feature: Register to the Application
@RegressionTest
Scenario: Create a new Account
Given I go to Home page
When I click on account detail
Then I click on create account link
Then I fill all the personal information
  | abc@12345 |
Then I enter a general information
Then I enter billing information
Then I choose taxable option
  | NO |
Then I choose same as billing address
  | YES |
And I click on create account button
And I verify register Successfully
  | Thanks for contacting us! |

here is my DriverFactory.java class:

package utils;
import com.github.javafaker.Faker;
import org.apache.log4j.xml.DOMConfigurator;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.PageFactory;
import org.apache.log4j.Logger;
import pageObjects.*;  
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
public  class DriverFactory {

public static WebDriver driver;
public static BasePage basePage;
public static LoginPage loginPage;
public static RegisterPage registerPage;
public static AddToCartPage addToCart;

public WebDriver getDriver() throws IOException {
    try {
        // Read Config
        Properties p = new Properties();
        FileInputStream fi = new FileInputStream(System.getProperty("user.dir") + "/src/main/java/properties/config.properties");
        p.load(fi);
        String browserName = p.getProperty("browser");

        switch (browserName) {
            case "chrome":
                // code
                if (null == driver) {
                    //   BasicConfigurator.configure();
                    DOMConfigurator.configure("log4j.xml");
                    // PropertyConfigurator.configure("log4j.properties");
                    System.setProperty("webdriver.chrome.driver", Constant.CHROME_DRIVER_DIRECTORY);


                    driver= new ChromeDriver();
                    //driver = DriverFactory.getInstance().getDriver();
                    driver.manage().window().maximize();
                    Log.info("Browser loaded properly");
                }
                break;

        }
    } catch (Exception e) {
        System.out.println("Unable to load browser: " + e.getMessage());
        Log.info("Unable to load browser: " + e.getMessage());
    } finally {
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS);
        basePage = new BasePage();

        loginPage = PageFactory.initElements(driver, LoginPage.class);
        registerPage = PageFactory.initElements(driver, RegisterPage.class);
        addToCart=PageFactory.initElements(driver, AddToCartPage.class);

    }
    return driver;
}
}

Here is my POM.xml file

<plugins>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
            <source>8</source>
            <target>8</target>
        </configuration>
    </plugin>

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.22.0</version>
        <configuration>
            <properties>
                <property>
                    <name>dataproviderthreadcount</name>
                    <value>20</value>
                    <parallel>methods</parallel>
                    <threadCount>4</threadCount>
                </property>
            </properties>

        </configuration>
    </plugin>
   </plugins>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.1.0</version>
<scope>compile</scope>
</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>4.2.3</version>
    <scope>test</scope>

</dependency>

<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.2.3</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>


<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-picocontainer -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-picocontainer</artifactId>
    <version>4.2.3</version>
    <scope>test</scope>
</dependency>


<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>datatable</artifactId>
    <version>1.1.12</version>
</dependency>

Here is my MasterHook. Java file

package stepDefinitions;
import cucumber.api.Scenario; 
import cucumber.api.java.After;
import cucumber.api.java.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.*;
import pageObjects.BasePage;
import utils.DriverFactory;
import java.io.IOException;


public class MasterHooks extends DriverFactory {

@Before
public void setup() throws IOException {

    driver = getDriver();
}

@After
public void tearDownAndScreenshotOnFailure(Scenario scenario) {
    try {
        if (driver != null && !scenario.isFailed()) {
            
           BasePage.captureScreenshot();
            driver.manage().deleteAllCookies();
            driver.quit();
            driver = null;
        }
        if (driver != null && scenario.isFailed()) {
            BasePage.captureScreenshot();
            driver.manage().deleteAllCookies();
            driver.quit();
            driver = null;
        }

        if (driver != null) {
            driver.manage().deleteAllCookies();
            driver.quit();
            driver = null;
        }
    } catch (Exception e) {
        System.out.println("Methods failed: tearDownAndScreenshotOnFailure, Exception: " + 
        e.getMessage());
        Log.error("Methods failed: tearDownAndScreenshotOnFailure, Exception: " + e.getMessage());
    }
}
}

MainRunnerTest.java

package runners;
import cucumber.api.testng.AbstractTestNGCucumberTests;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.testng.annotations.DataProvider;

@RunWith(Cucumber.class)

@CucumberOptions(features = {"src/test/java/features/"},

    glue = {"stepDefinitions"},

    monochrome = true, tags = {"@RegressionTest"}
)

public class MainRunnerTest extends AbstractTestNGCucumberTests {

@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
    return super.scenarios();
}
}

0 Answers0