【问题标题】:How to call a method from a helper class?如何从辅助类调用方法?
【发布时间】:2019-06-05 11:54:40
【问题描述】:

我有一个名为 CommonSteps.java 的类,其中包含以下方法:

    @Then("^the following error is displayed: \"([^\"]*)\"$")
public void the_error_is_displayed(String message) throws Exception {
    // although using the login page to retrieve - these will return if visible from any page!
    message = errorHelper.generateErrorMessage(message);
    assertThat(runtimeState.loginPage.getErrors(), hasItem(containsString(message)));
}

我想在断言中将此方法调用到另一个类中。到目前为止,我已经编写了以下代码:

    @Then("^an upload invoice error message is displayed$")
public void an_upload_invoice_error_message_is_displayed() throws Throwable {
    List<Map<String, Object>> invoiceDocuments = dbHelperInvoices.getInvoiceDocumentsFilePath(testData.getInt("orderRef"));
    if (invoiceDocuments.get(0).get("jobSheetFilePath") == null && invoiceDocuments.get(0).get("invoiceFilePath") != null) {
        assertTrue("Unexpected error message displayed", commonSteps.the_error_is_displayed(message));

我的问题是我应该用什么来代替消息变量?或者我需要添加什么才能使消息变量起作用?由于此代码引发错误。希望这是足够的信息。如果这是一个愚蠢的问题,我对编码很抱歉。

编辑:我之前的代码是

public void an_upload_invoice_error_message_is_displayed() throws Throwable {
    List<Map<String, Object>> invoiceDocuments = dbHelperInvoices.getInvoiceDocumentsFilePath(testData.getInt("orderRef"));
    if (invoiceDocuments.get(0).get("jobSheetFilePath") == null && invoiceDocuments.get(0).get("invoiceFilePath") != null) {
        assertTrue("Unexpected error message displayed", runtimeState.uploadInvoiceDocumentPage.isUploadJobSheetErrorDisplayed());
        outputHelper.takeScreenshot();
    } else if (invoiceDocuments.get(0).get("invoiceFilePath") == null && invoiceDocuments.get(0).get("jobSheetFilePath") != null) {
        assertTrue("Unexpected error message displayed", runtimeState.uploadInvoiceDocumentPage.isUploadInvoiceErrorDisplayed());
        outputHelper.takeScreenshot();
    } else if (invoiceDocuments.get(0).get("invoiceFilePath") == null && invoiceDocuments.get(0).get("jobSheetFilePath") == null){
        assertTrue("Unexpected error message displayed", runtimeState.uploadInvoiceDocumentsPage.isUploadInvoiceErrorDisplayed());
        assertTrue("Unexpected error message displayed", runtimeState.uploadInvoiceDocumentsPage.isUploadJobsheetErrorDisplayed());
        outputHelper.takeScreenshot();
    }
}

但是,我被告知要更改代码以从上面的 CommonSteps.java 调用方法

【问题讨论】:

    标签: java helper


    【解决方案1】:

    我最终只是对值进行了硬编码,因为我找不到任何其他方法来传递变量。不确定这是最好的方法,但它现在可以工作。我的代码如下:

    List<Map<String, Object>> invoiceDocuments = dbHelperInvoices.getInvoiceDocumentsFilePath(testData.getInt("orderRef"));
        if (invoiceDocuments.get(0).get("jobSheetFilePath") == null && invoiceDocuments.get(0).get("invoiceFilePath") != null) {
            commonSteps.the_error_is_displayed("Please select a Job Sheet to upload");
            outputHelper.takeScreenshot();
        } else if (invoiceDocuments.get(0).get("invoiceFilePath") == null && invoiceDocuments.get(0).get("jobSheetFilePath") != null) {
            commonSteps.the_error_is_displayed("Please select an Invoice to upload");
            outputHelper.takeScreenshot();
        } else if (invoiceDocuments.get(0).get("invoiceFilePath") == null && invoiceDocuments.get(0).get("jobSheetFilePath") == null){
            commonSteps.the_error_is_displayed("Please select a Job Sheet to upload");
            commonSteps.the_error_is_displayed("Please select an Invoice to upload");
            outputHelper.takeScreenshot();
        }
    

    【讨论】:

      【解决方案2】:

      您需要创建the_error_is_displayed(String) 所在的类的对象。然后你可以在an_upload_invoice_error_message_is_displayed() 方法中使用它的方法。

      您可以将此对象创建为类声明下方或an_upload_invoice_error_message_is_displayed() 方法中的类的字段。

      如果您仍然卡住,请将整个班级粘贴到 an_upload_invoice_error_message_is_displayed() 所在的位置。

      【讨论】:

        【解决方案3】:

        查看答案to this question

        您需要创建您想要调用的方法 - 在您的情况下为 the_error_is_displayed - public static 方法。这样就可以从任何地方调用它。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2011-03-27
          • 1970-01-01
          • 1970-01-01
          • 2011-08-03
          • 1970-01-01
          • 1970-01-01
          • 2018-08-09
          相关资源
          最近更新 更多