{"id":12252,"date":"2020-02-12T00:40:27","date_gmt":"2020-02-12T05:40:27","guid":{"rendered":"https:\/\/qxf2.com\/blog\/?p=12252"},"modified":"2020-02-12T00:40:27","modified_gmt":"2020-02-12T05:40:27","slug":"how-screenshots-are-saved-in-qxf2-framework","status":"publish","type":"post","link":"https:\/\/qxf2.com\/blog\/how-screenshots-are-saved-in-qxf2-framework\/","title":{"rendered":"How screenshots are captured for a test run in Qxf2&#8217;s Page Object Model framework"},"content":{"rendered":"<p>This post will help you in understanding\u00a0how the Qxf2&#8217;s page object model\u00a0captures the screenshots when a test is run.<\/p>\n<hr \/>\n<h3>Why this post:<\/h3>\n<p>Screenshots are desirable for bug analysis.\u00a0<span>Suppose you write a test script to automate a web page, you would not keep monitoring to see if it\u2019s working right every time.<\/span> You would let the script do its job and you would be occupied in some other work.\u00a0Screenshots help us in understanding the flow of application and checks if it is behaving accordingly.<\/p>\n<hr \/>\n<h3>Setup:<\/h3>\n<ul>\n<li>You will need to fork and clone<span>\u00a0<\/span><a title=\"\" href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">qxf2&#8217;s page object model<\/a>\u00a0repo.<\/li>\n<li>Follow the setup instructions mentioned in the<span>\u00a0<\/span><a title=\"\" href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\/blob\/master\/Readme.md\">Readme.md<\/a><span>\u00a0<\/span>file.<\/li>\n<\/ul>\n<hr \/>\n<h3>How capturing screenshots are implemented in Qxf2&#8217;s framework:<\/h3>\n<p>We have written a screenshot method in the utils\/wrapit.py file. This method is used as a decorator and this\u00a0<span>decorator is called before every method in Page Objects.<\/span> You can refer to <a href=\"https:\/\/qxf2.com\/blog\/python-decorators\/\">Understanding Python decorators<\/a> blog to know how decorators work in Python.<br \/>\nWe use a wrapper @Wrapit._screenshot before a test method to take screenshots. The below code sets the name in the text field predefined with the decorator.<\/p>\n<pre lang=\"python\">@Wrapit._screenshot\r\ndef set_name(self,name):\r\n   \"Set the name on the form\"\r\n    result_flag = self.set_text(self.name_field,name)<\/pre>\n<p>When a\u00a0@Wrapit._screenshot is called before the test method, the test method itself is passed as a parameter to the method screenshot. There is another method under screenshot namely wrapper which stores this test method name to use for screenshot name and executes this test method and captures the screenshot.\u00a0There is a screenshot counter which keeps incrementing after capturing each screenshot.<\/p>\n<pre lang=\"python\">   def _screenshot(func):\r\n        \"Decorator for taking screenshots\"\r\n        #Usage: Make this the first decorator to a method (right above the 'def function_name' line)\r\n        #Otherwise, we cannot name the screenshot with the name of the function that called it\r\n        def wrapper(*args,**kwargs):\r\n            result = func(*args,**kwargs)\r\n            screenshot_name = '%003d'%args[0].screenshot_counter + '_' + func.__name__\r\n            args[0].screenshot_counter += 1\r\n            args[0].save_screenshot(screenshot_name)\r\n            return result\r\n        return wrapper\r\n    _screenshot = staticmethod(_screenshot)<\/pre>\n<p><span>In a similar way, the screenshots for other Page Object methods are captured.\u00a0<\/span>Once the screenshots are captured,\u00a0the screenshots are converted into a Gif file and stored on the same Screenshot folder. You can refer to\u00a0<a href=\"https:\/\/qxf2.com\/blog\/converting-test-screenshots-into-gif\/\">Converting test screenshots into Gif<\/a> to understand how a Gif file is created.<\/p>\n<hr \/>\n<h3>How to run:<\/h3>\n<ul>\n<li>I hope you have already cloned<span>\u00a0<\/span><a title=\"\" href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\">qxf2 page object model<\/a>\u00a0repo and followed the setup section of our\u00a0<span>\u00a0<\/span><a title=\"\" href=\"https:\/\/github.com\/qxf2\/qxf2-page-object-model\/blob\/master\/Readme.md\">Readme.md<\/a><span>\u00a0<\/span>file.<\/li>\n<li>Next, open up Gitbash and execute<strong><em><span>\u00a0<\/span>pytest -k example_form<\/em><\/strong><span>\u00a0<\/span>command.<span>\u00a0<\/span><\/li>\n<\/ul>\n<p><span>A screenshot folder will be created for every test script inside the parent screenshot directory.\u00a0At the end of the test run, you will see the Screenshots and Gif file created under &#8220;\/Screenshot\/executed test run\/&#8221; folder.<\/span><\/p>\n<p><a href=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/screenshot1-1.png\" data-rel=\"lightbox-image-0\" data-rl_title=\"\" data-rl_caption=\"\" title=\"\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-12324 aligncenter\" src=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/screenshot1-1-273x300.png\" alt=\"\" width=\"273\" height=\"300\" srcset=\"https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/screenshot1-1-273x300.png 273w, https:\/\/qxf2.com\/blog\/wp-content\/uploads\/2020\/02\/screenshot1-1.png 390w\" sizes=\"auto, (max-width: 273px) 100vw, 273px\" \/><\/a><\/p>\n<p>I hope this post helps in understanding how screenshots are captured and stored in Qxf2&#8217;s Page Object Model framework.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post will help you in understanding\u00a0how the Qxf2&#8217;s page object model\u00a0captures the screenshots when a test is run. Why this post: Screenshots are desirable for bug analysis.\u00a0Suppose you write a test script to automate a web page, you would not keep monitoring to see if it\u2019s working right every time. You would let the script do its job and [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[189,107,18],"tags":[],"class_list":["post-12252","post","type-post","status-publish","format-standard","hentry","category-debugging","category-pytest","category-python"],"_links":{"self":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12252","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/comments?post=12252"}],"version-history":[{"count":29,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12252\/revisions"}],"predecessor-version":[{"id":12367,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/posts\/12252\/revisions\/12367"}],"wp:attachment":[{"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/media?parent=12252"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/categories?post=12252"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/qxf2.com\/blog\/wp-json\/wp\/v2\/tags?post=12252"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}