.NET Plugin Installation

Agent

To install the agent, you must unzip the folder containing the agent code.

The folder structure will be the following:

agent/
├── bin  : contains startup scripts and dependencies (DLLs)
├── conf : contains configuration file AgentConf.json
└── log  : default location for the log files

You must ensure that:

  • The user running the Agent must have read/write access to this folder
  • The following command line is executed if the port between the Agent and the Controller is closed:
 netsh http add urlacl url=http://+:8098/ sddl=D:(A;;GX;;;S-1-1-0) 
  • Note that the port used by the Agent is choosen randomly by default. You can force the Agent to use a specific port (and a specific adress) by adding one of the following options to the ...\Agent\conf\AgentConf.json:

"agentPort":7777,

"agentUrl":"http://machine:7777",

You can then start the agent with the script ...\bin\startAgent.bat. The new Agent will appear automatically on the Controller

1518625007821-376.png1518625286932-295.png

Controller

An additional Java plugin is needed in order to execute .NET keywords. This jar file (dotnet-plugin-X.Y.Z.jar) should have been provided to you with the rest of the Agent.

Adding this jar file into the .../controller/lib/ will allow you to create the new type of keywords:

1518684839238-509.png

Keyword Project

.NET Solution

To create new keywords for the .NET agent with Visual Studio Solution, you must first create a new Class Library (.NET Framework) project in Visual Studio:

1518621191693-818.png

This type of project will allow you to manage the dependencies and build of your project. Such project will look like that:

1518687031641-617.png

You will then be able to add a new keywords by adding to your function the [Keyword] annotation, as in this extract from the full example:

 [Keyword]
  public void gotoGoogle()
  {
       IWebDriver driver = getDriver(); driver.Navigate().GoToUrl("https://google.ch/search?q=" + input.GetValue("search"));
  }

Dependencies

Dependencies will be needed for using the Step API in your projects. The following actions will have to be performed on your new Visual Studio Solution:

  • First, the following nugget packages must be added to your new Project/Solution:
    • Selenium.Support version 3.3.0 (if creating Selenium based keywords)
    • Selenium.WebDriver version 3.3.0 (if creating Selenium based keywords)

1518621032170-842.png

1518621331044-403.png

  • If the following error message is displayed for one of those libraries, the included version is probably incorrect:

System.IO.FileLoadException: Could not load file or assembly '..., Version=X.Y.Z,...' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference

  • In addition, the following DLL files from Step are needed for building your new keywords library:
  1. Do an "Add reference":

1518621828861-807.png

  1. Browse for the DLLs:

1518621920481-515.png

  1. Select the files ScriptDev.dll,StepApi.dll and Newtonsoft.Json.dll located in the ...\Agent\bin\ folder of the Agent. You must now have two new references:

1518623243866-366.png

  1. If you have additional dependencies, you can add them in the same way.

Deployment

Keyword DLL

Deployment of your keyword library can be done by dropping the DLL file into the controller file system (using the same path specified during the Keyword creation (C:\Users\Administrator\source\repos\TestSelenium\TestSelenium\bin in our example). The DLL will then be uploaded on the Agents when running the keywords.

Library DLLs

If your keyword library need additional dependencies (like the Selenium driver DLL), the following additional steps will have to be done:

  • Copy your additional DLLs to the ...\agent\bin\ folder of the Agent
  • Restart the Agent

Recommended practices

NUnit

You can unit test your new keywords using the NUnit package. This will save you time as you will be able to debug your keywords without having to build and deploy your DLL to the controller.
To do that, you will first have to install the NUnit nugget dependencies (as described in this section). This will allow you to use [Test] annotated functions.

In order to have the most realistic test, you should use the ScriptRunner object which will simulate the execution of your keywords like Step does. See the following example for more information.

Example

The following .NET class is a simple example of two keywords using the Selenium driver. This demonstrate the usage of the basic API functions:

  • First, the new keywords are defined using the [Keyword] annotation
  • The getDriver and initDriver functions show the usage of a session. session.get and session.get allows to pass .NET objects during the live cycle of your virtual user. The Webdriver is typical of the usage of the session, as this driver contains the state of the user and have to be unique per virtual user. Note that we are using a wrapper class for the Driver so that the browser will be properly closed
  • The gotoGoogle function shows how to pass input values from the test plan to your keywords using input.GetValue
  • The testKeywords functions shows how to unit test your keywords using the ScriptRunner object

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using StepApi;
using NUnit.Framework;
using ScriptDev;

namespace STEP
{
    public class DriverWrapper : Closeable
    {
        private IWebDriver driver;

        public DriverWrapper(IWebDriver driver) => this.driver = driver;

        public void close() => this.driver.Close();

        public IWebDriver getDriver() => this.driver;
    }

    public class TestSeleniumNet : AbstractScript
    {

        protected IWebDriver getDriver()
        {
            return ((DriverWrapper)session.get("chromedriver")).getDriver();
        }

 [Keyword]
        public void initDriver()
        {
            ChromeOptions options = new ChromeOptions();

            string[] optionsList = { "start-maximized", "headless" };
            options.AddArguments(optionsList);

            IWebDriver driver = new ChromeDriver(@"C:\Step\installers\", options, TimeSpan.FromSeconds(30));

     session.put("chromedriver", driver);
        }

 [Keyword]
        public void gotoGoogle()
        {
            IWebDriver driver = getDriver();

            driver.Navigate().GoToUrl("https://google.ch/search?q=" + input.GetValue("search"));
        }

 [Test]
        public void testKeywords()
        {
     ScriptRunner runner = new ScriptRunner(typeof(TestSeleniumNet).Assembly);
     runner.run("initDriver");
     runner.run("gotoGoogle", "{'search':'exense'}");
        }
    }
}

Other API Functionality

Output and Attachments

In order to pass you data into Keywords output as JSON payload or attachment, you can use following code as an example :

Here the keyword class where we add to the output :

  • some text via the key / value pair with key "myOutputContent"
  • some text that will be available into the attachment file called "myAttachmentContent.txt"
class KW000_SandboxKeyword : AbstractScript
    {
        [Keyword]
       public void KW000_SandboxKeywordMethod()
        {
            output.add("myOutputContent", "Here the output content");
            output.addAttachment(AttachmentBuilder.generateAttachmentFromByteArray(System.Text.Encoding.Unicode.GetBytes("[attachmentContent1, attachmentContent2, attachmentContent3]"), "myAttachmentContent.txt"));
        }     
    }

In order to retrieve the output informations from your development environment, you can use the following code to call the keyword and display the output :

public void TestKW000()
        {
            ScriptRunner runner = new ScriptRunner(typeof(KW000_SandboxKeyword).Assembly);
            OutputMessage output = runner.run("KW000_SandboxKeywordMethod");

            Console.WriteLine(output.payload.ToString());
            Console.WriteLine(System.Text.Encoding.Unicode.GetString(AttachmentBuilder.Base64Decode(output.attachments[0].hexContent)));
        }

Once you execute this method, the output will be displayed as :

1519813116183-648.png

Then once you execute your Keyword within Step, it will display the output message and an attachment file that you can download by clicking on the attachment icon :

1519814702037-590.png

Tags:
Created by Dorian Cransac on 2018/02/14 14:15
     
Copyright © exense GmbH
v1.0