Most of us use PyCharm to automate the BDD tests using Behave using Python, however, it adds to the cost of test automation. The community version is also available for free, but we can’t run behave files through it. Visual Studio Code also supports Python. However, this blog explains how to use Visual Studio Code for Python + Behave BDD tests for FREE. PyCharm and VS Code are both excellent tools for writing Python code.
PyCharm Community edition and Visual Studio Code (VSCode) are both capable integrated development environments for Python coding. To conduct this evaluation, I chose the most frequently used IDE feature and compared the experience of PyCharm and VSCode.
Let’s see how can we use VSCode for “Python behave” Automation:
First, you should make sure that you have installed the Python extension. I recommend installing it because it will allow you to install libraries and run code. To run Python and Python behave in Visual Studio Code (VSCode), visit the Visual Studio Code Marketplace to get all the extensions you’ll need.
The following three extensions must be installed before proceeding to Python:-
Gherkin- for creating feature files
Pylance: This will install all libraries which are required to run Python
Python extension for VSCode
All of these extensions are available in the VSCode Marketplace, and you can install them manually.
Firstly, we have to create a launch.json file. From the “Run and Debug” option & this file can be generated.·
Go to the “Run and Debug” option, where you will see “Create a launch.json file,” and put the below code into that file.
{
"version": "0.2.0",
"configurations": [
{"name":"Python:Current File", // You can change this name as per your convenient
"type":"python",
"request":"launch",
"program":"${file}",
"console":"integratedTerminal",
"justMyCode":true
},
With this code, you will be able to create a “launch.json” file that will aid in running Python behavior in Visual Studio Code. As soon as you are done with the “launch.json” file, you need to add some libraries to the “settings.json” file, which will help you run the code through the command line. Open a settings JSON file by pressing (ctrl + shift + p) and selecting “Open Settings.” JSON is an option.
Add the below code to the “Settings.json” file. The code below will assist users in integrating with GitHub and running files via the command line.
The final line of code, “python.pythonpath,” will show the virtual environment path. For instructions on how to set up a virtual environment for this, see https://code.visualstudio.com/docs/python/environments Now, the user can go to the “feature file” that he/she wanted to run by selecting “Run” from the “Run and Debug” option.
Cloud Environment:
To automate an application in a cloud environment like (BrowserStack/SauceLabs), add the following code to the “Settings.json” file. Users will be able to run code in a cloud environment. Execution mode determines which cloud environment the user wishes to automate the application in, whether it is BrowserStack or SauceLabs. You can run your tests on the cloud environment of your choice by using the username and access key provided.
We’d all like to be able to navigate directly to that specific function or method from the feature file step, as we can in PyCharm (ctrl+click). This can be achieved by including the following code in Settings.json in VSCode: Your work in VS code will be faster and more flexible.
Pycharm is used to run a Python behave file, but many of us are aware that it’s not free, so we’ve found a solution with Visual Studio Code that will help people save money and run Python behave freely on the VSCode IDE.
Harish is an SDET with expertise in API, web, and mobile testing. He has worked on multiple Web and mobile automation tools including Cypress with JavaScript, Appium, and Selenium with Python and Java. He is very keen to learn new Technologies and Tools for test automation. His latest stint was in TestProject.io. He loves to read books when he has spare time.
There are many instances during test automation when you would download the files and then would want to verify the completeness of your files downloaded. The MD5 checksum verification is to validate the authenticity of the file so we can ensure that the received file is exactly the same as at the source. This blog explains what MD5 checksum is and also, how we can use it in test automation.
Let’s understand the need for MD5 checksum Verification for a file.
Different kinds of files are distributed on the network or any storage media at different destinations. So, there is a chance that the file can be corrupted because of a few missing bits during data transfer due to some different reasons. Below are a few factors that can cause this to happen, such as:
An interruption to the Internet or network connection.
Storage or space problems, including hard drive-related problems.
A corrupted disk or corrupted file.
A third party interfering with the transfer of data.
After receiving the file, we need to check if the file received is correct or incorrect. It also becomes necessary to verify that our copy of the file is authentic and does not contain any error by applying some sort of test.
We basically use the special key string for this file integrity test known as a checksum. Also, we can refer to checksum as a hash sum or a hash value, hash code, or simply a hash. The most famous and widely used checksum technique is nothing but the MD5 checksum.
the digest, MD5, or hash checksum has been extensively used in the software world to provide some assurance that an uploaded file arrived intact from its source
What is MD5?
The message-digest algorithm(MD5) is a cryptographic hash function whose main purpose is to verify that a file has not been altered.
MD5 hashes are 128 bits long and are usually displayed as their 32-digit hexadecimal equivalents. It doesn’t matter how large or small the file or text is.
As we learn, it is a 32-character hexadecimal number that is computed on a file. Various Checksum programs are used to generate checksum key strings from the files and verify the integrity of the files. Later by using that checksum string with the original ones shared by the file servers. The file servers often provide a pre-computed MD5, so that a user can compare the checksum of the downloaded file to it. There is a high probability that two files with the same MD5 checksum are the same.
How to calculate MD5 checksum for a file:
1. Calculate MD5 checksum for a file for windows:
With a command prompt, PowerShell command, or third-party applications like Hash Generator or MD5 Checksum Utility, you can generate a checksum for a file.
a.With the command prompt:
Basically, the command line tool is built-in into the Microsoft Windows 10 operating system, as a certificate service, which is “CertUtil”.
This command line offers a switch, “Hashfile”; We can generate a hash string. Here is a specific algorithm that we can use, such as MD5:
3. Calculate MD5 checksum for a file through automation using C# :
Here, you can calculate programmatically using .net, Java, Python, etc.
To calculate it for a file in C#, .net provides an inbuilt functionality for generating these hash functions, For that reason, we need to import the following NuGet package: https://www.nuget.org/packages/Security.Cryptography
System.Security.Cryptography.MD5
Firstly we need to instantiate the Message Digest Object.
The Compute Hash method of the instance returns the computed hash of the file (bypassing the file stream).
Later, we can convert it to hex, the Bit Converter allows you to represent it as a string for conversion.
So, below is an example code showing a method (GetMD5HashFromFile ()).
It accepts the file name along with the path and then returns the calculated checksum. Also, generates the checksum, After that, it converts into a hex string, and removes dashes. This is the typical format.
The hash string for the file returned by this method can be compared with the one provided by the file servers and checked if the file is altered or not.
private string GetMD5HashFromFile(string fileName)
using (var md5 = MD5.Create())
{
using (var stream = File.OpenRead(fileName))
{
var hash= md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", string.Empty);
}
}
You can call the above function wherever you need to get the checksum. For example:
As I have implemented and tested this in a test case automation, so, I believe this is a most simple and easier way to verify MD5 checksums for a file in automation using C#
Trupti is a Sr. SDET at SpurQLabs with overall experience of 9 years, mainly in .NET- Web Application Development and UI Test Automation, Manual testing. Having hands-on experience in testing Web applications in Selenium, Specflow and Playwright BDD with C#.
Every software application identifies, recognizes, and authenticates the user trying to access the system using some type of credentials (i.e Username and password). And once the user is verified/authenticated, that user is granted permission to access authorized resources.
So, while testing the software application for every test case, the first step we perform is the login step.
If the test suite consists of 100+ test cases, then the step of logging in to the application will be performed for each test case. You can imagine the unnecessary time that will consume for logging in to the application each time for all those 100+ test cases.
Can we find a way to reduce the time required for logging in to the application for every test case? or can we skip logging in to the software application altogether?
The playwright is an open-source test automation tool created by Microsoft. It is a cross-platform tool and has a set of features like Test Generator (codegen), Trace viewer, Inspector, etc, and the most important feature of Playwright which will help us to skip authentication in test automation is full isolation with browser context.
In addition, you can refer to the official document on playwright basic authentication.
First Off, we will have to login into the application via UI and reuse theauthentication state in different and multiple scenarios using the browser context.
Let’s see how we can do it.
First, we are going to create a GenerateSession.java class where all of our own logical and automated login steps will be implemented,
So, what we did here is, launched the browser and created a new page.
In BrowserContext browserContext = browser.newContext(); we have created a browser context. This browserContext will store our authentication state i.e it will store the session, cookies, local storage, etc.
As you can see, we created the browser context browserContext, but the question here is, where will this authentication state (browserContex) will be stored?
The best way to store this browser context in a .json file.
The playwright provides a method called storageState() which will helpus store the authentication state.
The above code snippet will store the authentication state, cookies, local storage, etc in json format in appLoginInfo.json file.
If the appLoginInfo.json file is not created then the playwright will create a new file with the provided name appLoginInfo.json at the provided location in setPath(Paths.get(“appLoginInfo.json”)));
If you have theappLoginInfo.json file already created, each time you execute the above snippet, a new authentication state with cookies and local storage will be overwritten in theappLoginInfo.json file.
Now, we are all set up with our authentication state, cookies, and local storage. Our next question is
How do we use that authentication state and skip login for each scenario?
We can use a Playwright to skip authentication scenarios. As we all know, Web applications use cookies and token-based authentications. These authentications are stored in the form of cookies or local storage. The playwright provides us with methods that can store these authentication state and then create a new browser context with the previously restored session, which in turn help us skip login/authentication for the scenarios.
Here, we will set the stored authentication state using the methods above to the new browser context created for each scenario we run.
The playwright provides us with a method newContext() which will create a new browser context.
And by using NewContextOptions(), we can add/set the stored authentication state in the appLoginInfo.json file.
Let’s look at how we can do it in the code snippet below.
Here, setStorageStatePath(Paths.get(“appLoginInfo.json”))will set the authentication state stored in appLoginInfo.json to the new browser context created in the previous step.
So, every time we execute the above code snippet, the browser context that is stored in appLoginInfo.json file will be set to the new browser context and the previous session will be restored.
Please refer to the methods listed below. I used these methods to skip authentication scenarios for one of the Salesforce apps.
In this way, Playwright helps us to store the authentication state, cookies, local storage, etc. And we can use this authentication state and set it to our new browser context and restore the previous session, which will in turn help us skip the login step for the n number of scenarios for a software application.
Apurva is a Test Engineer, with 3+ years of experience in Manual and automation testing. Having hands-on experience in testing Web as well as Mobile applications in Selenium and Playwright BDD with Java. And loves to explore and learn new tools and technologies.