1

I made extension class and VF page for getting signature image. I confirmed it works well through many test .

And then, I need to make test class to deploy that extension class into production org.

However I don't know how to make test class getting parameter which is image url made in VF page.

Did you guys have any know how to solve my needs?

my codes is below:

vf_MakeSignature.page

<apex:page docType="html-5.0" standardcontroller="Contract" extensions="ctr_SignatureExtension" showheader="false"
           sidebar="false" standardStylesheets="false" id="pg">
    <apex:includeScript value="/soap/ajax/28.0/connection.js"/>

    <style>
    .container {
        text-align: center;
        font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
        color: cadetblue;
        font-weight: 500;
        font-size: 14px;
    }

    .button {
        font-family: calibri;
        border-radius: 8px;
        background-color: rgb(51, 116, 116);
        height: 36px;
        color: azure;
        font-size: 17px;
        border-width: 0px;
        width: 116px;
    }
    </style>
    <apex:form id="pbform">
        <apex:pageMessages />
        <div class="container">
            <h1 class="labelCol vfLabelColTextWrap ">Record Signature:</h1>
            <canvas id="sig-canvas" height="100px" width="350px" style="border: 3px solid antiquewhite; border-radius: 8px;" ></canvas>
        </div><br/>

        <div style="margin-left: 41%;">
            <apex:commandButton value="Save Signature" onclick="saveSignature();return false;" styleClass="button"/>&nbsp;&nbsp;&nbsp;
            <apex:commandButton value="Clear" onclick="clearSign();return false;" styleClass="button"/>
        </div>
    </apex:form>
    <script>
        // Set up the canvas
        var canvas = document.getElementById("sig-canvas");
        var ctx = canvas.getContext("2d");
        ctx.strokeStyle = "#222222";
        ctx.lineWith = 2;

        // Set up mouse events for drawing
        var drawing = false;
        var mousePos = { x:0, y:0 };
        var lastPos = mousePos;
        canvas.addEventListener("mousedown", function (e) {
            drawing = true;
            lastPos = getMousePos(canvas, e);
        }, false);
        canvas.addEventListener("mouseup", function (e) {
            drawing = false;
        }, false);
        canvas.addEventListener("mousemove", function (e) {
            mousePos = getMousePos(canvas, e);
        }, false);

        // Get the position of the mouse relative to the canvas
        function getMousePos(canvasDom, mouseEvent) {
            var rect = canvasDom.getBoundingClientRect();
            return {
                x: mouseEvent.clientX - rect.left,
                y: mouseEvent.clientY - rect.top
            };
        }

        // Get a regular interval for drawing to the screen
        window.requestAnimFrame = (function (callback) {
            return window.requestAnimationFrame ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame ||
            window.oRequestAnimationFrame ||
            window.msRequestAnimaitonFrame ||
            function (callback) {
                window.setTimeout(callback, 1000/60);
            };
        })();

        // Draw to the canvas
        function renderCanvas() {
            if (drawing) {
                ctx.moveTo(lastPos.x, lastPos.y);
                ctx.lineTo(mousePos.x, mousePos.y);
                ctx.stroke();
                lastPos = mousePos;
            }
        }

        // Allow for animation
        (function drawLoop () {
            requestAnimFrame(drawLoop);
            renderCanvas();
        })();

        // Set up touch events for mobile, etc
        canvas.addEventListener("touchstart", function (e) {
            mousePos = getTouchPos(canvas, e);
            var touch = e.touches[0];
            var mouseEvent = new MouseEvent("mousedown", {
                clientX: touch.clientX,
                clientY: touch.clientY
            });
            canvas.dispatchEvent(mouseEvent);
        }, false);
        canvas.addEventListener("touchend", function (e) {
            var mouseEvent = new MouseEvent("mouseup", {});
            canvas.dispatchEvent(mouseEvent);
        }, false);
        canvas.addEventListener("touchmove", function (e) {
            var touch = e.touches[0];
            var mouseEvent = new MouseEvent("mousemove", {
                clientX: touch.clientX,
                clientY: touch.clientY
            });
            canvas.dispatchEvent(mouseEvent);
        }, false);

        // Get the position of a touch relative to the canvas
        function getTouchPos(canvasDom, touchEvent) {
            var rect = canvasDom.getBoundingClientRect();
            return {
                x: touchEvent.touches[0].clientX - rect.left,
                y: touchEvent.touches[0].clientY - rect.top
            };
        }

        // Prevent scrolling when touching the canvas
        document.body.addEventListener("touchstart", function (e) {
            if (e.target == canvas) {
                e.preventDefault();
            }
        }, false);
        document.body.addEventListener("touchend", function (e) {
            if (e.target == canvas) {
                e.preventDefault();
            }
        }, false);
        document.body.addEventListener("touchmove", function (e) {
            if (e.target == canvas) {
                e.preventDefault();
            }
        }, false);

        function clearCanvas() {
            canvas.width = canvas.width;
        }

        function saveSignature() {
            var strDataURI = canvas.toDataURL();
            strDataURI = strDataURI.replace(/^data:image\/(png|jpg);base64,/, "");
            var contId = location.href.split('=')[1];
            contId = contId.split('&')[0];
            //  alert(contId);
            var result = ctr_SignatureExtension.saveSignature(strDataURI, contId, processResult);
        }

        function processResult(result) {
            //  alert(result);
            var resultParam = result.replace(/amp;/gi, "");
            //  alert('https://c.cs58.visual.force.com' + resultParam);
            location.href = 'https://c.cs58.visual.force.com' + resultParam;

            //  alert(JSON.stringify(result));
            //  var resultParam = JSON.stringify(result).replace(/amp;/gi, "").substring(1, JSON.stringify(result).replace(/amp;/gi, "").length-1);
            //  alert('https://c.cs58.visual.force.com' + resultParam);
            //  location.href = 'https://c.cs58.visual.force.com' + resultParam;
        }
    </script>
</apex:page>

ctr_SignatureExtension.cls

global class ctr_SignatureExtension {
    public Id Id {
        get; set;
    }

    public ctr_SignatureExtension(ApexPages.StandardController controller) {
        Id = controller.getRecord().Id;
        System.debug( Id );
    }

    public Id idParam() {
        return Id;
    }

    @RemoteAction
    global static PageReference saveSignature( String imageUrl, String contId ) {
        Attachment accSign = new Attachment();

        try {
            Contract cont = [SELECT Id, AccountId, Account.PersonContactId, Status, CustomerSignedId, CustomerSignedDate FROM Contract WHERE Id =: contId];
            cont.Status = 'Approved';
            cont.CustomerSignedId = cont.Account.PersonContactId;
            cont.CustomerSignedDate = Date.today();
            update cont;

            accSign.ParentID = contId;
            accSign.Body = EncodingUtil.base64Decode( imageUrl );
            accSign.contentType = 'image/png';
            Date d = date.today();
            String accName = [SELECT Account.Name FROM Contract WHERE ID = :contId].Account.Name;
            accSign.Name = '서명_' + accName + d.year() + '/' + d.month() + '/'+d.day();
            accSign.OwnerId = UserInfo.getUserId();
            insert accSign;

            accSign = [SELECT Id FROM Attachment WHERE ParentId =: contId ORDER BY CreatedDate DESC LIMIT 1];

        } catch ( Exception e ) {
            system.debug( '---------- ' + e.getMessage() );
        }

        System.debug( LoggingLevel.DEBUG, accSign );

        PageReference pageWhereWeWantToGo = Page.vf_AttachPDFOnContract;
        pageWhereWeWantToGo.getParameters().put('cID', contId );
        pageWhereWeWantToGo.getParameters().put('signature', 'true');
        pageWhereWeWantToGo.getParameters().put('signatureId', accSign.Id);
        return pageWhereWeWantToGo;
    }
}
0

In apex class to test this you will need to simply provide the parameters to the method and capture results from the method and assert the results .

You do not need to worry about passing parameter via url

Here is a sample approach

@isTest()
private class Testctr_SignatureExtension{

@isTest
private static void SendEmailCustomerDetailPdf1() {

    // Create test data for your scenario

     Account acc = new Account(Name ='Test');
     insert acc;

     Contact con = new Contact();
     con.LastName = 'Test';
     insert con;

     //Create Contract
     Contract contract = new Contract();
     contract.status = 'Draft';
     Contact.AccountId = acc.Id;
     insert contract;

     // Set page Context
     PageReference pageRef = Page.vf_MakeSignature;
     Test.setCurrentPage(pageRef);

     // Start Test class

     Test.StartTest();
        ApexPages.StandardController sc = new ApexPages.StandardController(contract.Id);
        PageReference testReference = ctr_SignatureExtension.saveSignature('https://testurl.com',con);
     Test.StopTest();
     // Assert the logic
     System.assertEquals([SELECT Id FROM Attachment WHERE ParentId =: contId ORDER BY CreatedDate DESC LIMIT 1].size() , 1,'Attachment created for contact');
    // Add more assertions

  }
}
| improve this answer | |
  • I got it ! Thank you for answering. I'll try. – 손창주 Jul 26 '19 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.