+ Start a Discussion
baboonworksfinebaboonworksfine 

How to do integration test on Visualforce pages?

Suppose I have a VF page called "Quick_Edit" for updating an opportunity:

<apex:page standardController="Opportunity">
  <apex:pageMessages />

  <apex:form >
    <apex:pageBlock title="Quick Edit">

      <apex:pageBlockButtons style="float:right;">
        <apex:commandButton value="Save" action="{!save}"/>
        <apex:commandButton value="Cancel" action="{!cancel}"/>
      </apex:pageBlockButtons>
                        
      <span>Opportunity Name:</span>
      <apex:inputField value="{!opportunity.Name}"/>

    </apex:pageBlock>
  </apex:form>

</apex:page>

 

I have a test class like this:

@isTest
public class QuickEditTest {
    
    static testMethod void opportunityEditTest() {
        
        // setting up data
        
        Opportunity op = new Opportunity(
            Name = 'TestOpportunity1', 
            CloseDate = System.today(), 
            StageName = 'Prospecting'
        );
        
        insert op;
           
        
        PageReference pageRef = new PageReference('/apex/'+op.Id);
        Test.setCurrentPage(pageRef);
        
        // now what should I do
    }
}

1. Is it possible to insert data into the form and test the save action?

2. If not, do I have to created getters/setters in a controller (say opportunity extension controller) to set updated field value directly on the opportunity object and then call save?

3. If there is no real good way to interacted with the VF page in tests, what is the purpors of using Test.setCurrentPage(pageRef)? Because I can just test the controller alone.

 

If anybody develops Rails app, you probably know what I need is functionalities like Capybara or Webrat ... 

TheSwamiTheSwami

Currently there is no test method within salesforce which will interact with Visualforce pages.  Apex test methods only test apex code (Visualforce controllers).  In your example, you are using a standard controller, so creating a test method does not really do much for you.

 

I have used htmlunit and selenium for testing of Visualforce pages.  If you want to interact with pages in your testing, using a 3rd party tool is the solution.

Jia HuJia Hu

There is a full example of how to test the interaction with the VF pages

http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_error_handling.htm

 

Just think VF page as a black box, and you set up the input parameter and then test the output one.