Skip to main content

Notifications

Announcements

No record found.

Small and medium business | Business Central, N...
Suggested answer

Test Codeunit doesn't recognize PageHandler

Posted on by 10

I've written a test codeunit that is supposed to invoke an action on a page that opens a window, so I've defined a PageHandler in the HandlerFunctions property. However, the tests still throw an "Unhandled UI" error.

Here is the full Test Codeunit:

codeunit 78000 "Changelog Action Test CCL"
{
    Description = 'Change Log Plus Action';
    Subtype = Test;

    trigger OnRun();
    begin
        IsInitialized := false;

        OpenChangeLogPlus_ItemList();
        OpenChangeLogPlus_ItemCard();
    end;

    var
        LibraryRandom: Codeunit "Library - Random";
        LibrarySetupStorage: Codeunit "Library - Setup Storage";
        LibraryTestInitialize: Codeunit "Library - Test Initialize";
        LibraryVariableStorage: Codeunit "Library - Variable Storage";
        Assert: Codeunit "Library Assert";
        IsInitialized: Boolean;
        sEntryFilter: Text;

    [Test]
    [HandlerFunctions('ChangeLogPlusPageHandler')]
    procedure OpenChangeLogPlus_ItemList()
    var
        recItem: Record Item;
        pagItemList: TestPage "Item List";
    begin
        // [SCENARIO #0001]
        // [GIVEN] A randomly selected item on the Item List page
        Initialize();

        GetRandomItem(recItem);
        pagItemList.OpenView();
        pagItemList.GoToRecord(recItem);

        // [WHEN] Invoking the "Change Log Plus" action
        pagItemList."Change Log Plus CCL".Invoke();
        pagItemList.Close();

        // [THEN] Filter matches Item Table No. only
        Assert.AreEqual(recItem.GetFilters, sEntryFilter, 'Filter doesn''t match');
    end;

    [Test]
    [HandlerFunctions('ChangeLogPlusPageHandler')]
    procedure OpenChangeLogPlus_ItemCard()
    var
        recItem: Record Item;
        pagItemCard: TestPage "Item Card";
    begin
        // [SCENARIO #0001]
        // [GIVEN] A randomly selected item on the Item Card page
        Initialize();

        GetRandomItem(recItem);
        pagItemCard.OpenView();
        pagItemCard.GoToRecord(recItem);

        // [WHEN] Invoking the "Change Log Plus" action
        pagItemCard."Change Log Plus CCL".Invoke();
        pagItemCard.Close();

        // [THEN] Filter matches Item Table No. & Primary Key 1 only
        Assert.AreEqual(recItem.GetFilters, sEntryFilter, 'Filter doesn''t match');
    end;

    [PageHandler]
    procedure ChangeLogPlusPageHandler(var ChangeLogPlus: Page "Change Log Plus CCL");
    var
        recChangeLogEntry: Record "Change Log Entry";
    begin
        Clear(recChangeLogEntry);
        ChangeLogPlus.GetRecord(recChangeLogEntry);
        sEntryFilter := recChangeLogEntry.GetFilters();
        ChangeLogPlus.Close();
    end;

    local procedure CreateItems(iCount: Integer)
    var
        recItem: Record Item;
        iIndex: Integer;
    begin
        if iCount <= 0 then
            iCount := 1;

        while iIndex < iCount do begin
            Clear(recItem);

            recItem."No." := LibraryRandom.RandText(8);
            if recItem.Insert(true) then
                iIndex  = 1;
        end;
    end;

    local procedure GetRandomItem(var recItem: Record Item)
    var
        iSkip: Integer;
    begin
        recItem.FindFirst();
        iSkip := LibraryRandom.RandIntInRange(0, recItem.Count - 1);
        recItem.Next(iSkip);
    end;

    local procedure Initialize();
    begin
        LibraryTestInitialize.OnTestInitialize(Codeunit::"Changelog Action Test CCL");
        ClearLastError();
        LibraryVariableStorage.Clear();
        LibrarySetupStorage.Restore();
        if IsInitialized then
            exit;

        LibraryTestInitialize.OnBeforeTestSuiteInitialize(Codeunit::"Changelog Action Test CCL");

        LibraryRandom.Init();

        // CUSTOMIZATION: Prepare setup tables etc. that are used for all test functions
        CreateItems(5);

        IsInitialized := true;
        Commit();

        // CUSTOMIZATION: Add all setup tables that are changed by tests to the SetupStorage, so they can be restored for each test function that calls Initialize.
        // This is done InMemory, so it could be run after the COMMIT above
        //   LibrarySetupStorage.Save(DATABASE::"[SETUP TABLE ID]");

        LibraryTestInitialize.OnAfterTestSuiteInitialize(Codeunit::"Changelog Action Test CCL");
    end;
}

And here is the procedure that is executed by the invoked action:

procedure OpenChangeLogFromRec(recordVariant: Variant; filterKeyBoolean: Boolean)
var
    recChangeLogEntry: Record "Change Log Entry";
    cuDataTypeManagement: Codeunit "Data Type Management";
    inputRecordRef: RecordRef;
    invalidErr: Label 'Not a valid Record: %1', Comment = '%1: The Variant passed to the procedure.';
begin
    if not cuDataTypeManagement.GetRecordRef(recordVariant, inputRecordRef) then
        Error(invalidErr, Format(recordVariant));

    Clear(recChangeLogEntry);
    recChangeLogEntry.SetRange("Table No.", inputRecordRef.Number);
    if filterKeyBoolean then
        recChangeLogEntry.SetRange("Primary Key Field 1 Value", inputRecordRef.KeyIndex(1).FieldIndex(1).Value);
    Page.Run(Page::"Change Log Plus CCL", recChangeLogEntry);
end;

Lastly, here's the error message, as well as the page ID.

pastedimage1679407181089v1.png

pastedimage1679407254240v2.png

  • Suggested answer
    Marco Mels Profile Picture
    Marco Mels on at
    RE: Test Codeunit doesn't recognize PageHandler

    Hello,

    We currently do not have dedicated Dev support via the Dynamics 365 Business Central forums, but I wanted to provide you some additional resources to assist. If you need assistance with debugging or coding I would recommend discussing this on one of our communities.

    www.yammer.com/dynamicsnavdev

    www.dynamicsuser.net/.../14

    I will open this up to the community in case they have something to add.

    Thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans