Reputation: 77
I wrote the following feature step to take screen shot after failed scenario and close the browser. On the Step Defs method when I use (Scenario scenario) parameter and Run it But it generates an error message.
Following Feature Step I wrote:
And I close the browser
Following is the code I have written in Step Definition file for that step:
@And("^I close the broswer$")
public void i_Close_The_Broswer(Scenario scenario ) throws Exception {
if (scenario.isFailed()) {
byte[] screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
Error Message displays after run:
cucumber.runtime.CucumberException: Arity mismatch: Step Definition
'stepDefinations.LoginPageStepDefs.i_Close_The_Broswer(Scenario) in
file:/C:/Users/thaider/eclipse-workspace/AcWs_Automation/target/test-
classes/' with pattern [^I close the broswer$] is declared with 1
parameters. However, the gherkin step has 0 arguments [].
Test Base Class:
package utilities;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.Scenario;
import cucumber.api.java.After;
import cucumber.api.java.Before;
public class TestBase {
public static WebDriver driver;
public static Properties prop;
public TestBase() {
try {
prop = new Properties();
FileInputStream fis = new FileInputStream(
"C:/Users/thaider/eclipse-workspace/AcWs_Automation/src/test/java/config/config.properties");
prop.load(fis);
} catch (IOException e) {
e.getMessage();
}
}
public static void initialization() {
String browserName = prop.getProperty("browser");
if(browserName.equals("chrome")){
//System.setProperty("webdriver.chrome.driver", "src/main/resources/Driver/chromedriver.exe");
System.setProperty("webdriver.chrome.driver", prop.getProperty("webdriver.chrome.driver"));
// System.setProperty("log4j.configurationFile", prop.getProperty("log4j2ConfigFile"));
driver = new ChromeDriver();
}
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);
driver.get(prop.getProperty("url"));
}
public static void closeSession() {
driver.quit();
}
}
Login Page Step Definition File:
package stepDefinations;
import java.awt.Window;
import java.io.File;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriverException;
import cucumber.api.PendingException;
import cucumber.api.Scenario;
import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import pageObject_OR.AcceptContinue;
import pageObject_OR.LoginPage;
import utilities.TestBase;
import utilities.WindowsHandle;
import org.openqa.selenium.WebElement;
public class LoginPageStepDefs extends TestBase {
AcceptContinue acceptPage;
LoginPage loginPage;
WindowsHandle windowHandle;
@Given("^I want to open a browser$")
public void i_want_to_open_a_browser() throws Exception{
TestBase.initialization();
}
@Then("^I click on Accept & Continue$")
public void i_click_on_Accept_Continue() throws Exception{
acceptPage = new AcceptContinue();
acceptPage.clickAcceptContinue();
}
@Then("^I validate seal logo is displayed$")
public void i_validate_seal_logo_is_displayed() throws Exception {
loginPage = new LoginPage();
Thread.sleep(3000);
loginPage.validateCrmImage();
}
@Then("^I am in the ACWS login page as a CS User$")
public void i_am_in_the_ACWS_login_page_as_a_CS_User() throws Exception {
// Write code here that turns the phrase above into concrete actions
loginPage = new LoginPage();
loginPage.loginToHomePage("Testcs", "test123");
}
@And("^I close the broswer$")
public void i_Close_The_Broswer(Scenario scenario ) throws Exception {
if (scenario.isFailed()) {
byte[] screenshot = ((TakesScreenshot)
driver).getScreenshotAs(OutputType.BYTES);
scenario.embed(screenshot, "image/png");
}
TestBase.closeSession();
}
pom.xml File:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>AcWs_Automation</groupId>
<artifactId>AcWs_Automation</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- REPORTING DEPENDENCIES ARE BELOW -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<testFailureIgnore>true </testFailureIgnore>
</configuration>
</plugin>
<plugin>
<groupId>net.masterthought</groupId>
<artifactId>maven-cucumber-reporting</artifactId>
<version>3.15.0</version>
<executions>
<execution>
<id>execution</id>
<phase>verify</phase>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<projectName>ExecuteAutomation</projectName>
<outputDirectory>${project.build.directory}/cucumber-report-html</outputDirectory>
<cucumberOutput>${project.build.directory}/cucumber.json</cucumberOutput>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
<!-- Project Selenium, Junit, Apache POI Dependecy Below -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version> 3.12.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.directory.studio</groupId>
<artifactId>org.apache.commons.io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.16-beta2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>openxml4j</artifactId>
<version>1.0-beta</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core
<dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId>
<version>2.11.2</version> </dependency> -->
<!-- BELOW DEPENDECIES HAS BEEN COMMENTED OUT FOR DUPLIATION -->
<!-- <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId>
<version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId> <version>3.9</version> </dependency>
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId>
<version>3.9</version> </dependency> <dependency> <groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId> <version>1.1</version> </dependency>
<dependency> <groupId>org.apache.poi</groupId> <artifactId>openxml4j</artifactId>
<version>1.0-beta</version> </dependency> -->
</dependencies>
Project Structure and Feature File
Upvotes: 2
Views: 12408
Reputation: 5034
From the Cucumber API for Scenario (https://github.com/cucumber/cucumber-jvm/blob/master/core/src/main/java/cucumber/api/Scenario.java) :
Before or After Hooks that declare a parameter of this type will receive an instance of this class
Note that it says only the Before and After hooks - not the Step definitions @And, @Given, @When, @Then. Those expect only the arguments specified in the line expression.
Upvotes: 2