{"id":30406,"date":"2015-12-02T15:14:34","date_gmt":"2015-12-02T09:44:34","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=30406"},"modified":"2015-12-07T14:53:44","modified_gmt":"2015-12-07T09:23:44","slug":"javascript-to-groovy-objects-in-geb-functional-test-cases","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/javascript-to-groovy-objects-in-geb-functional-test-cases\/","title":{"rendered":"Javascript to groovy objects in GEB functional test cases"},"content":{"rendered":"<h2 style=\"text-align: center;\">Use Case<\/h2>\n<p>While\u00a0<a title=\"Functional Testing Services\" href=\"http:\/\/www.tothenew.com\/testing\/automated-independent-manual-testing\">writing functional tests<\/a> sometimes we need to test whether a certain javascript objects have some particular values.<\/p>\n<p>If we have a JavaScript object such as in our page<\/p>\n<p>[code]<br \/>\nvar myObj = { attr1 : &#8216;attr1-val&#8217;, attr2 : &#8216;attr2-val&#8217;};<br \/>\n[\/code]<\/p>\n<p>then after reaching that page we can execute the below in our GEB spec file to get that Object as a Map.<\/p>\n<p>[code]<br \/>\njs.exec(&quot;return myObj&quot;)<br \/>\n[\/code]<\/p>\n<p>Taking this object we can compare the values from some other places for validation e.g. compare with an API to make sure that the data present here matches the data being received from the server.<\/p>\n<p>But sometimes our objects may be bigger or complex. In such situation it could be better to have these objects as Groovy objects so we can easily compare them.<\/p>\n<h2 style=\"text-align: center;\">Solution<\/h2>\n<p>So we can have a class like<\/p>\n<p>[code]<br \/>\n@EqualsAndHashCode<br \/>\nclass MyArgs {<br \/>\n   String attr1<br \/>\n   String attr2<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>Now we can get an object in our GEB tests by using<\/p>\n<p>[code]<br \/>\nMyArgs myArgs = js.exec(&quot;return myObj&quot;) as MyArgs<br \/>\n[\/code]<\/p>\n<h2 style=\"text-align: center;\">Problem &#8211; Extra properties in JS Object<\/h2>\n<p>But what would happen in case the JavaScript object had an extra key?<\/p>\n<p>[code]<br \/>\nvar myObj = { attr1 : &#8216;attr1-val&#8217;, attr2 : &#8216;attr2-val&#8217;, &#8216;EXTRA&#8217;: &#8216;EXTRA-VAL&#8217;};<br \/>\n[\/code]<\/p>\n<p>Our above code will break as there will be no property such as EXTRA in our MyArgs class.<\/p>\n<p>We will see something like<\/p>\n<p>[code]<br \/>\ngroovy.lang.MissingPropertyException: No such property: EXTRA for class: MyArgs<br \/>\n[\/code]<\/p>\n<h2 style=\"text-align: center;\">Solution<\/h2>\n<p>We can take care of any extra properties by using a little metaprogramming trick<\/p>\n<p>[code]<br \/>\n@EqualsAndHashCode<br \/>\nclass MyArgs {<br \/>\n   String attr1<br \/>\n   String attr2<\/p>\n<p>   def propertyMissing(String name, value){<br \/>\n        \/\/ WARNING DON&#8217;T remove this<br \/>\n    }<\/p>\n<p>}<br \/>\n[\/code]<\/p>\n<p>Now only the relevant properties will be mapped and no exception will be thrown in case extra properties are present.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use Case While\u00a0writing functional tests sometimes we need to test whether a certain javascript objects have some particular values. If we have a JavaScript object such as in our page [code] var myObj = { attr1 : &#8216;attr1-val&#8217;, attr2 : &#8216;attr2-val&#8217;}; [\/code] then after reaching that page we can execute the below in our GEB [&hellip;]<\/p>\n","protected":false},"author":161,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":0},"categories":[1818,1],"tags":[1561,14,897,9,55],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30406"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/161"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=30406"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/30406\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=30406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=30406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=30406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}