1
\$\begingroup\$

I have a working solution on a training exercise that I wish to improve as I feel there is a better way to utilize the form and get to the fieldsets etc using page model.

HTML:

<!DOCTYPE HTML>
<html lang="en-US">
<head>

    <title>QAE Challenge</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />



    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script type="text/javascript">window.NREUM||(NREUM={}),__nr_require=function(e,n,t){function r(t){if(!n[t]){var o=n[t]={exports:{}};e[t][0].call(o.exports,function(n){var o=e[t][1][n];return r(o||n)},o,o.exports)}return n[t].exports}if("function"==typeof __nr_require)return __nr_require;for(var o=0;o<t.length;o++)r(t[o]);return r}({1:[function(e,n,t){function r(){}function o(e,n,t){return function(){return i(e,[c.now()].concat(u(arguments)),n?null:this,t),n?void 0:this}}var i=e("handle"),a=e(3),u=e(4),f=e("ee").get("tracer"),c=e("loader"),s=NREUM;"undefined"==typeof window.newrelic&&(newrelic=s);var p=["setPageViewName","setCustomAttribute","setErrorHandler","finished","addToTrace","inlineHit","addRelease"],d="api-",l=d+"ixn-";a(p,function(e,n){s[n]=o(d+n,!0,"api")}),s.addPageAction=o(d+"addPageAction",!0),s.setCurrentRouteName=o(d+"routeName",!0),n.exports=newrelic,s.interaction=function(){return(new r).get()};var m=r.prototype={createTracer:function(e,n){var t={},r=this,o="function"==typeof n;return i(l+"tracer",[c.now(),e,t],r),function(){if(f.emit((o?"":"no-")+"fn-start",[c.now(),r,o],t),o)try{return n.apply(this,arguments)}catch(e){throw f.emit("fn-err",[arguments,this,e],t),e}finally{f.emit("fn-end",[c.now()],t)}}}};a("actionText,setName,setAttribute,save,ignore,onEnd,getContext,end,get".split(","),function(e,n){m[n]=o(l+n)}),newrelic.noticeError=function(e,n){"string"==typeof e&&(e=new Error(e)),i("err",[e,c.now(),!1,n])}},{}],2:[function(e,n,t){function r(e,n){if(!o)return!1;if(e!==o)return!1;if(!n)return!0;if(!i)return!1;for(var t=i.split("."),r=n.split("."),a=0;a<r.length;a++)if(r[a]!==t[a])return!1;return!0}var o=null,i=null,a=/Version\/(\S+)\s+Safari/;if(navigator.userAgent){var u=navigator.userAgent,f=u.match(a);f&&u.indexOf("Chrome")===-1&&u.indexOf("Chromium")===-1&&(o="Safari",i=f[1])}n.exports={agent:o,version:i,match:r}},{}],3:[function(e,n,t){function r(e,n){var t=[],r="",i=0;for(r in e)o.call(e,r)&&(t[i]=n(r,e[r]),i+=1);return t}var o=Object.prototype.hasOwnProperty;n.exports=r},{}],4:[function(e,n,t){function r(e,n,t){n||(n=0),"undefined"==typeof t&&(t=e?e.length:0);for(var r=-1,o=t-n||0,i=Array(o<0?0:o);++r<o;)i[r]=e[n+r];return i}n.exports=r},{}],5:[function(e,n,t){n.exports={exists:"undefined"!=typeof window.performance&&window.performance.timing&&"undefined"!=typeof window.performance.timing.navigationStart}},{}],ee:[function(e,n,t){function r(){}function o(e){function n(e){return e&&e instanceof r?e:e?f(e,u,i):i()}function t(t,r,o,i){if(!d.aborted||i){e&&e(t,r,o);for(var a=n(o),u=v(t),f=u.length,c=0;c<f;c++)u[c].apply(a,r);var p=s[y[t]];return p&&p.push([b,t,r,a]),a}}function l(e,n){h[e]=v(e).concat(n)}function m(e,n){var t=h[e];if(t)for(var r=0;r<t.length;r++)t[r]===n&&t.splice(r,1)}function v(e){return h[e]||[]}function g(e){return p[e]=p[e]||o(t)}function w(e,n){c(e,function(e,t){n=n||"feature",y[t]=n,n in s||(s[n]=[])})}var h={},y={},b={on:l,addEventListener:l,removeEventListener:m,emit:t,get:g,listeners:v,context:n,buffer:w,abort:a,aborted:!1};return b}function i(){return new r}function a(){(s.api||s.feature)&&(d.aborted=!0,s=d.backlog={})}var u="nr@context",f=e("gos"),c=e(3),s={},p={},d=n.exports=o();d.backlog=s},{}],gos:[function(e,n,t){function r(e,n,t){if(o.call(e,n))return e[n];var r=t();if(Object.defineProperty&&Object.keys)try{return Object.defineProperty(e,n,{value:r,writable:!0,enumerable:!1}),r}catch(i){}return e[n]=r,r}var o=Object.prototype.hasOwnProperty;n.exports=r},{}],handle:[function(e,n,t){function r(e,n,t,r){o.buffer([e],r),o.emit(e,n,t)}var o=e("ee").get("handle");n.exports=r,r.ee=o},{}],id:[function(e,n,t){function r(e){var n=typeof e;return!e||"object"!==n&&"function"!==n?-1:e===window?0:a(e,i,function(){return o++})}var o=1,i="nr@id",a=e("gos");n.exports=r},{}],loader:[function(e,n,t){function r(){if(!E++){var e=x.info=NREUM.info,n=l.getElementsByTagName("script")[0];if(setTimeout(s.abort,3e4),!(e&&e.licenseKey&&e.applicationID&&n))return s.abort();c(y,function(n,t){e[n]||(e[n]=t)}),f("mark",["onload",a()+x.offset],null,"api");var t=l.createElement("script");t.src="https://"+e.agent,n.parentNode.insertBefore(t,n)}}function o(){"complete"===l.readyState&&i()}function i(){f("mark",["domContent",a()+x.offset],null,"api")}function a(){return O.exists&&performance.now?Math.round(performance.now()):(u=Math.max((new Date).getTime(),u))-x.offset}var u=(new Date).getTime(),f=e("handle"),c=e(3),s=e("ee"),p=e(2),d=window,l=d.document,m="addEventListener",v="attachEvent",g=d.XMLHttpRequest,w=g&&g.prototype;NREUM.o={ST:setTimeout,SI:d.setImmediate,CT:clearTimeout,XHR:g,REQ:d.Request,EV:d.Event,PR:d.Promise,MO:d.MutationObserver};var h=""+location,y={beacon:"bam.nr-data.net",errorBeacon:"bam.nr-data.net",agent:"js-agent.newrelic.com/nr-1118.min.js"},b=g&&w&&w[m]&&!/CriOS/.test(navigator.userAgent),x=n.exports={offset:u,now:a,origin:h,features:{},xhrWrappable:b,userAgent:p};e(1),l[m]?(l[m]("DOMContentLoaded",i,!1),d[m]("load",r,!1)):(l[v]("onreadystatechange",o),d[v]("onload",r)),f("mark",["firstbyte",u],null,"api");var E=0,O=e(5)},{}]},{},["loader"]);</script>
            <script type="text/javascript">
        document.addEventListener("DOMContentLoaded", function(){
            const FORM_TIME_START = Math.floor((new Date).getTime()/1000);
            let formElement = document.getElementById("tfa_0");
            if (null === formElement) {
                formElement = document.getElementById("0");
            }
            let appendJsTimerElement = function(){
                let formTimeDiff = Math.floor((new Date).getTime()/1000) - FORM_TIME_START;
                let cumulatedTimeElement = document.getElementById("tfa_dbCumulatedTime");
                if (null !== cumulatedTimeElement) {
                    let cumulatedTime = parseInt(cumulatedTimeElement.value);
                    if (null !== cumulatedTime && cumulatedTime > 0) {
                        formTimeDiff += cumulatedTime;
                    }
                }
                let jsTimeInput = document.createElement("input");
                jsTimeInput.setAttribute("type", "hidden");
                jsTimeInput.setAttribute("value", formTimeDiff.toString());
                jsTimeInput.setAttribute("name", "tfa_dbElapsedJsTime");
                jsTimeInput.setAttribute("id", "tfa_dbElapsedJsTime");
                jsTimeInput.setAttribute("autocomplete", "off");
                if (null !== formElement) {
                    formElement.appendChild(jsTimeInput);
                }
            };
            if (null !== formElement) {
                if(formElement.addEventListener){
                    formElement.addEventListener('submit', appendJsTimerElement, false);
                } else if(formElement.attachEvent){
                    formElement.attachEvent('onsubmit', appendJsTimerElement);
                }
            }
        });
    </script>

    <link href="https://www.tfaforms.com/dist/form-builder/5.0.0/wforms-layout.css?v=530-13" rel="stylesheet" type="text/css" />

    <link href="https://www.tfaforms.com/uploads/themes/theme-52691.css" rel="stylesheet" type="text/css" />
    <link href="https://www.tfaforms.com/dist/form-builder/5.0.0/wforms-jsonly.css?v=530-13" rel="alternate stylesheet" title="This stylesheet activated by javascript" type="text/css" />
    <script type="text/javascript" src="https://www.tfaforms.com/wForms/3.11/js/wforms.js?v=530-13"></script>
    <script type="text/javascript">
        if(wFORMS.behaviors.prefill) wFORMS.behaviors.prefill.skip = true;
    </script>
    <script type="text/javascript" src="https://www.tfaforms.com/wForms/3.11/js/localization-en_US.js?v=530-13"></script>



</head>
<body class="default wFormWebPage">


    <div id="tfaContent">
        <div class="wFormContainer"  >
    <div class="wFormHeader"></div>
    <style type="text/css"></style><div class=""><div class="wForm" id="4710335-WRPR" dir="ltr">
<div class="codesection" id="code-4710335"></div>
<h3 class="wFormTitle" id="4710335-T">QAE Challenge</h3>
<form method="post" action="https://www.tfaforms.com/responses/processor" class="hintsBelow labelsAbove" id="4710335" role="form" enctype="multipart/form-data">
<div class="oneField field-container-D    " id="tfa_1-D">
<label id="tfa_1-L" class="label preField reqMark" for="tfa_1">Your email</label><br><div class="inputWrapper"><input type="text" id="tfa_1" name="tfa_1" value="" aria-required="true" title="Your email" data-dataset-allow-free-responses="" class="validate-email required"></div>
</div>
<div class="oneField field-container-D    " id="tfa_4-D" role="group" aria-labelledby="tfa_4-L">
<label id="tfa_4-L" class="label preField ">Favorite Food</label><br><div class="inputWrapper"><span id="tfa_4" class="choices vertical "><span class="oneChoice"><input type="checkbox" value="tfa_5" class="" id="tfa_5" name="tfa_5" data-conditionals="#tfa_8" aria-labelledby="tfa_4-L tfa_5-L"><label class="label postField" id="tfa_5-L" for="tfa_5"><span class="input-checkbox-faux"></span>Cheese</label></span><span class="oneChoice"><input type="checkbox" value="tfa_6" class="" id="tfa_6" name="tfa_6" data-conditionals="#tfa_14" aria-labelledby="tfa_4-L tfa_6-L"><label class="label postField" id="tfa_6-L" for="tfa_6"><span class="input-checkbox-faux"></span>Bread</label></span><span class="oneChoice"><input type="checkbox" value="tfa_7" class="" id="tfa_7" name="tfa_7" data-conditionals="#tfa_19" aria-labelledby="tfa_4-L tfa_7-L"><label class="label postField" id="tfa_7-L" for="tfa_7"><span class="input-checkbox-faux"></span>Veggies</label></span></span></div>
</div>
<fieldset id="tfa_8" class="section" data-condition="`#tfa_5`">
<legend id="tfa_8-L">Cheese</legend>
<div class="oneField field-container-D    " id="tfa_9-D" role="radiogroup" aria-labelledby="tfa_9-L">
<label id="tfa_9-L" class="label preField ">Pick a cheese</label><br><div class="inputWrapper"><span id="tfa_9" class="choices vertical "><span class="oneChoice"><input type="radio" value="tfa_10" class="" id="tfa_10" name="tfa_9" aria-labelledby="tfa_9-L tfa_10-L"><label class="label postField" id="tfa_10-L" for="tfa_10"><span class="input-radio-faux"></span>Chedder</label></span><span class="oneChoice"><input type="radio" value="tfa_11" class="" id="tfa_11" name="tfa_9" aria-labelledby="tfa_9-L tfa_11-L"><label class="label postField" id="tfa_11-L" for="tfa_11"><span class="input-radio-faux"></span>Swiss</label></span><span class="oneChoice"><input type="radio" value="tfa_12" class="" id="tfa_12" name="tfa_9" aria-labelledby="tfa_9-L tfa_12-L"><label class="label postField" id="tfa_12-L" for="tfa_12"><span class="input-radio-faux"></span>Brie</label></span></span></div>
</div>
<div class="oneField field-container-D    " id="tfa_13-D">
<label id="tfa_13-L" class="label preField " for="tfa_13">Attach Picture</label><br><div class="inputWrapper"><input type="file" id="tfa_13" name="tfa_13" size="" title="Attach Picture" class=""></div>
</div>
</fieldset>
<fieldset id="tfa_14" class="section" data-condition="`#tfa_6`">
<legend id="tfa_14-L">Bread</legend>
<div class="oneField field-container-D    " id="tfa_15-D" role="group" aria-labelledby="tfa_15-L">
<label id="tfa_15-L" class="label preField ">Bread Types</label><br><div class="inputWrapper"><span id="tfa_15" class="choices vertical "><span class="oneChoice"><input type="checkbox" value="tfa_16" class="" id="tfa_16" name="tfa_16" aria-labelledby="tfa_15-L tfa_16-L"><label class="label postField" id="tfa_16-L" for="tfa_16"><span class="input-checkbox-faux"></span>Rye</label></span><span class="oneChoice"><input type="checkbox" value="tfa_17" class="" id="tfa_17" name="tfa_17" aria-labelledby="tfa_15-L tfa_17-L"><label class="label postField" id="tfa_17-L" for="tfa_17"><span class="input-checkbox-faux"></span>Almond Flour</label></span><span class="oneChoice"><input type="checkbox" value="tfa_18" class="" id="tfa_18" name="tfa_18" aria-labelledby="tfa_15-L tfa_18-L"><label class="label postField" id="tfa_18-L" for="tfa_18"><span class="input-checkbox-faux"></span>Something else</label></span></span></div>
</div>
</fieldset>
<fieldset id="tfa_19" class="section" data-condition="`#tfa_7`">
<legend id="tfa_19-L">Veggies</legend>
<div class="oneField field-container-D    " id="tfa_20-D">
<label id="tfa_20-L" class="label preField " for="tfa_20">Select all the ones you like</label><br><div class="inputWrapper"><select id="tfa_20" multiple name="tfa_20[]" title="Select all the ones you like" class=""><option value="">Please select...</option>
<option value="tfa_21" id="tfa_21" class="">Peppers</option>
<option value="tfa_22" id="tfa_22" class="">Celery</option>
<option value="tfa_23" id="tfa_23" class="">Apples</option></select></div>
</div>
</fieldset>
<div class="actions" id="4710335-A"><input type="submit" data-label="Submit" class="primaryAction" id="submit_button" value="Submit"></div>
<div style="clear:both"></div>
<input type="hidden" value="709-8a7ad32603893fd5f51e64de3741d534" name="tfa_dbCounters" id="tfa_dbCounters" autocomplete="off"><input type="hidden" value="4710335" name="tfa_dbFormId" id="tfa_dbFormId"><input type="hidden" value="" name="tfa_dbResponseId" id="tfa_dbResponseId"><input type="hidden" value="51914f055edc03755e1f773c4771ede9" name="tfa_dbControl" id="tfa_dbControl"><input type="hidden" value="1553796870" name="tfa_dbTimeStarted" id="tfa_dbTimeStarted" autocomplete="off"><input type="hidden" value="3" name="tfa_dbVersionId" id="tfa_dbVersionId"><input type="hidden" value="" name="tfa_switchedoff" id="tfa_switchedoff">
</form>
</div></div><div class="wFormFooter"><p class="supportInfo"><a target="new" class="contactInfoLink" href="https://www.tfaforms.com/forms/help/4710335">Contact Information</a><br></p></div>
  <p class="supportInfo" >


      </p>
 </div>    </div>

        <script src='https://www.tfaforms.com/js/iframe_resize_helper_internal.js'></script>

<script type="text/javascript">window.NREUM||(NREUM={});NREUM.info={"beacon":"bam.nr-data.net","licenseKey":"c33294f5df","applicationID":"90069622","transactionName":"YQNTMBRRXxZTAkJZVlhJchEVRF4IHUs=","queueTime":0,"applicationTime":129,"atts":"TURQRlxLTBg=","errorBeacon":"bam.nr-data.net","agent":""}</script></body>
</html>

Here is the code :

<?php

namespace Pages;

use SensioLabs\Behat\PageObjectExtension\PageObject\Page;
use Behat\Mink\Exception\ElementNotFoundException;
use WebDriver\Exception\NoAlertOpenError;


class ChallengePage extends Page
{
    protected $path = '/4710335';

    protected $elements = array(
        'Form' => array('xpath' => '//*[@id="4710335"]'),
        'Section_Cheese' => array('xpath' => '//fieldset[@id="tfa_8"]')
    );


    public function fillEmailField($email):ChallengePage
    {
        $form = $this->getElement('Form');
        $form->fillField('tfa_1', $email);
        return $this;
    }

    public function submitForm()
    {
        $form = $this->getElement('Form');
        try{
            $form->submit();
            $this->getDriver()->getWebDriverSession()->accept_alert();
        }catch(NoAlertOpenError $e){

        }

    }

    public function checkFavoriteFood($favFood):ChallengePage
    {
        $form = $this->getElement('Form');
        if ($favFood=='Cheese')
            $form->checkField('tfa_5');
        return $this;
    }


    public function unCheckFavoriteFood($favFood):ChallengePage
    {
        $form = $this->getElement('Form');
        if ($favFood=='Cheese')
            $form->uncheckField('tfa_5');
        return $this;
    }

    public function isSectionVisible($sectionName):Bool
    {
         $section = $this->find('xpath', $this->elements['Section_'.$sectionName]['xpath']);
         return $section->isVisible();
    }


    public function attachAFileForUpload():ChallengePage
    {
        $form = $this->getElement('Form');
        $form->attachFileToField('tfa_13', public_path().'/rabbit.jpg' );
        return $this;
    }



    private function wait($seconds) {
        sleep($seconds);
    }

}
\$\endgroup\$
  • \$\begingroup\$ I don't see any advantage in checking null !== cumulatedTime && on a parseInt() value. \$\endgroup\$ – mickmackusa Mar 30 at 2:07

Your Answer

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

Browse other questions tagged or ask your own question.