{"id":6645,"date":"2012-08-28T10:18:55","date_gmt":"2012-08-28T04:48:55","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6645"},"modified":"2016-12-19T15:16:40","modified_gmt":"2016-12-19T09:46:40","slug":"using-modules-to-avoid-structures-repetition-in-geb-test-pages","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/using-modules-to-avoid-structures-repetition-in-geb-test-pages\/","title":{"rendered":"Using Modules to avoid structures repetition in Geb Test Pages"},"content":{"rendered":"<p>Using modules in Geb testing can help to avoid repetition of same code\/ sections in Geb test pages. In this blog I want to talk about repeating data structures such as tables and how to avoid this repition by modelling them.In Geb testing if there is some content which is used in multiple pages then we can make its definition reusable by creating a module.They are similar to pages, but unlike pages they extend Module.<\/p>\n<p>[java]<br \/>\nclass SubmitButtonModule extends Module {<br \/>\n    static content = {<br \/>\n        button { $(&amp;quot;input&amp;quot;, type: &amp;quot;submit&amp;quot;) }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>Page can use modules in following manner:<\/p>\n<p>[java]<br \/>\nclass LoginPage extends Page {<br \/>\n    static content = {<br \/>\n        myModule { module SubmitButtonModule }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>This module method is only available in content template definitions. The content is set as an instance of module. Now we can use this module in following manner:<\/p>\n<p>[java]<br \/>\nBrowser.drive {<br \/>\n    to LoginPage<br \/>\n    myModule.button.click()<br \/>\n}<br \/>\n[\/java]<\/p>\n<p><strong>Repeating Structures<\/strong><\/p>\n<p>A more interesting usage of modules which I used in my project is to deal with  repeating sections that are present on one page, such as table rows. Consider the following HTML<\/p>\n<p>[java]<br \/>\n&amp;lt;table&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;th&amp;gt;Company Name&amp;lt;\/th&amp;gt;&amp;lt;th&amp;gt;Location&amp;lt;\/th&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;ABC Pvt Ltd&amp;lt;\/td&amp;gt;&amp;lt;td&amp;gt;Banglore&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n    &amp;lt;tr&amp;gt;<br \/>\n        &amp;lt;td&amp;gt;XYZ Pvt Ltd&amp;lt;\/td&amp;gt;&amp;lt;td&amp;gt;Noida&amp;lt;\/td&amp;gt;<br \/>\n    &amp;lt;\/tr&amp;gt;<br \/>\n&amp;lt;\/table&amp;gt;<\/p>\n<p>[\/java]<\/p>\n<p>We can model this with the following<\/p>\n<p>[java]<br \/>\nimport geb.*<\/p>\n<p>class CompanyPage extends Page {<br \/>\n    static content = {<br \/>\n        companyResult { index -&amp;gt;<br \/>\n            module CompanyTableRow, $(&amp;quot;table tr&amp;quot;, index + 1) \/\/ +1 is used to avoid header row<br \/>\n        }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>[java]<br \/>\nclass CompanyTableRow extends Module {<br \/>\n    static content = {<br \/>\n         cell { i -&amp;gt; $(&amp;quot;td&amp;quot;, i) }<br \/>\n        companyName { cell(0).text() }<br \/>\n        location { cell(1).text() }<br \/>\n    }<br \/>\n}<br \/>\n[\/java]<\/p>\n<p>We can now write code like\u2026<\/p>\n<p>[java]<br \/>\nassert companyResult(0).companyName == &amp;quot;ABC Pvt Ltd&amp;quot;<br \/>\nassert companyResult(1).location == &amp;quot;Noida&amp;quot;<br \/>\n[\/java]<\/p>\n<p>I tried to explain with simple examples. We can use it with multi-level navigation structures as well.<br \/>\nHope this will help you.<br \/>\nGunpreet Bedi<br \/>\ngunpreet@intelligrape.com<\/p>\n<p><a href=\"https:\/\/twitter.com\/gunpreet_ginny\">https:\/\/twitter.com\/gunpreet_ginny<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using modules in Geb testing can help to avoid repetition of same code\/ sections in Geb test pages. In this blog I want to talk about repeating data structures such as tables and how to avoid this repition by modelling them.In Geb testing if there is some content which is used in multiple pages then [&hellip;]<\/p>\n","protected":false},"author":44,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[14,897,4840],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6645"}],"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\/44"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6645"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6645\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}