My Feature file -
Feature: Open google page
@Sanity
Scenario: Open google search
Given User open google search
Then enter text "hi" in search bar
And close the browser
###########################################################################
Page Object class -
package pageObjects;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class GoogleObjects {
public WebDriver ldriver;
public GoogleObjects(WebDriver rdriver) {
ldriver = rdriver;
PageFactory.initElements(rdriver, this);
}
@FindBy(name = "q")
@CacheLookup
WebElement search;
public void Search(String searchTxt) {
search.sendKeys(searchTxt);
search.sendKeys(Keys.DOWN, Keys.ENTER);
}
public void btnClick() {
ldriver.close();
}
}
############################################################################
StepDifinition class -
package stepDefinitions;
import org.openqa.selenium.WebDriver;
import io.cucumber.java.en.*;
import pageObjects.GoogleObjects;
public class GoogleSearch {
public WebDriver driver;
@Given("^User open google search$")
public void user_open_google_search() throws Throwable {
driver.get("https://www.google.com");
}
@Then("^enter text "([^"]*)" in search bar$")
public void enter_text_something_in_search_bar(String TxT) throws Throwable {
GoogleObjects gb = new GoogleObjects(driver);
gb.Search(TxT);
}
@And("^close the browser$")
public void close_the_browser() throws Throwable {
GoogleObjects gb = new GoogleObjects(driver);
gb.close();
}
}
##########################################################################
Test Runner class -
package runner;
import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
@RunWith(Cucumber.class)
@CucumberOptions(
features = {".//Features/google.feature"},
glue = {"stepDefinitions"},
monochrome=true,
tags = "@Sanity",
plugin = {"pretty","html:target/reports.html" ,
"json:target/reports.json"}
)
public class testRunner {
}
######################################################################
hooks class -
package stepDefinitions;
import java.io.IOException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import io.cucumber.java.After;
import io.cucumber.java.Before;
public class hooks {
WebDriver driver;
@Before
public WebDriver setUp() throws IOException {
System.setProperty("webdriver.chrome.driver","*:\****\***\Drivers\chromedriver.exe");
driver = new ChromeDriver();
return driver;
}
@After
public void tearDown() {
driver.quit();
}
}
###########################################################我用cucumber框架创建了一个Maven项目,并编写了一个特性文件定义、测试运行器和钩子文件。当我运行测试程序时,当我导入io.cucumber.java. invalidmethodsignatureexception时,我得到了"io.cucumber.java. invalidmethodsignatureexception"。之前/之后,但当我从junit导入时,我的测试正在运行,但是@before和@after在黄瓜中不起作用,如果我们从junit导入。我不明白为什么我得到InvalidMethodSignatureException。我尝试为@Before注释提供order=0。它不起作用以上是代码,请帮助我。
你会得到这个问题,因为用io.cucumber.java.Before
注释的方法必须是void
。
在您的示例中,它返回WebDriver
。这就是你得到InvalidMethodSignatureException
的原因。