How to do it...

  1. Open your AL project in Visual Studio Code.
  2. In Explorer, select My Test.al and in the editor, add a new test function to the codeunit:
[Test]
procedure TestPageTest()
begin

end;
  1. We're going to recreate the SuccessTest() test that we created in the previous recipe, only this time, we will create the test to mimic what the user would actually do in the Business Central client. Thinking about what the user does manually to create a new Television Show record, our test needs to do the following:
    1. Open the Television Show Card page.
    2. Enter a value into the Code field and validate it to create the new record.
  • Let's start by creating a few variables that we'll need in our TestPageTest() function:
TelevisionShow: Record "Television Show";
TelevisionShowCard: TestPage "Television Show Card";
LibraryUtility: Codeunit "Library - Utility";
Assert: Codeunit Assert;
NewShowCode: Code[20];

The testpage object is a special kind of page object that can only be used in test functions. It allows you to execute pages in a test mode where the pages execute like normal pages would, except that there is no UI created. Test pages also provide access to some additional page functions that are not normally available.

More information can be found here: https://docs.microsoft.com/en-us/dynamics365/business-central/dev-itpro/developer/methods-auto/testpage/testpage-data-type.

  1. Now, let's add our test logic (following the previously discussed Given-When-Then format, of course!):
//GIVEN: Television Show Card
NewShowCode := LibraryUtility.GenerateRandomCode20(TelevisionShow.FieldNo(Code), Database::"Television Show");

//WHEN: user opens Television Show card and creates new record
TelevisionShowCard.OpenNew();
TelevisionShowCard.Code.SetValue(NewShowCode);
TelevisionShowCard.OK().Invoke();

//THEN: the new record is successfully added to the table and exists once
TelevisionShow.SetRange(Code, NewShowCode);
Assert.RecordCount(TelevisionShow, 1);

Okay, we're doing a few things in the previous code:

    1. First, we generate a random code to use for the new Television Show identifier.
    2. Next, we perform the test using the same steps that a user would perform if they were doing it manually:
      1. Open the Television Show Card page.
      2. Validate the random identifier value into the Code field.
      3. Close the Television Show Card page
    3. Finally, to test the result, we filter the Television Show table directly for the random identifier we generated. We expect to find one record in the table matching that unique identifier.
  1. Let's try it out! Press F5 to build and publish your test application. In your development sandbox, follow these steps:
    1. Use the  icon to search for Test Tool and click the link to open it.
    2. Delete any existing entries in the page or create a new test suite.
    3. Select Process | Get Test Codeunits | Select Test Codeunits and press OK to view the list of all the testing codeunits.
    4. Find the My Test entry and select it. Click OK.
    5. Select Run | All.

If all goes according to plan, you should see your new test with a success result, like this:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.218.147