我有一个名为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));
我的问题是,我应该用什么代替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中调用该方法
答案 0 :(得分:0)
您需要创建一个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()
所在的地方。
答案 1 :(得分:0)
查看答案to this question
您需要将要调用的方法-在您的情况下为the_error_is_displayed
-一个public static
方法。这样就可以在任何地方调用它。
答案 2 :(得分:0)
我最终只是对值进行了硬编码,因为我找不到其他任何方法来传递变量。不确定这是最好的方法,但目前是否可以使用。我的代码如下:
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();
}