Protractor with C# for Angular application testing | Code Example

Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would. Protractor is built on top of WebDriverJS, which uses native events and browser-specific drivers to interact with your application as a user would.

Protractor supports Angular-specific locator strategies, which allows you to test Angular-specific elements without any setup effort on your part. You no longer need to add waits and sleeps to your test. Protractor can automatically execute the next step in your test the moment the webpage finishes pending tasks, so you don’t have to worry about waiting for your test and webpage to sync.

Step 1: Create a new MSTest Project in Visual Studio.

Step 2: Add NuGet Package References of “Protractor”.

Step 3: Add NuGet Package References of “ChromeDriver”

Step 4: Below is Code Example, Lets get started.

Code Example | Protractor with C#

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using Protractor;
using OpenQA.Selenium.Chrome;

namespace ProtractorCSharp
{
    [TestClass]
    public class ProtractorCSharp
    {
        private static IWebDriver Driver;
        private static NgWebDriver ngDriver;
        private static String base_url = "https://softwaretestingblog.com";

        [AssemblyInitialize]
        public static void AssemblyInit(TestContext context)
        {
            var options = new ChromeOptions();
            options.AddArgument("start-maximized");

            Driver = new ChromeDriver(options);
            Driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(40);
            ngDriver = new NgWebDriver(Driver);
            ngDriver.IgnoreSynchronization = true;

            Driver.Navigate().GoToUrl(base_url);
            ngDriver.Url = Driver.Url;
        }

        [TestMethod]
        public void ProtractorTestUsingCSharp()
        {
            ngDriver.FindElement(NgBy.Binding("BindingName")).Click();
            ngDriver.FindElement(NgBy.Model("ModelName")).SendKeys("Text");
            ngDriver.FindElement(NgBy.Repeater("Value")).Click();
            ngDriver.FindElement(NgBy.SelectedOption("Options")).Click();
            ngDriver.FindElement(By.ClassName("ClassName")).SendKeys("Text");
            ngDriver.FindElement(By.TagName("Tag")).Click();
            ngDriver.FindElement(By.CssSelector("#CssSelector")).Click();
            ngDriver.FindElement(By.Id("Id")).Click();
        }

        [AssemblyCleanup]
        public static void AssemblyTearDown()
        {
            ngDriver.Close();
            ngDriver.Quit();
        }
    }
}

This is a open-source international community to improve software testing practices, send your post here

Advertisements

Leave a Reply