Java 缺少返回语句,如何修复?

Java 缺少返回语句,如何修复?,java,selenium,intellij-idea,webautomation,Java,Selenium,Intellij Idea,Webautomation,目标:我有一个名为“InvokeChromeTest”的类,我正在扩展到另一个名为“Base”的类来访问chromedriver和data.properties。运行代码时,我得到错误信息: 错误:(22,3)java:缺少返回语句 我不确定如何解决这个问题。下面是我的示例代码。请让我知道我能做些什么来修复 src/test/java/loginPage/InvokeChromeTest package loginPage; import credentials.ProfileCredenti

目标:我有一个名为“InvokeChromeTest”的类,我正在扩展到另一个名为“Base”的类来访问chromedriver和data.properties。运行代码时,我得到错误信息:

错误:(22,3)java:缺少返回语句

我不确定如何解决这个问题。下面是我的示例代码。请让我知道我能做些什么来修复

src/test/java/loginPage/InvokeChromeTest

package loginPage;

import credentials.ProfileCredentials;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import resources.Base;

public class InvokeChromeTest extends Base {

  @Test

  public WebDriver initializeDriver() {
    driver = initializeDriver();
    driver.get(dataProperties.getProperty("url"));
    ProfileCredentials p = new ProfileCredentials(driver);
    p.getAuthorize().click();
    p.getApiKey().sendKeys("testKey");
    p.getAuthCred().click();
    p.getCloseAuth().click();

  }

  @AfterTest
  public void teardown() {
    driver.close();
  }

}
src/main/java/resources/Base

package resources;

import io.github.bonigarcia.wdm.WebDriverManager;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Base {

  public WebDriver driver;
  protected Properties dataProperties;

  public WebDriver initializeDriver() {
    // Create global property file
    dataProperties = new Properties();
    InputStream dataPropertiesInputStream = null;
    try{
      dataPropertiesInputStream = getClass().getClassLoader().getResourceAsStream("data.properties");
      dataProperties.load(dataPropertiesInputStream);
    } catch (IOException e) {
      e.printStackTrace();
    }
    String browserName = dataProperties.getProperty("browser");
    System.out.println(browserName);

    if (browserName.equals("chrome")) {
      WebDriverManager.chromedriver().setup();
      driver = new ChromeDriver();
    } else if (browserName.equals("firefox")) {
      WebDriverManager.firefoxdriver().setup();
      driver = new FirefoxDriver();
    }
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    return driver;
  }

}
src/main/java/credentials/ProfileCredentials

package credentials;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class ProfileCredentials {

  WebDriver driver;


  public ProfileCredentials(WebDriver driver) {
    this.driver = driver;
  }

  private By authorize = By.xpath("//button[@class='btn authorize unlocked']");
  private By apikey = By.xpath("//div[@class='wrapper']//section//input");
  private By authorizecred = By.xpath("//button[@class='btn modal-btn auth authorize button']");
  private By closeauth = By.xpath("//button[@class='btn modal-btn auth btn-done button']");

  public WebElement getAuthorize() {
    return driver.findElement(authorize);
  }

  public WebElement getApiKey() {
    return driver.findElement(apikey);
  }

  public WebElement getAuthCred() {
    return driver.findElement(authorizecred);
  }

  public WebElement getCloseAuth() {
    return driver.findElement(closeauth);
  }
}

我解决了这个问题。“Base”和“InvokeChromeTest”具有相同的方法名。这就是它是递归的原因。

在InvokeChromeTest中,initializeDriver方法需要返回WebDriver。您的
InvokeChromeTest
类中的
initializeDriver
方法指示它将返回
WebDriver
,但它永远不会返回。(不相关,但该方法本身似乎也会无限递归…@CanBayar和@David,感谢您的快速响应。我是否只需要添加
返回驱动程序底部InvokeChrome测试块的内部?对不起,是指initializeDriver块的内部。