private void PostPurchaseInvoice(
            UserContext userContext,
            ClientLogicalForm purchaseInvoicePage)
        {
            ClientLogicalForm openPostedInvoiceDialog;
            using (new TestTransaction(TestContext, "Post"))
            {
                var postConfirmationDialog = purchaseInvoicePage.Action("Post")
                    .InvokeCatchDialog();
                if (postConfirmationDialog == null)
                {
                    userContext.ValidateForm(purchaseInvoicePage);
                    Assert.Fail("Confirm Post dialog not found");
                }
                openPostedInvoiceDialog = postConfirmationDialog.Action("Yes")
                    .InvokeCatchDialog();
            }

            if (openPostedInvoiceDialog == null)
            {
                Assert.Fail("Open Posted Invoice dialog not found");
            }

            ClientLogicalForm postedPurchaseInvoicePage;
            using (new TestTransaction(TestContext, "OpenPostedPurchaseInvoice"))
            {
                postedPurchaseInvoicePage = userContext.EnsurePage(
                        PostedPurchaseInvoiceCard,
                        openPostedInvoiceDialog.Action("Yes").InvokeCatchForm());

            }

            TestContext.WriteLine(
                    "Posted Purchase Invoice {0}",
                    postedPurchaseInvoicePage.Caption);

            TestScenario.ClosePage(
                TestContext,
                userContext,
                postedPurchaseInvoicePage);
        }
        private void PostSalesOrder(UserContext userContext, ClientLogicalForm newSalesOrderPage)
        {
            ClientLogicalForm postConfirmationDialog;
            using (new TestTransaction(TestContext, "Post"))
            {
                postConfirmationDialog = newSalesOrderPage.Action("Post...").InvokeCatchDialog();
            }

            if (postConfirmationDialog == null)
            {
                userContext.ValidateForm(newSalesOrderPage);
                Assert.Inconclusive("Post dialog can't be found");
            }

            using (new TestTransaction(TestContext, "ConfirmShipAndInvoice"))
            {
                ClientLogicalForm dialog = userContext.CatchDialog(postConfirmationDialog.Action("OK").Invoke);
                if (dialog != null)
                {
                    // after confiming the post we dont expect more dialogs
                    Assert.Fail("Unexpected Dialog on Post - Caption: {0} Message: {1}", dialog.Caption, dialog.FindMessage());
                }
            }
        }
        private ClientLogicalForm CreateNewPurchaseInvoice(UserContext userContext)
        {
            // Invoke using the Purchase Invoice action on Role Center and catch the new page
            var newPurchaseInvoicePage = userContext.EnsurePage(
                MiniPurchaseInvoiceCard,
                userContext.RoleCenterPage.Action("Purchase Invoice")
                    .InvokeCatchForm());

            var vendorName = TestScenario.SelectRandomRecordFromListPage(
                TestContext,
                userContext,
                MiniVendorList,
                "Name");

            TestScenario.SaveValueAndIgnoreWarning(
                TestContext,
                userContext,
                newPurchaseInvoicePage.Control("Vendor Name"),
                vendorName);

            var vendorInvoiceNo = SafeRandom.GetRandomNext(100000, 999999);
            TestScenario.SaveValueWithDelay(
                newPurchaseInvoicePage.Control("Vendor Invoice No."),
                vendorInvoiceNo);

            // Add a random number of lines between 2 and 15
            var noOfLines = SafeRandom.GetRandomNext(2, 15);
            for (var line = 0; line < noOfLines; line++)
            {
                AddPurchaseInvoiceLine(userContext, newPurchaseInvoicePage, line);
            }

            userContext.ValidateForm(newPurchaseInvoicePage);
            TestContext.WriteLine(
                "Created Purchase Invoice {0}",
                newPurchaseInvoicePage.Caption);
            return newPurchaseInvoicePage;
        }
        public void RunCreateAndPostSalesOrder(UserContext userContext)
        {
            // Invoke using the new sales order action on Role Center
            var newSalesOrderPage = userContext.EnsurePage(SalesOrderPageId, userContext.RoleCenterPage.Action("Sales Order").InvokeCatchForm());

            // Start in the No. field
            newSalesOrderPage.Control("No.").Activate();

            // Navigate to Sell-to Customer No. field in order to create record
            var sellToCustControl = newSalesOrderPage.Control("Sell-to Customer No.");
            sellToCustControl.Activate();

            // select a random customer from Sell-to Customer No. lookup
            var custNo = TestScenario.SelectRandomRecordFromLookup(TestContext, userContext, sellToCustControl, "No.");

            // Set Sell-to Customer No. to a Random Customer and ignore any credit warning
            TestScenario.SaveValueAndIgnoreWarning(TestContext, userContext, sellToCustControl, custNo);

            TestScenario.SaveValueWithDelay(newSalesOrderPage.Control("External Document No."), custNo);
            var newSalesOrderNo = newSalesOrderPage.Control("No.").StringValue;
            userContext.ValidateForm(newSalesOrderPage);
            TestContext.WriteLine("Created Sales Order No. {0} for Cust No. {1}", newSalesOrderNo, custNo);

            // Add a random number of lines between 2 and 25
            var noOfLines = SafeRandom.GetRandomNext(2, 25);
            for (var line = 0; line < noOfLines; line++)
            {
                AddSalesOrderLine(userContext, newSalesOrderPage, line);
            }

            // Check Validation errors
            userContext.ValidateForm(newSalesOrderPage);

            // Post the order
            PostSalesOrder(userContext, newSalesOrderPage);

            // Close the page
            TestScenario.ClosePage(TestContext, userContext, newSalesOrderPage);
        }