gpt4 book ai didi

java - 同一步骤在多个特征文件中重复但无法使用

转载 作者:行者123 更新时间:2023-12-02 12:11:47 25 4
gpt4 key购买 nike

我正在学习 cucumber 框架,但卡在以下内容,在互联网上搜索但无法得到任何具体的想法。

Feature File 1:

Feature: Facebook title
#Scenario 1
Scenario: Verify the facebook title
Given the user is navigated to FB login page
Then the title should be correct
And close the browser

Feature File 2:

Feature: Google title
Scenario: Verify google title
Given user is navigated to google
Then verify the title
And close the browser

StepDefinitionGoogleTitle.java -code

public class StepDefinitionGoogleTitle extends setupchrome {
static WebDriver driver = setup();
@Given("^user is navigated to google$")
public void navitaetoGoogle() throws Throwable {
driver.get("https://www.google.com");
}
@Then("^verify the title$")
public void verifytheTitle() throws Throwable {
Assert.assertEquals("Title is not as expected","Google",driver.getTitle());
}
@When("^enter something and search$")
public void searchTesxt() throws Throwable {
driver.findElement(By.id("lst-ib")).sendKeys("akash");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.RETURN);
}
@Then("^verify the title after search$")
public void verify_the_title_after_search() throws Throwable {
Assert.assertEquals("Title is not as expected","akash - Google Search",driver.getTitle());
}}

StepDefinitionFacebookTitle.java - code

public class StepDefinitionFacebookTitle extends setupchrome {
WebDriver driver = setup();
@And("^close the browser$")
public void closeTheBrowser() throws Throwable {
driver.close();
}}

setupchrome.java - code

public class setupchrome {
protected static WebDriver driver;
protected static WebDriver setup()
{
if(driver==null)
{
System.setProperty("webdriver.chrome.driver","E:\\D\\Jar FIles\\chromedriver.exe");
driver = new ChromeDriver();
}
return driver;
}}

上述功能文件的步骤定义写在两个单独的文件中。对于常见步骤关闭浏览器我只在一个文件中编写了该方法,就好像我在两个文件中都添加了该方法一样文件它会给出一个编译时错误,指示重复的步骤,但在运行该文件时,我收到以下错误消息:

java.lang.NullPointerExceptionatstepDefinition.StepDefinitionFacebookTitle.closeTheBrowser (StepDefinitionFacebookTitle.java:55)at ?.And close the browser(Gmail.feature:6)

最佳答案

正如 Daniel 上面所说,这取决于您使用的框架。我怀疑您混淆了 PageFactory/PageObjectModel 和步骤定义文件。将页面对象类与步骤文件分开。

如果您也可以粘贴步骤定义文件,以便我们查看,那就太好了。

如果您刚刚开始使用 Cucumber,您也可以按照简单的教程 here 进行操作。

关于java - 同一步骤在多个特征文件中重复但无法使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46485509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com