0

I'm trying to simulate an user, selecting a single record on a list page (i.e. Item List). This is for testing a procedure, which opens said list page, waiting for user input and, if the user presses Ok, adds the selected lines from the list page as lines to a document (i.e. Purchase Line/Purchase Header)

    [Test]
    [HandlerFunctions('ItemListOkModalPageHandler')]
    procedure TestAddItemLineItemList()
    var
        LRec_PurchaseHeader: Record "Purchase Header";
        LRec_PurchaseLine: Record "Purchase Line";
        LC_PageHelper: Codeunit "Page Helper";
    begin
        //[GIVEN] given
        CreatePurchaseOrder(LRec_PurchaseHeader);
        LRec_PurchaseLine.SetRange("Document Type", LRec_PurchaseHeader."Document Type");
        LRec_PurchaseLine.SetRange("Document No.", LRec_PurchaseHeader."No.");
        LRec_PurchaseLine.SetRange(Type, "Purchase Line Type"::Item);
        GC_LibraryAssert.RecordIsEmpty(LRec_PurchaseLine);
        //[WHEN] when
        LC_PageHelper.AddMultiplePurchaseLines(LRec_PurchaseHeader);
        //[THEN] then
        GC_LibraryAssert.RecordCount(LRec_PurchaseLine, 1);  // error happens here
    end;

    [ModalPageHandler]
    procedure ItemListOkModalPageHandler(var PTP_ItemList: TestPage "Item List");
    begin
        PTP_ItemList.First();
        PTP_ItemList.OK().Invoke();
    end;

When trying to do it this way, I get the following error message:

Error:
        Assert.RecordCount failed. Expected number of Purchase Line entries: 1. Actual: 0.

Any suggestions, what am I doing wrong?

Edit: Code of Procedure AddMultiplePurchaseLines

procedure AddMultiplePurchaseLines(PRec_PurchaseHeader: Record "Purchase Header")
    var
        LRec_Item: Record Item;
        LP_ItemList: Page "Item List";
    begin
        PRec_PurchaseHeader.TestField(Status, "Purchase Document Status"::Open);
        if GuiAllowed then begin
            LP_ItemList.LookupMode(true);
            if LP_ItemList.RunModal() = Action::LookupOK then begin
                LRec_Item.SetFilter("No.", LP_ItemList.GetSelectionFilter());
                AddPurchaseOrderItemLines(PRec_PurchaseHeader, LRec_Item);
            end;
        end;
    end;
Arti
  • 1
  • 2

1 Answers1

0

The first issue you need to resolve is that activating LookupMode on a Page changes the returned value of RunModal.

This means that in AddMultiplePurchaseLines you must change it to:

if LP_ItemList.RunModal() = Action::LookupOK then begin

Secondly the call to Expand in ItemListOkModalPageHandler does not do anything. All it does is expand the tree structure (if it exists).

kaspermoerch
  • 16,127
  • 4
  • 44
  • 67
  • Thanks for pointing at the problem with the return value of the RunModal procedure. However, it does not fix the problem with selecting the records for the test case. – Arti Jan 19 '22 at 06:09