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 know what are Hooks and @Before annotation in cucumber but I am not able to understand how do I start my browser and pass that instance to the step definition class.
I have created a sample project to highlight the issue. I have a feature file with 2 scenarios. Those two scenario are go to Bing and Google search pages and verifying the page titles respectively.
Below is my Sample code and project structure:
Project structure:
PageTitleVerification.feature
Feature: Verify the title of the search pages
@regression
Scenario: Validate the Bing Home Page title
Given Go to Bing Home Page
Then Verify Bing page title
@regression
Scenario: Validate the Google Home Page title
Given Go to Google Home Page
Then Verify Google page title
BingHomePage.java
public class BingHomePage {
WebDriver driver;
public BingHomePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void gotoBingHomePage() {
driver.navigate().to("https://www.bing.com/");
}
public boolean verifyBingPageTitle() {
String expectedPageTitle = "Bing";
String actualPageTitle = driver.getTitle();
return actualPageTitle.equals(expectedPageTitle);
}
}
GoogleHomePage.java
public class GoogleHomePage {
WebDriver driver;
public GoogleHomePage(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
public void gotoGoogleHomePage() {
driver.navigate().to("https://www.google.com/");
}
public boolean verifyGooglePageTitle() {
String expectedPageTitle = "Google";
String actualPageTitle = driver.getTitle();
return actualPageTitle.equals(expectedPageTitle);
}
}
BingStepDef.java
public class BingStepDef implements En {
//Question:
//How do I initialize this driver instance with the Hooks @Before
WebDriver driver ;
BingHomePage bingHomePage;
public BingStepDef() {
bingHomePage = new BingHomePage(driver);
Given("Go to Bing Home Page", () -> {
bingHomePage.gotoBingHomePage();
});
Then("Verify Bing page title", () -> {
boolean result = bingHomePage.verifyBingPageTitle();
Assert.assertTrue(result);
});
}
}
GoogleStepDef.java
public class GoogleStepDef implements En {
//Question:
//How do I initialize this driver instance with the Hooks @Before
WebDriver driver ;
GoogleHomePage googleHomePage;
public GoogleStepDef() {
googleHomePage = new GoogleHomePage(driver);
Given("Go to Google Home Page", () -> {
googleHomePage.gotoGoogleHomePage();
});
Then("Verify Google page title", () -> {
boolean result = googleHomePage.verifyGooglePageTitle();
Assert.assertTrue(result);
});
}
}
Hooks.java
public class Hooks {
WebDriver driver;
@Before
public void setup() {
System.setProperty("webdriver.chrome.driver", "./src/main/resources/Drivers/chromedriver.exe");
driver = new ChromeDriver();
}
@After
public void tearDown() {
driver.quit();
}
}
TestRunner.java
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/main/java/com/alok/feature/"
, glue = {"com.alok.stepdef"}
, plugin = {"pretty","html:target/cucumber"}
, dryRun = false
, monochrome = true
, tags = {"@regression"}
)
public class TestRunner {
}
TestRunnerParallel.java
@RunWith(Cucumber.class)
@CucumberOptions(
features = "./src/main/java/com/alok/feature"
, glue = {"com.alok.stepdef"}
, plugin = {"pretty","html:target/cucumber"}
, dryRun = false
, monochrome = true
, tags = {"@regression"}
)
public class TestRunnerParallel extends AbstractTestNGCucumberTests {
@Override
@DataProvider(parallel = true)
public Object[][] scenarios() {
return super.scenarios();
}
}
TestNG.XML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" data-provider-thread-count="2">
<test name="Test Regression" >
<classes>
<class name="com.alok.test.TestRunnerParallel"/>
</classes>
</test>
</suite>
PageFactory.init
? You do not have fields in your page objects except of a driver field. So there is nothing to initialize in your objects.PageFactory.init
. Thanks.