{"id":186,"date":"2018-10-31T11:05:43","date_gmt":"2018-10-31T11:05:43","guid":{"rendered":"https:\/\/citrusleaf.in\/blog\/?p=186"},"modified":"2018-10-31T11:05:49","modified_gmt":"2018-10-31T11:05:49","slug":"acceptance-testing-select2-ckeditor-in-codeception","status":"publish","type":"post","link":"https:\/\/citrusleaf.in\/blog\/acceptance-testing-select2-ckeditor-in-codeception\/","title":{"rendered":"Acceptance Testing Select2 and CKEditor in CodeCeption"},"content":{"rendered":"\n<p><a href=\"https:\/\/citrusleaf.in\/blog\/\/acceptance-testing-with-codeception\/\">In the previous article about CodeCeption<\/a>, we learned about writing the test cases with CodeCeption, its installation and basics of doing acceptance testing. <\/p>\n\n\n\n<p>This post will explore two of the most used JavaScript driven HTML components &#8211; <a href=\"https:\/\/select2.org\/\">Select2<\/a> and <a href=\"https:\/\/ckeditor.com\">CKEditor<\/a>.<br><\/p>\n\n\n\n<p>Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options. We always use this library when there are lots of options in a select tag <\/p>\n\n\n\n<p>CKEditor is a battle-tested WYSIWYG HTML editor. It&#8217;s our go-to solution when we need to give complete control to the user on their content.<br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Select2<\/h4>\n\n\n\n<p>Select2 works by changing any existing &lt;select> tag into an auto-complete one. When performing acceptance testing we may need to select an option from any select tag. <\/p>\n\n\n\n<p>This is how standard select tag works in CodeCeption:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$I->selectOption('form select[name=account]', 'Premium');\n$I->selectOption('form input[name=payment]', 'Monthly');\n$I->selectOption('\/\/form\/select[@name=account]', 'Monthly');\n?><\/code><\/pre>\n\n\n\n<p>However, since Select2 is a JS library, we need to use <g class=\"gr_ gr_17 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling multiReplace\" id=\"17\" data-gr-id=\"17\">it&#8217;s<\/g> own methods to select the values. (Assuming that the Select2 has been initialized on a select tag)-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$('#mySelect2').val('1'); \/\/ Select the option with a value of '1'.\n$('#mySelect2').trigger('change'); \/\/ Notify any JS components that the value changed.<\/code><\/pre>\n\n\n\n<p>But this is JavaScript. How do we call it from CodeCeption? <\/p>\n\n\n\n<p>The answer is simple- using the &#8220;executeJS()&#8221; method available in CodeCeption. <\/p>\n\n\n\n<p>Here&#8217;s how to do it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$I->executeJS(\"$('#mySelect2').val('1')\");\n$I->executeJS(\"$('#mySelect2').trigger('change')\");<\/code><\/pre>\n\n\n\n<p>Make sure to call &#8220;trigger(&#8216;change&#8217;)&#8221; method every time you select any option, otherwise, Select2 will not update the UI.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">CKEditor-<\/h4>\n\n\n\n<p>Once you&#8217;ve initialized CKEditor on the &lt;textarea> tag, you can&#8217;t use standard CodeCeption methods for settings its value. Instead, you need to use CKEditor&#8217;s methods.<\/p>\n\n\n\n<p>CKEditor has concept of instances. There can be multiple CKEditor instances on a page. And hence, you need to specify which one to use. Here&#8217;s how to set an instance&#8217;s body (text) value-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CKEDITOR.instances[\"name\"].insertText(\"text editor content\");<\/code><\/pre>\n\n\n\n<p>Once again, you need to use &#8220;<g class=\"gr_ gr_13 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace\" id=\"13\" data-gr-id=\"13\">executeJS<\/g>()&#8221; method of CodeCeption to execute the above JavaScript, like this-<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n$I->executeJS('CKEDITOR.instances[\"name\"].insertText(\"text editor content\")');<\/code><\/pre>\n\n\n\n<p>As you can see, CodeCeption has made it really easy to test everything- from standard HTML components to JavaScript driven components. This concludes this post about CodeCeption.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the previous article about CodeCeption, we learned about writing the test cases with CodeCeption, its installation and basics of doing acceptance testing. This post will explore two of the most used JavaScript driven HTML&hellip; <\/p>\n","protected":false},"author":2,"featured_media":152,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[14],"tags":[15],"class_list":["post-186","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-testing","tag-codeception"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/citrusleaf.in\/blog\/wp-content\/uploads\/2018\/10\/featured_image-6.png?fit=1024%2C600&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pei5Vv-30","_links":{"self":[{"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/posts\/186","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/comments?post=186"}],"version-history":[{"count":2,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":188,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/posts\/186\/revisions\/188"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/media\/152"}],"wp:attachment":[{"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/media?parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/categories?post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/citrusleaf.in\/blog\/wp-json\/wp\/v2\/tags?post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}