{"id":2809,"date":"2020-04-02T17:29:04","date_gmt":"2020-04-02T17:29:04","guid":{"rendered":"https:\/\/codoid.com\/behave-before-scenario-example-using-selenium\/"},"modified":"2022-02-04T18:28:29","modified_gmt":"2022-02-04T18:28:29","slug":"behave-before-scenario-example-using-selenium","status":"publish","type":"post","link":"https:\/\/codoid.com\/selenium-testing\/behave-before-scenario-example-using-selenium\/","title":{"rendered":"Behave Before Scenario Example using Selenium"},"content":{"rendered":"<p>Running prerequisite and cleanup snippets are necessary to make your BDD scenarios independent. In this blog, you will learn how to setup and tear down using Python Behave framework and \u2018Before\u2019 Scenario Example using Selenium. <\/p>\n<p> We, as a <a href=\"https:\/\/codoid.com\/automation-testing-services\/\">test automation services company<\/a>, use Python and behave for multiple automation testing projects.<\/p>\n<h3>Launching &#038; Quitting Browser Before and After Scenario<\/h3>\n<p>Inside the <b>environment.py<\/b> file,  create a fixture to launch a browser before scenario and quit it after the scenario.<\/p>\n<h3>Setup &#038; Tear Down Code<\/h3>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n@fixture\r\ndef launch_browser(context):\r\n    #Launch Browser\r\n    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')\r\n    yield context.driver\r\n\r\n    #Clean Up Browser\r\n    context.driver.quit()\r\n    print(&quot;=============&gt;Browser is quit&quot;)\r\n  <\/pre>\n<\/div>\n<p><\/p>\n<p>If you notice the above code, you can find both Setup &#038; Tear-down in the same method. It reduces your scripting efforts to a great extend and eases the script debugging &#038; maintenance. The <a href=\"https:\/\/realpython.com\/introduction-to-python-generators\/\" target=\"_blank\" rel=\"noopener noreferrer\">&#8216;yield&#8217; statement<\/a> provides the webdriver driver instance. After that the test run executes each steps in the scenario and resumes the remaining statements (i.e. the clean-up steps) which are after the yield statement.<\/p>\n<h3>Before Scenario Method Call<\/h3>\n<p>After defining the fixture, you need a method to call the fixture (i.e. before_scenario).<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\ndef before_scenario(context,scenario):\r\n    the_fixture1 = use_fixture(launch_browser, context)\r\n  <\/pre>\n<\/div>\n<h3>Full Code<\/h3>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfrom behave import fixture, use_fixture\r\nfrom selenium import webdriver\r\n\r\n@fixture\r\ndef launch_browser(context):\r\n    #Launch Browser\r\n    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')\r\n    yield context.driver\r\n\r\n    #Clean Up Browser\r\n    context.driver.quit()\r\n    print(&quot;=============&gt;Browser is quit&quot;)\r\n\r\n\r\ndef before_scenario(context,scenario):\r\n    the_fixture1 = use_fixture(launch_browser, context)\r\n  <\/pre>\n<\/div>\n<h3>How to get the scenario status?<\/h3>\n<p>Behave has four statuses for each Scenario namely: untested, skipped, passed, failed. To retrieve the status, use the below statement.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nprint(context.scenario.status)\r\n  <\/pre>\n<\/div>\n<h3>Scenario Duration<\/h3>\n<p>In <a href=\"https:\/\/codoid.com\/python-behave-bdd-framework-overview\/\">behave framework<\/a>, you can get the scenario duration in the clean-up section as shown below.<\/p>\n<div class=\"editor editor-dark\">\n<div class=\"editor-top\">\n<ul class=\"editor-controls\">\n<li><\/li>\n<li><\/li>\n<li><\/li>\n<\/ul>\n<\/div>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n@fixture\r\ndef launch_browser(context):\r\n    #Launch Browser\r\n    context.driver = webdriver.Chrome(executable_path='driverschromedriver.exe')\r\n    print(&quot;=============&gt;Browser is launched&quot;)\r\n    yield context.driver\r\n\r\n    #Clean Up Browser\r\n    context.driver.quit()\r\n    print(context.scenario.duration)\r\n    print(&quot;=============&gt;Browser is quit&quot;)\r\n  <\/pre>\n<\/div>\n<h3>In Conclusion<\/h3>\n<p>We hope the snippets which are shared in this blog article are useful. In our upcoming blog articles, we will through light on some of the most useful Python automation testing snippets. Subscribe to our blogs to get latest updates.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Running prerequisite and cleanup snippets are necessary to make your BDD scenarios independent. In this blog, you will learn how to setup and tear down using Python Behave framework and \u2018Before\u2019 Scenario Example using Selenium. We, as a test automation services company, use Python and behave for multiple automation testing projects. Launching &#038; Quitting Browser [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5440,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[187,179,20],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.3 (Yoast SEO v20.3) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Behave Before Scenario Example using Selenium - Codoid<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Behave Before Scenario Example using Selenium\" \/>\n<meta property=\"og:description\" content=\"Running prerequisite and cleanup snippets are necessary to make your BDD scenarios independent. In this blog, you will learn how to setup and tear down using Python Behave framework and \u2018Before\u2019 Scenario Example using Selenium. We, as a test automation services company, use Python and behave for multiple automation testing projects. Launching &#038; Quitting Browser [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/\" \/>\n<meta property=\"og:site_name\" content=\"Codoid\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codoid.softwaretestingcompany\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-02T17:29:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-04T18:28:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/Behave_Before_Scenario_Example_using_Selenium_Blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codoid\" \/>\n<meta name=\"twitter:site\" content=\"@codoid\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Behave Before Scenario Example using Selenium - Codoid","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"og_locale":"en_US","og_type":"article","og_title":"Behave Before Scenario Example using Selenium","og_description":"Running prerequisite and cleanup snippets are necessary to make your BDD scenarios independent. In this blog, you will learn how to setup and tear down using Python Behave framework and \u2018Before\u2019 Scenario Example using Selenium. We, as a test automation services company, use Python and behave for multiple automation testing projects. Launching &#038; Quitting Browser [&hellip;]","og_url":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/","og_site_name":"Codoid","article_publisher":"https:\/\/www.facebook.com\/codoid.softwaretestingcompany","article_published_time":"2020-04-02T17:29:04+00:00","article_modified_time":"2022-02-04T18:28:29+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/Behave_Before_Scenario_Example_using_Selenium_Blog.jpg","type":"image\/jpeg"}],"author":"admin","twitter_card":"summary_large_image","twitter_creator":"@codoid","twitter_site":"@codoid","twitter_misc":{"Written by":"admin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/#article","isPartOf":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/"},"author":{"name":"admin","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586"},"headline":"Behave Before Scenario Example using Selenium","datePublished":"2020-04-02T17:29:04+00:00","dateModified":"2022-02-04T18:28:29+00:00","mainEntityOfPage":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/"},"wordCount":449,"commentCount":0,"publisher":{"@id":"https:\/\/codoid.com\/#organization"},"articleSection":["Selenium Testing","Fixed","Blog"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/","url":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/","name":"Behave Before Scenario Example using Selenium - Codoid","isPartOf":{"@id":"https:\/\/codoid.com\/#website"},"datePublished":"2020-04-02T17:29:04+00:00","dateModified":"2022-02-04T18:28:29+00:00","breadcrumb":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/behave-before-scenario-example-using-selenium\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codoid.com\/"},{"@type":"ListItem","position":2,"name":"Behave Before Scenario Example using Selenium"}]},{"@type":"WebSite","@id":"https:\/\/codoid.com\/#website","url":"https:\/\/codoid.com\/","name":"Codoid","description":"","publisher":{"@id":"https:\/\/codoid.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codoid.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/codoid.com\/#organization","name":"Codoid - Software Testing Company","url":"https:\/\/codoid.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codoid.com\/#\/schema\/logo\/image\/","url":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/09\/Codoid_Software_Testing_Company_Logo.png","contentUrl":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/09\/Codoid_Software_Testing_Company_Logo.png","width":500,"height":500,"caption":"Codoid - Software Testing Company"},"image":{"@id":"https:\/\/codoid.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/codoid.softwaretestingcompany","https:\/\/twitter.com\/codoid","https:\/\/www.instagram.com\/codoid.softwaretestingcompany\/","https:\/\/www.linkedin.com\/company\/codoid-qacompany","https:\/\/www.pinterest.com\/codoid9282\/codoid-software-testing-company\/","https:\/\/www.youtube.com\/channel\/UCCmOTDQgcf4W8oo22mBMUYA"]},{"@type":"Person","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586","name":"admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codoid.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/4d33f9036c9d60684b9478f738779823?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4d33f9036c9d60684b9478f738779823?s=96&d=mm&r=g","caption":"admin"},"sameAs":["https:\/\/codoid.com"]}]}},"_links":{"self":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2809"}],"collection":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/comments?post=2809"}],"version-history":[{"count":7,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2809\/revisions"}],"predecessor-version":[{"id":11734,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2809\/revisions\/11734"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media\/5440"}],"wp:attachment":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media?parent=2809"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/categories?post=2809"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/tags?post=2809"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}