Home United States dc3 United States software in english pdf Most Complete MSTest Framework Tutorial Using. Net Core

Most Complete MSTest Framework Tutorial Using. Net Core

385
0
SHARE

Learn about the MSTest Framework using .Net Core in Selenium C# and the aspects related to cross browser testing and parallel testing in this MSTest tutorial.
Join the DZone community and get the full member experience. With the advent of programming languages like Python, Ruby on Rails, etc., there is thinking amongst the developer community that the C language is losing relevance. Strikingly, C is still considered a dominant programming language for system programming as it provides optimized machine instructions for any type of provided input. Not only C, the languages that are derived from C, i.e., C# and C++, are also embraced with arms wide open by the developer community. As far as unit testing/automation testing using C# is concerned, there are some frameworks like NUnit, xUnit. Net, MSTest Framework, etc., to save the day. Selenium is a widely popular test automation framework as it is compatible with C# and other popular programming languages. It is predominantly used for cross-browser testing as it enables interactions with the web elements (present on a web page) via Selenium WebDriver. It supports all popular web browsers: Firefox, Chrome, Safari, Internet Explorer, Microsoft Edge, etc. We have covered the Selenium framework and Selenium WebDriver architecture in more detail in our earlier blogs. You should definitely check them out in case you are relatively new to the Selenium framework. Test frameworks compatible with Selenium can be used to perform automated browser testing or cross-browser testing of website/web applications. In this Selenium C# tutorial, we have a look at the MSTest framework, which is the default test framework for testing. Net applications. We will also cover the aspects related to cross-browser testing and parallel testing with MSTest. Below are some of the sub-topics that are covered as a part of this MSTest tutorial. MSTest framework for Selenium automated testing is the default test framework that comes along with Visual Studio. In the earlier days, it started as a command-line tool for executing tests. It is also referred to as Visual Studio Unit Testing Framework; however, the name MSTest is more synonymous with the developers. The MSTest framework provides the necessary tools to verify & validate your source code. The framework recognizes tests via the different attributes/annotations under which the test code is present. Some of the popular attributes are [TestInitialize], [TestMethod], [TestCleanup], etc. We will have a detailed look at each of these attributes in further sections of this MSTest tutorial. Since the MSTest framework comes pre-bundled with Visual Studio, developers who use the Visual Studio IDE for development & testing prefer the MSTest framework over other test frameworks like NUnit, xUnit. Net, etc. However, the choice and preference will also depend on the type & complexity of the project. For development, we are making use of the Community Edition of Visual Studio 2019; the same can be downloaded from the official download site of Visual Studio. You also have the option to choose from the Professional or Enterprise edition, though the choice should solely depend upon your project requirements. The necessary packages are selected for the installation since the packages occupy a good amount of disk space after installation. Once the installation is complete, we have to install the required packages for executing the tests based on the MSTest framework. Every test framework requires the corresponding Test Adapter to be installed, as the test adapter is an enabler for executing the test code. As this Selenium C# tutorial is focused on the MSTest framework, you have to install MSTest Adapter to run MSTests. In order to install the required packages, perform the following steps: In case you have not created an MSTest Test Project, you can still install the MSTest framework using the ‘NuGet Package Manager Console commands’ or using the NuGet GUI. At the time of this blog, the latest version of the MSTest framework and MSTest Adapter was 2.2.1, respectively. You can download and install the MSTest framework by either of the two methods, as shown below: a. PM (Package Manager) commands from the ‘‘NuGet Package Manager Console” – For executing commands from the NuGet PM console, go to ‘Tools’ -> ‘NuGet Package Manager’ -> ‘Package Manager Console.’ To install the packages, we make use of the Install-Package command with the required as the argument to the command. The installation screenshots are shown below: You can confirm whether the packages are installed or not by executing the command Get-Package on the Package Manager Console. Shown below is the command execution output: b. NuGet Package Manager – To open the NuGet Package Manager, go to ‘Tools’ -> ‘NuGet Package Manager’ -> ‘Manage NuGet Packages for Solution.’ In the Browse section, search for the following packages and click Install: In this MSTest tutorial, any reference to the MSTest framework refers to MSTest v2, i.e., version 2.2.1. The M2Test V2 framework was introduced a couple of years back with powerful features that are ideal for using the MSTest framework for cross-browser testing. Some of the striking features of MSTest V2 are below: The MSTest V2 portfolio comprises the framework, adapter, templates, etc. Due to the implementation of these features, MSTest V2 is preferred by developers who are performing automated browser testing using Selenium C#. We highly recommend using the latest version of Visual Studio, i.e., VS 2019, but in case you are using an old version that comes with MSTest V1, you can migrate to MSTest V2 by following these simple steps: The primary role of annotations in a test framework is to inform the underlying framework on how the source code should be interpreted. Once the code is compiled, a DLL (Dynamic Link Library) is generated, which can be executed using the console or a GUI. Like the NUnit framework annotations, the MSTest framework also adds annotations between brackets before the method is declared. For example, the basic setup for automated browser testing can be done using the [TestInitialize] annotation. The resources allocated during initialization can be freed using the method implemented under the [TestCleanup] annotation. In this MSTest tutorial, we will cover the most frequently used MSTest framework annotations: There are other annotations like [DynamicData], [DataSource], etc., but these are not much relevant in the context of cross-browser testing or automated browser testing. To demonstrate the usage of annotations in the MSTest Framework, we will create a simple test code with two test cases. The code under each Annotation only has a Console. WriteLine to trace the execution flow. Shown below is the execution log: Implementation under [ClassInitialize] and [ClassCleanup] annotations are respectively called once before and after executing the methods in the class. Similarly, implementation under [TestInitialize] and [TestCleanup] annotations are respectively called once before and after executing each test of the class. During the process of web product development, you would have encountered situations where some functionalities do not work as expected on certain browsers (or browser versions) or devices, or operating systems. Hence, it is necessary to test the product functionalities on different browsers, particularly on those browsers which your customers will use to access your website/web application. From a development standpoint, you have to ensure that changes for browser compatibility do not hamper the design aspects of the product, i.e., the UI and UX. MSTest can be used with the Selenium framework to perform cross-browser testing and ensure that the behavior of the product is uniform across different browsers and devices. Cross-browser testing can be performed by installing the Selenium WebDriver of the browser on which you want to do the testing. Selenium WebDriver for popular web browsers like Chrome, Firefox, Internet Explorer, Microsoft Edge, etc. can be downloaded from the locations mentioned below: If the WebDriver executable is present in the location where the web browser executable is present, you need not specify the path when the WebDriver instance is created in the source code. To demonstrate the MSTest framework usage with local Selenium WebDriver, we will take the example of a simple To-Do app. The test scenario for the Sample ToDo app will be: Implementation Create a new project of the type MSTest Test Project (. Net Core). The default test framework for this project is MSTest. Hence you need not perform the package installation for the MSTest framework again. As we will be using the Selenium framework, you have to install the Selenium WebDriver. To install the Selenium WebDriver packages for Visual Studio, you have to execute the following commands on the Package Manager Console: In case you require in-depth information about Selenium WebDrivers and web element locators, we recommend you to check out the Locators In Selenium WebDriver tutorial. To make the code more modular, we have separated the task of creating Selenium WebDrivers for different web browsers in a separate file, i.e., WebDriverInfra.cs. All it has is the implementation for the creation of WebDriver instances of Chrome, Firefox, Edge, etc. The code structure is as shown below: FileName – src/Test/LocalMsTest.cs Code Walkthrough Step 1 – To start with, the entire Test Code is added under an [TestClass] annotation. Step 2 – The ‘LambdaTest ToDo app’ is executed against two web-browsers, i.e., Firefox and Chrome. Hence, the [DataRow] attribute is used to set the values of the parameters (Firefox and Chrome) of the test. Step 3 – The method, i.e., NavigateToDoApp, which contains the actual test implementation, is added under the [TestMethod] annotation. The test method is iteratively called for each browser type, i.e., Chrome and Firefox, and once the browser instance is created, the required test operations are performed. The first two items on https://lambdatest.github.io/sample-todo-app/ are checked and a new item with the text ‘Yey, Let’s add it to list’ is added to the list. The required web elements on the page are located using the Inspect Tool functionality of the respective browser. Below is the execution snapshot on the Firefox browser and a screenshot from Test Explorer on Visual Studio. Parallelism in the MSTest framework can be achieved by using the In-assembly Parallel feature of MSTest V2. Below are the two ways in which it can be implemented: 1. Using Annotations Parallelism is possible at the Class level or the Method level. The syntax is as shown below: In the above syntax: 2. Using RunSettings In case your project has multiple test assemblies which you want to parallelize, you can achieve parallelism by creating a file named.runsettings at the root of the solution. The example demonstrated as a part of Local Testing can be converted to Parallel Test by simply adding the following line in src/Test/LocalMsTest.cs Parallel testing on local infrastructure can hit a roadblock if you want to test your website/web application across ‘N’ different browsers, browser versions, operating systems, and devices. You obviously cannot invest endlessly in setting up the test infrastructure since it is neither scalable nor economical. Not performing thorough cross-browser testing might result in a buggy experience if the customer accesses your product from an untested web browser. It reduces the overall test coverage as well. A feasible solution is to use a cloud-based cross-browser testing platform like the LambdaTest, where tests are executed on a Remote Selenium Grid. Using LambdaTest, cross-browser tests can be executed on 2000+ different combinations of browsers, operating systems, and devices. Porting the existing implementation to remote Selenium Grid requires changes in the infrastructure-related code. Once you create an account on LambdaTest, you should make a note of the user name and access-key from Profile Section as that combination is used for accessing the remote Selenium Grid on LambdaTest. The Automation Dashboard can be used to view all your text logs, screenshots, and video recording for your entire Selenium test. The desired browser and platform capabilities are generated using LambdaTest capabilities Generator. For example, below are the capabilities for Safari on the macOS Mojave platform: For demonstrating the usage of LambdaTest and the effectiveness of Parallel test execution on a Remote Selenium Grid, we will implement the below three test cases: Test Case 1 – LamdaTest Sample To-Do App. Browsers on which cross-browser testing is performed are: Test Case 2 and 3 – Google Search for LambdaTest. Both the test cases are the same, but the execution will be performed on different web browsers. Implementation FileName – 2_LT_Parallel_Test.cs Code Walkthrough Step 1 – OpenQA. Selenium. Remote package/namespace is imported as RemoteWebDriver class is defined in it. Step 2 – The LambdaTest credentials are passed for accessing the remote Selenium Grid. Refer to our documentation on LambdaTest Authentication Credentials to learn how to integrate LambdaTest in your test scripts. Step 3 – Browser and Device capabilities are generated using the LambdaTest capabilities generator, and the same is passed to the Remote WebDriver API. Step 4 – The current plan on LambdaTest defines how many tests can be executed in parallel on the Remote Selenium Grid. For demo purposes, we have used 5 Parallel tests. Therefore, we have set the scope of Parallelism to MethodLevel with Workers to 5, i.e., five threads/tests. Step 5 – The initialization code is implemented as a part of the [TestInitialize] attribute. It will be called only once during the initialization of the tests. Step 6 – As the browser capabilities on LambdaTest accept input parameters as browser-name, browser-version, platform-name, the combination is passed via [DataRow] attribute. These are passed as the input parameters to each test-case. Each test is executed iteratively till all the test combinations as a part of [DataRow] are exhausted. The remote web driver is created as a part of the test case as it uses these three parameters to create a browser instance on the target platform. The rest of the implementation is specific to what needs to be achieved as a part of the test case, i.e., Operations on LambdaTest ToDo app or Google Search for ‘LambdaTest’ on specific browser/OS combinations. As seen in the execution snapshot procured from Visual Studio, the test case is performed for 4 different browser/OS combinations, which were passed in the [DataRow] attribute. You can visit https://automation.lambdatest.com/logs/?testID=&build= to check the execution status of the test. As seen in the screenshot from LambdaTest automation, three tests were executed in Parallel. Below is the snapshot of successful test execution from Test Explorer on Visual Studio and Automation Tab (Test Status = Completed) on LambdaTest. In this Selenium C# tutorial, we had a look at the MSTest framework – the default test framework that comes along with Visual Studio. MSTest V2 can be used for cross-browser testing as it supports parallelism, which is an important aspect when it comes to automated browser testing. The advantage of MSTest V2 is that it has cross-platform support and is highly extensible. Annotations/attributes simplify the division of the various sections involved in a test case, i.e., initialization, test case logic, de-initialization. The MSTest framework can also be used to define the behavior of the tests as it supports data-driven tests. When it comes to cross-browser testing, cloud-based testing can opt over local cross-browser testing. It is more scalable and helps you test the product thoroughly on different (browser + platform + device) combinations. Published at DZone with permission of Himanshu Sheth. See the original article here. Opinions expressed by DZone contributors are their own.