Listen to this blog
Bazel is one of the most widely used open source build tool these days, the rationale behind using Bazel is aplenty. Let’s 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 & 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.
Advantages of Bazel
1) Platform Independent: You can run your Selenium scripts on Linux, macOS, Windows.
2) Compiling Large Files: Bazel can handle large files easily.
3) Languages: You can build and test C/C++, Objective-C, Java, Python, and Bourne Shell scripts in one build file.
How to run Selenium Scripts using Bazel?
Bazel has its own build language. Using Bazel’s predefined test rule, you can set automated test scripts in the build file irrespective of the programming languages used. We, as a test automation company, 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’s features before deploying. In this blog article, we have used Windows 10 and Python to show the code execution. Let’s start from installation.
Prerequisite – Visual C++ Redistributable for Visual Studio 2015
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 – Visual C++ Redistributable for Visual Studio 2015
Bazel Installation on Windows 10
Step #1 – Download bazel-
Step #2 – Rename the downloaded file to bazel.exe
Step #3 – Copy and paste the bazel.exe wherever you want.
Step #4 – Set the bazel.exe path in PATH environment variable in Windows 10.
Step #5 – Restart your PC
Create a Python Project
Step #1 – Create a Python project
Step #2 – Create a directory called ‘scripts’
Step #3 – Create a directory called ‘drivers’
Step #4 – Download and paste the chromedriver.exe in drivers folder
Step #5 – Create a python file and name it as test-1.py inside the scripts folder
Step #6 – Paste the below snippet inside the test-1.py file
from selenium import webdriver from bazel_tools.tools.python.runfiles import runfiles r = runfiles.Create() driver = webdriver.Chrome(executable_path=r.Rlocation("test_suite/drivers/chromedriver.exe")) driver.get("https://codoid.com") driver.quit()
Note: In line #4, the chromedriver executable path is set using Bazel’s runfiles module. Because all the supporting & 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.
Creating WORKSPACE file
Bazel identifies a directory as Bazel Workspace when it contains WORKSPACE file.
Step #1 – Create WORKSPACE file without any extension
Step #2 – Paste the below line to name the workspace
workspace(name = "test_suite")
Creating BUILD file
Bazel build file is being used to compile and run your test code. We, as a software testing company, 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 & execute test code of different languages into a single build file. Let’s see how to create a build file.
Step #1: Inside the root directory, create a BUILD file without any extension.
Step #2: Copy and paste the below snippet in BUILD file.
py_binary( name = "test-1", srcs = ["scripts/test-1.py"], srcs_version = "PY2AND3", data = [":drivers/chromedriver.exe"], deps = ["@bazel_tools//tools/python/runfiles"], )
Line #1: py_binary is one of the Bazel’s rules. For Python, Bazel has two more rules – py_library & py_test. Any Python related tasks can be defined using these three rules in the build file.
Line #2: You can name your tasks using ‘name’ attribute.
Line #3: ‘srcs’ attribute is used to mention source files inside the rule. You can also mention multiple source files.
Line #5: 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 ‘data’ attribute to copy all the supporting files.
Line #6: 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.
Now it is the time to run the script. You can run the rules from command prompt. Let’s see how to run our test-1 task.
Clean – bazel clean
Run – bazel run :test-1
That’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. Full Code Download Link
As a test automation company, 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.