{"id":2819,"date":"2020-04-24T10:35:14","date_gmt":"2020-04-24T10:35:14","guid":{"rendered":"https:\/\/codoid.com\/running-selenium-scripts-using-bazel\/"},"modified":"2022-02-04T18:32:27","modified_gmt":"2022-02-04T18:32:27","slug":"running-selenium-scripts-using-bazel","status":"publish","type":"post","link":"https:\/\/codoid.com\/selenium-testing\/running-selenium-scripts-using-bazel\/","title":{"rendered":"Running Selenium Scripts using Bazel"},"content":{"rendered":"<p>Bazel is one of the most widely used open source  build tool these days, the rationale behind  using Bazel is aplenty. Let\u2019s say you want to compile Java and Python projects into one build file, then Bazel would be the ideal choice. As a <a href=\"https:\/\/codoid.com\/\">QA company<\/a>, we use Maven, Gradle, and Grunt to compile &#038; kick-off automated test suites. Bazel is something new for us. In this blog article, you will learn how to run Selenium Scripts using Bazel.<\/p>\n<h3>Advantages of Bazel<\/h3>\n<p><b>1) Platform Independent:<\/b> You can run your Selenium scripts on Linux, macOS, Windows.<\/p>\n<p><b>2) Compiling Large Files:<\/b> Bazel can handle large files easily.<\/p>\n<p><b>3) Languages:<\/b> You can build and test C\/C++, Objective-C, Java, Python, and Bourne Shell scripts in one build file.<\/p>\n<h3>How to run Selenium Scripts using Bazel?<\/h3>\n<p>Bazel has its own build language. Using Bazel\u2019s predefined test rule, you can set automated test scripts in the build file irrespective of the programming languages used. We, as a <a href=\"https:\/\/codoid.com\/automation-testing-services\/\">test automation company<\/a>, explore new technologies with the intend to ease automation testing process. Bazel is so programmer friendly, even Selenium developers are using Bazel to test Selenium\u2019s features before deploying. In this blog article, we have used <b>Windows 10<\/b> and <b>Python<\/b> to show the code execution. Let\u2019s start from installation.<\/p>\n<h3>Prerequisite &#8211; Visual C++ Redistributable for Visual Studio 2015<\/h3>\n<p>Visual C++ Redistributable for Visual Studio 2015 is a prerequisite if you are installing Bazel on Windows 10. Use the following link to install the package &#8211; <a href=\"https:\/\/www.microsoft.com\/en-us\/download\/details.aspx?id=48145\" target=\"_blank\" rel=\"noopener noreferrer\">Visual C++ Redistributable for Visual Studio 2015<\/a><\/p>\n<h3>Bazel Installation on Windows 10<\/h3>\n<p><b>Step #1<\/b> &#8211; Download <b>bazel-<version>-windows-x86_64.exe<\/b> from the following link &#8211; <a href=\"https:\/\/github.com\/bazelbuild\/bazel\/releases\" target=\"_blank\" rel=\"noopener noreferrer\">Download Bazel<\/a><\/p>\n<p><b>Step #2<\/b> &#8211; Rename the downloaded file to bazel.exe<\/p>\n<p><b>Step #3<\/b> &#8211; Copy and paste the bazel.exe wherever you want.<\/p>\n<p><b>Step #4<\/b> &#8211; Set the bazel.exe path in PATH environment variable in Windows 10.<\/p>\n<p><b>Step #5<\/b> &#8211; Restart your PC<\/p>\n<h3>Create a Python Project<\/h3>\n<p>Step #1 &#8211; Create a Python project<\/p>\n<p>Step #2 &#8211; Create a directory called &#8216;scripts&#8217;<\/p>\n<p>Step #3 &#8211; Create a directory called &#8216;drivers&#8217;<\/p>\n<p>Step #4 &#8211; Download and paste the chromedriver.exe in drivers folder<\/p>\n<p>Step #5 &#8211; Create a python file and name it as test-1.py inside the scripts folder<\/p>\n<p>Step #6 &#8211; Paste the below snippet inside the test-1.py file<\/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\nfrom selenium import webdriver\r\nfrom bazel_tools.tools.python.runfiles import runfiles\r\nr = runfiles.Create()\r\ndriver = webdriver.Chrome(executable_path=r.Rlocation(&quot;test_suite\/drivers\/chromedriver.exe&quot;))\r\ndriver.get(&quot;https:\/\/codoid.com&quot;)\r\ndriver.quit()\r\n  <\/pre>\n<\/div>\n<blockquote><p><b>Note: <\/b>In line #4, the chromedriver executable path is set using Bazel\u2019s runfiles module. Because all the supporting &#038; testdata files for automated scripts need to be accessed using runfiles module. Maybe in future enhancements we will have a simple workaround instead of calling runfiles.<\/p><\/blockquote>\n<h3>Creating WORKSPACE file<\/h3>\n<p>Bazel identifies a directory as Bazel Workspace when it contains <b>WORKSPACE<\/b> file.<\/p>\n<p>Step #1 &#8211; Create WORKSPACE file without any extension<\/p>\n<p>Step #2 &#8211; Paste the below line to name the workspace<\/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\nworkspace(name = &quot;test_suite&quot;)\r\n  <\/pre>\n<\/div>\n<h3>Creating BUILD file<\/h3>\n<p>Bazel build file is being used to compile and run your test code. We, as a <a href=\"https:\/\/codoid.com\/software-testing-company\/\">software testing company<\/a>, use Maven POM.xml files to compile and run test code for Java projects. If you have test code base in different programming languages, then you can opt for Bazel to compile &#038; execute test code of different languages into a single build file. Let\u2019s see how to create a build file.<\/p>\n<p>Step #1: Inside the root directory, create a BUILD file without any extension.<\/p>\n<p>Step #2: Copy and paste the below snippet in <b>BUILD<\/b> file.<\/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\npy_binary(\r\n    name = &quot;test-1&quot;,\r\n    srcs = [&quot;scripts\/test-1.py&quot;],\r\n    srcs_version = &quot;PY2AND3&quot;,\r\n    data = [&quot;:drivers\/chromedriver.exe&quot;],\r\n    deps = [&quot;@bazel_tools\/\/tools\/python\/runfiles&quot;],\r\n)\r\n  <\/pre>\n<\/div>\n<h6>Code Explanation<\/h6>\n<p><b>Line #1:<\/b> py_binary is one of the Bazel&#8217;s rules. For Python, Bazel has two more rules &#8211; py_library &#038; py_test. Any Python related tasks can be defined using these three rules in the build file.<\/p>\n<p><b>Line #2:<\/b> You can name your tasks using &#8216;name&#8217; attribute.<\/p>\n<p><b>Line #3:<\/b> &#8216;srcs&#8217; attribute is used to mention source files inside the rule. You can also mention multiple source files.<\/p>\n<p><b>Line #5:<\/b> To launch the chrome browser, we need chromedriver.exe file. So we are copying the file inside the runfiles folder which can be found in bazel-out folder. Use &#8216;data&#8217; attribute to copy all the supporting files.<\/p>\n<p><b>Line #6:<\/b> To access runfiles in Python code, you need runfiles module from bazel_tools. Refer Line#2 in test-1.py file, you can understand why we are mentioning this dependency here.<\/p>\n<h3>Bazel Run<\/h3>\n<p>Now it is the time to run the script. You can run the rules from command prompt. Let&#8217;s see how to run our test-1 task.<\/p>\n<p><b>Clean<\/b> &#8211; bazel clean<\/p>\n<p><b>Run<\/b> &#8211; bazel run :test-1<\/p>\n<p>That&#8217;s it. You can see browser launch after running the above commands. We have uploaded the Python project in Github. Please refer it if you face any issues. <a href=\"https:\/\/github.com\/codoid-repos\/bazel-selenium-tests\" target=\"_blank\" rel=\"noopener noreferrer\">Full Code Download Link<\/a><\/p>\n<h3>In Conclusion<\/h3>\n<p>As a <a href=\"https:\/\/codoid.com\/automation-testing-services\/\">test automation company<\/a>, we manage and maintain automation test scripts in multiple programming languages. However, managing single build file for compiling test code from different programming languages is a daunting task and eventually a great value add for QA companies. In our subsequent blog articles, we will be publishing more about nuances of Python automation testing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Bazel is one of the most widely used open source build tool these days, the rationale behind using Bazel is aplenty. Let\u2019s say you want to compile Java and Python projects into one build file, then Bazel would be the ideal choice. As a QA company, we use Maven, Gradle, and Grunt to compile &#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":5067,"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>Running Selenium Scripts using Bazel - 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=\"Running Selenium Scripts using Bazel\" \/>\n<meta property=\"og:description\" content=\"Bazel is one of the most widely used open source build tool these days, the rationale behind using Bazel is aplenty. Let\u2019s say you want to compile Java and Python projects into one build file, then Bazel would be the ideal choice. As a QA company, we use Maven, Gradle, and Grunt to compile &#038; [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/\" \/>\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-24T10:35:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-02-04T18:32:27+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/selenium_bazel.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=\"5 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Running Selenium Scripts using Bazel - 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":"Running Selenium Scripts using Bazel","og_description":"Bazel is one of the most widely used open source build tool these days, the rationale behind using Bazel is aplenty. Let\u2019s say you want to compile Java and Python projects into one build file, then Bazel would be the ideal choice. As a QA company, we use Maven, Gradle, and Grunt to compile &#038; [&hellip;]","og_url":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/","og_site_name":"Codoid","article_publisher":"https:\/\/www.facebook.com\/codoid.softwaretestingcompany","article_published_time":"2020-04-24T10:35:14+00:00","article_modified_time":"2022-02-04T18:32:27+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/codoid.com\/wp-content\/uploads\/2020\/04\/selenium_bazel.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":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/#article","isPartOf":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/"},"author":{"name":"admin","@id":"https:\/\/codoid.com\/#\/schema\/person\/360ee1d38151acb7c746787fbfa8e586"},"headline":"Running Selenium Scripts using Bazel","datePublished":"2020-04-24T10:35:14+00:00","dateModified":"2022-02-04T18:32:27+00:00","mainEntityOfPage":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/"},"wordCount":909,"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\/running-selenium-scripts-using-bazel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/","url":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/","name":"Running Selenium Scripts using Bazel - Codoid","isPartOf":{"@id":"https:\/\/codoid.com\/#website"},"datePublished":"2020-04-24T10:35:14+00:00","dateModified":"2022-02-04T18:32:27+00:00","breadcrumb":{"@id":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codoid.com\/learn\/selenium-testing\/running-selenium-scripts-using-bazel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codoid.com\/"},{"@type":"ListItem","position":2,"name":"Running Selenium Scripts using Bazel"}]},{"@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\/2819"}],"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=2819"}],"version-history":[{"count":6,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2819\/revisions"}],"predecessor-version":[{"id":11739,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/posts\/2819\/revisions\/11739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media\/5067"}],"wp:attachment":[{"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/media?parent=2819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/categories?post=2819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codoid.com\/wp-json\/wp\/v2\/tags?post=2819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}