{"id":551,"date":"2019-12-04T17:24:28","date_gmt":"2019-12-04T11:54:28","guid":{"rendered":"http:\/\/http:\/\/artoftesting.com\/\/?p=551"},"modified":"2020-01-07T14:45:01","modified_gmt":"2020-01-07T09:15:01","slug":"beanshellscripting","status":"publish","type":"post","link":"https:\/\/artoftesting.com\/beanshellscripting","title":{"rendered":"BeanShell Scripting in JMeter"},"content":{"rendered":"\n<p>Before starting with beanShell scripting demo, let&#8217;s first understand &#8211; What is a BeanShell?<br>A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">BeanShell Scripting in JMeter example<\/h3>\n\n\n\n<p>In this example, we will open the search engine Bing, write a search term and fetch top 5 links from the search results.<br><br>Step.1.\u00a0I hope you are aware of the usual JMeter scripting, so we won&#8217;t go in deep and assume that script to open bing and write search term is already scripted. As shown in the image below, we have a transaction controller &#8220;Launch bing&#8221; and &#8220;Get all results links&#8221;. These transaction controllers will have their respective HTTP requests.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"628\" height=\"144\" src=\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/bean1.jpg\" alt=\"\" class=\"wp-image-552\" srcset=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/bean1.jpg 628w, https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/bean1-300x69.jpg 300w\" sizes=\"auto, (max-width: 628px) 100vw, 628px\" \/><\/figure>\n\n\n\n<p><strong>Step.2.<\/strong>\u00a0First of all, we will run the script and get text response in the view result tree and create a regular expression to fetch link, for more information about regular expression extractor, please refer to our post on\u00a0Correlation in JMeter.<br><br>Step.3.\u00a0The next step would be to create the Regexp Extractor that will be added as child of the HTTP request under the &#8220;Get all result links&#8221; controller.<br><br>Step.4.\u00a0Add BeanShell Script element as a child and write the following code-<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">keyword = \"buddha quotes\";\nlinks = vars.get(\"links\");\nf = <strong>new<\/strong> FileOutputStream(\"result.csv\",<strong>true<\/strong>);\np = <strong>new<\/strong> PrintStream(f); \n<strong>this<\/strong>.interpreter.setOut(p);\nprint(keyword +  \",\"  + links);\nf.close();<\/pre>\n\n\n\n<p>Check the image below for reference &#8211;<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"594\" height=\"381\" src=\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/bean2.jpg\" alt=\"\" class=\"wp-image-553\" srcset=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/bean2.jpg 594w, https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/bean2-300x192.jpg 300w\" sizes=\"auto, (max-width: 594px) 100vw, 594px\" \/><\/figure>\n\n\n\n<p>The code is self-explanatory and will create a result.csv file with links for the keyword &#8220;buddha quotes&#8221;. Also, we can parameterize the keyword and make the script more generic and dynamic.<br><br><\/p>\n\n\n\n<p> <em>That\u2019s all we have in this post, please share it with your friends and colleagues, comment below if you have any queries. Check out the complete JMeter tutorial below.<\/em> <\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p><a href=\"http:\/\/artoftesting.com\/jmeter-tutorial\">JMeter Tutorial<\/a><\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Before starting with beanShell scripting demo, let&rsquo;s first understand &ndash; What is a BeanShell?A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled. BeanShell Scripting in JMeter example In this example, we will open the search engine Bing, write a search term and &#8230; <a title=\"BeanShell Scripting in JMeter\" class=\"read-more\" href=\"https:\/\/artoftesting.com\/beanshellscripting\" aria-label=\"Read more about BeanShell Scripting in JMeter\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":1583,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-jmeter"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v25.0 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Beanshell Scripting in JMeter with Sample Script - ArtOfTesting<\/title>\n<meta name=\"description\" content=\"Beanshell scripting in JMeter - A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/artoftesting.com\/beanshellscripting\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Beanshell Scripting in JMeter with Sample Script - ArtOfTesting\" \/>\n<meta property=\"og:description\" content=\"Beanshell scripting in JMeter - A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/artoftesting.com\/beanshellscripting\" \/>\n<meta property=\"og:site_name\" content=\"ArtOfTesting\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/facebook.com\/artoftesting\" \/>\n<meta property=\"article:published_time\" content=\"2019-12-04T11:54:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-01-07T09:15:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"700\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Kuldeep Rana\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@theartoftesting\" \/>\n<meta name=\"twitter:site\" content=\"@theartoftesting\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kuldeep Rana\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#article\",\"isPartOf\":{\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting\"},\"author\":{\"name\":\"Kuldeep Rana\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5\"},\"headline\":\"BeanShell Scripting in JMeter\",\"datePublished\":\"2019-12-04T11:54:28+00:00\",\"dateModified\":\"2020-01-07T09:15:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting\"},\"wordCount\":279,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/artoftesting.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#primaryimage\"},\"thumbnailUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg\",\"articleSection\":[\"JMeter\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/artoftesting.com\/beanshellscripting#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting\",\"url\":\"https:\/\/artoftesting.com\/beanshellscripting\",\"name\":\"Beanshell Scripting in JMeter with Sample Script - ArtOfTesting\",\"isPartOf\":{\"@id\":\"https:\/\/artoftesting.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#primaryimage\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#primaryimage\"},\"thumbnailUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg\",\"datePublished\":\"2019-12-04T11:54:28+00:00\",\"dateModified\":\"2020-01-07T09:15:01+00:00\",\"description\":\"Beanshell scripting in JMeter - A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.\",\"breadcrumb\":{\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/artoftesting.com\/beanshellscripting\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#primaryimage\",\"url\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg\",\"contentUrl\":\"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg\",\"width\":700,\"height\":400,\"caption\":\"beanshell scripting in jmeter\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/artoftesting.com\/beanshellscripting#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/artoftesting.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Performance Testing\",\"item\":\"https:\/\/artoftesting.com\/category\/performance-testing\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"JMeter\",\"item\":\"https:\/\/artoftesting.com\/category\/performance-testing\/jmeter\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"BeanShell Scripting in JMeter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/artoftesting.com\/#website\",\"url\":\"https:\/\/artoftesting.com\/\",\"name\":\"ArtOfTesting\",\"description\":\"A Beginners Guide to Testing\",\"publisher\":{\"@id\":\"https:\/\/artoftesting.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/artoftesting.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/artoftesting.com\/#organization\",\"name\":\"ArtOfTesting\",\"url\":\"https:\/\/artoftesting.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png\",\"contentUrl\":\"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png\",\"width\":400,\"height\":60,\"caption\":\"ArtOfTesting\"},\"image\":{\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/facebook.com\/artoftesting\",\"https:\/\/x.com\/theartoftesting\",\"https:\/\/www.linkedin.com\/groups\/4797819\/\",\"https:\/\/in.pinterest.com\/artoftesting\/\",\"https:\/\/www.youtube.com\/channel\/UCQ9PUVenvvyrUdDQ9yKn31Q\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5\",\"name\":\"Kuldeep Rana\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/artoftesting.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g\",\"caption\":\"Kuldeep Rana\"},\"description\":\"Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Beanshell Scripting in JMeter with Sample Script - ArtOfTesting","description":"Beanshell scripting in JMeter - A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/artoftesting.com\/beanshellscripting","og_locale":"en_US","og_type":"article","og_title":"Beanshell Scripting in JMeter with Sample Script - ArtOfTesting","og_description":"Beanshell scripting in JMeter - A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.","og_url":"https:\/\/artoftesting.com\/beanshellscripting","og_site_name":"ArtOfTesting","article_publisher":"https:\/\/facebook.com\/artoftesting","article_published_time":"2019-12-04T11:54:28+00:00","article_modified_time":"2020-01-07T09:15:01+00:00","og_image":[{"width":700,"height":400,"url":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg","type":"image\/jpeg"}],"author":"Kuldeep Rana","twitter_card":"summary_large_image","twitter_creator":"@theartoftesting","twitter_site":"@theartoftesting","twitter_misc":{"Written by":"Kuldeep Rana","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/artoftesting.com\/beanshellscripting#article","isPartOf":{"@id":"https:\/\/artoftesting.com\/beanshellscripting"},"author":{"name":"Kuldeep Rana","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5"},"headline":"BeanShell Scripting in JMeter","datePublished":"2019-12-04T11:54:28+00:00","dateModified":"2020-01-07T09:15:01+00:00","mainEntityOfPage":{"@id":"https:\/\/artoftesting.com\/beanshellscripting"},"wordCount":279,"commentCount":0,"publisher":{"@id":"https:\/\/artoftesting.com\/#organization"},"image":{"@id":"https:\/\/artoftesting.com\/beanshellscripting#primaryimage"},"thumbnailUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg","articleSection":["JMeter"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/artoftesting.com\/beanshellscripting#respond"]}]},{"@type":"WebPage","@id":"https:\/\/artoftesting.com\/beanshellscripting","url":"https:\/\/artoftesting.com\/beanshellscripting","name":"Beanshell Scripting in JMeter with Sample Script - ArtOfTesting","isPartOf":{"@id":"https:\/\/artoftesting.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/artoftesting.com\/beanshellscripting#primaryimage"},"image":{"@id":"https:\/\/artoftesting.com\/beanshellscripting#primaryimage"},"thumbnailUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg","datePublished":"2019-12-04T11:54:28+00:00","dateModified":"2020-01-07T09:15:01+00:00","description":"Beanshell scripting in JMeter - A BeanShell is light-weight scripting language. It has Java like syntax and being a scripting language it is interpreted instead of compiled.","breadcrumb":{"@id":"https:\/\/artoftesting.com\/beanshellscripting#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/artoftesting.com\/beanshellscripting"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/beanshellscripting#primaryimage","url":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg","contentUrl":"https:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/BeanShell-Scripting-in-JMeter.jpg","width":700,"height":400,"caption":"beanshell scripting in jmeter"},{"@type":"BreadcrumbList","@id":"https:\/\/artoftesting.com\/beanshellscripting#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/artoftesting.com\/"},{"@type":"ListItem","position":2,"name":"Performance Testing","item":"https:\/\/artoftesting.com\/category\/performance-testing"},{"@type":"ListItem","position":3,"name":"JMeter","item":"https:\/\/artoftesting.com\/category\/performance-testing\/jmeter"},{"@type":"ListItem","position":4,"name":"BeanShell Scripting in JMeter"}]},{"@type":"WebSite","@id":"https:\/\/artoftesting.com\/#website","url":"https:\/\/artoftesting.com\/","name":"ArtOfTesting","description":"A Beginners Guide to Testing","publisher":{"@id":"https:\/\/artoftesting.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/artoftesting.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/artoftesting.com\/#organization","name":"ArtOfTesting","url":"https:\/\/artoftesting.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/","url":"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png","contentUrl":"http:\/\/artoftesting.com\/wp-content\/uploads\/2019\/12\/Artoftesting_logo.png","width":400,"height":60,"caption":"ArtOfTesting"},"image":{"@id":"https:\/\/artoftesting.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/facebook.com\/artoftesting","https:\/\/x.com\/theartoftesting","https:\/\/www.linkedin.com\/groups\/4797819\/","https:\/\/in.pinterest.com\/artoftesting\/","https:\/\/www.youtube.com\/channel\/UCQ9PUVenvvyrUdDQ9yKn31Q"]},{"@type":"Person","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/7846d06225b52c778d160becf65996a5","name":"Kuldeep Rana","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/artoftesting.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cb5979a4b81ca7739c75080e473fad391a8665364e72abaddec9002dd4553326?s=96&d=mm&r=g","caption":"Kuldeep Rana"},"description":"Kuldeep is the founder and lead author of ArtOfTesting. He is skilled in test automation, performance testing, big data, and CI-CD. He brings his decade of experience to his current role where he is dedicated to educating the QA professionals."}]}},"_links":{"self":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/comments?post=551"}],"version-history":[{"count":0,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/posts\/551\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/media\/1583"}],"wp:attachment":[{"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/media?parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/categories?post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/artoftesting.com\/wp-json\/wp\/v2\/tags?post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}