<!DOCTYPE project [
    <!ENTITY config SYSTEM "./config.xmlf">
    <!ENTITY login SYSTEM "./login.xmlf">
]>
<project basedir="." default="run-all-tests">
    <taskdef resource="webtestTaskdefs.properties" />
    <!-- for propertyregex: allows to build a message string with parameter replacement: "User {0} successfully created" -->
    <!-- usage example -->
    <!--
        <propertyregex property="updated.byAdmin"
            input="${user.updated.byAdmin}"
            regexp="\{0\}"
            replace="${it_user_signup.fullname}"
            global="false" />
        <verifytext description="verify success message" text="${updated.byAdmin}"/>
     -->
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />

    <!-- Use i18n bundle, 2nd one listed is the default -->
    <property file="../../../../common/target/classes/ApplicationResources_${user.language}.properties"/>
    <property file="../../../../common/target/classes/ApplicationResources.properties"/>
    <property file="../../../../common/target/classes/displaytag_${user.language}.properties"/>
    <property file="../../../../common/target/classes/displaytag.properties"/>

    <!-- runs all targets -->
    <target name="run-all-tests" 
        depends="Login,Logout,PasswordHint,Signup,UserTests,StaticPages,WebServices,DWR,FileUpload"
        description="Call and executes all test cases (targets)"/>
    
    <!-- runs user-related tests -->
    <target name="UserTests" depends="EditUser,SearchUser,SaveUser,AddUser,ActiveUsers"
        description="Call and executes all user test cases (targets)">
        <echo>Successfully ran all User UI tests!</echo>
    </target>
    
    <!-- Login to the application -->
    <target name="Login" description="Runs login test and verifies Main Menu's Title">
        <webtest name="login">
            &config;
            <steps>
                &login;
            </steps>
        </webtest>
    </target>

    <!-- Logout of the application -->
    <target name="Logout" description="Runs logout test and verifies Login's Title">
        <webtest name="logout">
            &config;
            <steps>
                &login;
                <invoke description="get Logout Page" url="/logout"/>
                <verifytitle description="we should see the login title" text=".*${login.title}.*" regex="true"/>
            </steps>
        </webtest>
    </target>

    <!-- Retrieve Password Hint -->
    <target name="PasswordHint" description="Gets the user's password hint">
        <webtest name="passwordHint">
            &config;
            <steps>
                <invoke description="get Login Page" url="/"/>
                <verifytitle description="we should see the login title" text=".*${login.title}.*" regex="true"/>
                <invoke description="click password hint link" url="/passwordHint?username=admin"/>
                <verifytitle description="show login title again" text=".*${login.title}.*" regex="true"/>
            </steps>
        </webtest>
    </target>
    
    <target name="StaticPages" description="Ensures that StaticFilter is working">
        <webtest name="staticPages">
            &config;
            <steps>
                &login;
                <invoke description="go to static HTML page" url="/scripts/dojo/test.html"/>
                <verifytitle description="we should see static title" text="Static HTML Page" regex="true"/>
                <verifytext description="verify message" text="Static content!"/>
            </steps>
        </webtest>
    </target>

    <!-- Verify the edit user screen displays without errors -->
    <target name="EditUser"  description="Tests selecting the 'Edit Profile' forward">
        <webtest name="editUser">
            &config;
            <steps>
                &login;
                <invoke description="click Edit Profile link" url="/userform"/>
                <verifytitle description="we should see the user profile title" 
                    text=".*${userProfile.title}.*" regex="true"/>
            </steps>
        </webtest>
    </target>
    
    <!-- Verify the user list screen displays without errors -->
    <target name="SearchUser" description="Tests search for and displaying all users">
        <webtest name="searchUser">
            &config;
            <steps>
                &login;
                <invoke description="click View Users link" url="/admin/users"/>
                <verifytitle description="we should see the user list title" 
                    text=".*${userList.title}.*" regex="true"/>
                <setinputfield description="set search term" name="q" value="matt_raible"/>
                <clickbutton label="${button.search}" description="Click Search"/>
                <verifyXPath
                     description="Count the number of users found"
                     xpath="count(//table[@id='users']/tbody/tr)"
                     text="1"/>
            </steps>
        </webtest>
    </target>
    
    <!-- Edit a user and then save -->
    <target name="SaveUser" description="Tests editing and saving a user">
        <webtest name="saveUser">
            &config;
            <steps>
                &login;
                <invoke description="click Edit Profile link" url="/userform"/>
                <verifytitle description="we should see the user profile title" 
                    text=".*${userProfile.title}.*" regex="true"/>
                <setinputfield description="set address" name="address.address" value="2255 Lane Avenue"/>
                <setinputfield description="set zipcode" name="address.postalCode" value="59826"/>
                <clickbutton label="${button.save}" description="Click Save"/>
                <verifytitle description="display Main Menu if save OK"
                     text=".*${mainMenu.title}.*" regex="true"/>
            </steps>
        </webtest>
    </target>
    
    <!-- Add a new user after logging in -->
    <target name="AddUser" description="Adds a new user profile">
        <webtest name="addUser">
            &config;
            <steps>
                &login;
                <invoke description="click Add Button" url="/userform?method=Add&amp;from=list"/>
                <verifytitle description="view the user profile title" text=".*${userProfile.title}.*" regex="true"/>
                    
                <setinputfield description="set username" name="username" value="newuser"/>
                <setinputfield description="set password" name="password" value="test"/>
                <setinputfield description="set confirmPassword" name="confirmPassword" value="test"/>
                <setinputfield description="set firstName" name="firstName" value="Test"/>
                <setinputfield description="set lastName" name="lastName" value="Name"/>
                <setinputfield description="set city" name="address.city" value="Denver"/>
                <setinputfield description="select province" name="address.province" value="CO"/>
                <setselectfield description="select country" name="address.country" value="US"/>
                <setinputfield description="set postalCode" name="address.postalCode" value="80210"/>
                <setinputfield description="set email" name="email" value="newuser@raibledesigns.com"/>
                <setinputfield description="set website" name="website" value="http://raibledesigns.com"/>
                <setinputfield description="set passwordHint" name="passwordHint" value="test"/>
                <setselectfield description="select roles" name="userRoles" value="ROLE_USER"/>
                <clickbutton label="${button.save}" description="Click button 'Save'"/>

                <verifytitle description="view user list screen" text=".*${userList.title}.*" regex="true"/>
                <verifytext description="verify success message" regex="true"
                    text='&lt;div class="alert alert-success.*&gt;.*Test Name.*&lt;/div&gt;'/>
                    
                <!-- Delete user -->
                <clickLink description="Click edit user link" label="newuser"/>
                <prepareDialogResponse description="Confirm delete" dialogType="confirm" response="true"/>
                <clickbutton label="${button.delete}" description="Click button 'Delete'"/>
                <verifyNoDialogResponses/>
                <verifytext description="verify success message" regex="true"
                    text='&lt;div class="alert alert-success.*&gt;.*Test Name.*&lt;/div&gt;'/>
                <verifytitle description="display user list" text=".*${userList.title}.*" regex="true"/>
            </steps>
        </webtest>
    </target>
    
    <!-- Sign up a new user (no login) -->
    <target name="Signup" description="Signup a new user">
        <webtest name="signup">
            &config;
            <steps>
                <invoke description="click signup link" url="/signup"/>
                <verifytitle description="view signup title" text=".*${signup.title}.*" regex="true"/>
                
                <setinputfield description="set username" name="username" value="signup-user"/>
                <setinputfield description="set password" name="password" value="test"/>
                <setinputfield description="set confirmPassword" name="confirmPassword" value="test"/>
                <setinputfield description="set firstName" name="firstName" value="Test"/>
                <setinputfield description="set lastName" name="lastName" value="Name"/>
                <setinputfield description="set city" name="address.city" value="Denver"/>
                <setinputfield description="set state" name="address.province" value="CO"/>
                <setselectfield description="select country" name="address.country" value="US"/>
                <setinputfield description="set postalCode" name="address.postalCode" value="80210"/>
                <setinputfield description="set email" name="email" value="signup-user@raibledesigns.com"/>
                <setinputfield description="set website" name="website" value="http://raibledesigns.com"/>
                <setinputfield description="set passwordHint" name="passwordHint" value="test"/>
                <clickbutton label="${button.register}" description="Click button 'Signup'"/>

                <verifytitle description="view main menu" text=".*${mainMenu.title}.*" regex="true"/>
                <verifytext description="verify success message" text="${user.registered}"/>
            </steps>
        </webtest>
    </target>

    <!-- View Active Users -->
    <target name="ActiveUsers" description="View Active Users List">
        <webtest name="activeUsers">
            &config;
            <steps>
                &login;
                <invoke description="get activeUsers URL" url="/admin/activeUsers"/>
                <verifytitle description="we should see the activeUsers title" 
                    text=".*${activeUsers.title}.*" regex="true"/>
            </steps>
        </webtest>
    </target>
    
    <!-- File Upload -->
    <target name="FileUpload" description="Tests uploading a file">
        <webtest name="fileUpload">
            &config;
            <steps>
                &login;
                <invoke description="click Upload a File link" url="/fileupload"/>
                <verifytitle description="we should see file upload form" text=".*${upload.title}.*" regex="true"/>
                <setinputfield description="set name" name="name" value="Canoo Test File"/>
                <setFileField description="set file" name="file" fileName="web-tests.xml"/>
                <clickbutton label="${button.upload}" description="Click Upload"/>
                <verifytitle description="show file data if upload OK" text=".*${display.title}.*" regex="true"/>
                <verifytext description="verify original filename displayed on page" text="web-tests.xml"/>
            </steps>
        </webtest>
    </target>

    <!-- Verify CXF is configured properly. -->
    <target name="WebServices" description="Verifies CXF Services are listed">
        <webtest name="webservices">
            &config;
            <steps>
                &login;
                <invoke url="/services/"/>
                <verifytitle description="should see services listing" text="CXF - Service list.*" regex="true"/>
                <verifytext description="verify page renders" text="Available SOAP services"/>
                <verifytext description="verify UserManager exist" text="UserService"/>
            </steps>
        </webtest>
    </target>

    <!-- Verify DWR is configured properly. -->
    <target name="DWR" description="Verifies DWR Index Page">
        <webtest name="dwr">
            &config;
            <steps>
                &login;
                <invoke url="/dwr/"/>
                <verifytitle description="should DWR Test Index" text="DWR Test Index.*" regex="true"/>
                <clicklink description="click on UserManager link" label="UserManager"/>
                <verifytext description="verify next page comes up" text="Methods For: UserManager"/>
            </steps>
        </webtest>
    </target>
</project>
