2

I have a class with the name CommonSteps.java which contains the following method:

    @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)));
}

I would like to call this method into another class in an assertion. I have written the following code so far :

    @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));

My question is what should i use in the place of message variable ? Or what do I need to add to get message variable to work ? As this code is throwing an error. Hope this is enough information. I am new to coding so apologies if this is a daft question.

EDIT: My previous code for this was

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();
    }
}

However, I was told to change the code to call the method from CommonSteps.java above

jamiemca
  • 69
  • 9

3 Answers3

0

You need to create an object of the Class where the_error_is_displayed(String) lives. Then you can use its methods in the an_upload_invoice_error_message_is_displayed() method.

You can create this object as a field to the class just underneith the class declaration, or within the an_upload_invoice_error_message_is_displayed() method.

If you're still stuck, paste the entire class where an_upload_invoice_error_message_is_displayed() lives.

James
  • 347
  • 4
  • 13
0

See the answer to this question

You need to make the method that you'd like to call - the_error_is_displayed in your case - a public static method. That way it can be called from anywhere.

hmiedema9
  • 948
  • 4
  • 17
0

I ended up just hardcoding the value as I couldn't find any other way to pass the variable through. Not sure this is the best way but it will work for now. My code is below :

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();
    }
jamiemca
  • 69
  • 9