【问题标题】:Missing return statement, how can I fix?缺少退货声明,我该如何解决?
【发布时间】:2020-09-15 21:47:30
【问题描述】:

目标:我有一个名为“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);
  }
}

【问题讨论】:

  • InvokeChromeTest中,initializeDriver方法需要返回一个WebDriver。
  • initializeDriver 类中的InvokeChromeTest 方法表明它将返回WebDriver,但它永远不会返回。 (不相关,但该方法似乎也在自身上无限递归......)
  • @CanBayar 和@David,感谢您的快速回复。我只需要在底部的 InvokeChromeTest 块内添加return driver; 吗?
  • 抱歉,是在 initializeDriver 块内部。

标签: java selenium intellij-idea webautomation


【解决方案1】:

我发现了问题所在。 “Base”和“InvokeChromeTest”具有相同的方法名称。这就是为什么它是递归的。

【讨论】:

    猜你喜欢
    • 2012-10-04
    • 2013-07-11
    • 2016-06-23
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多