Open topic with navigation

# LeanFT Grid code samples (.NET SDK)

The following code samples show how to lock a LeanFT grid node that provides the capabilities required for your test, and then perform operations on that node.

Note: Supported from LeanFT 14.51.

In this topic:

## Lock a node as part of the launch browser step

Call BrowserFactory.Launch with a browser description that includes environment capabilities. This call locks a node with the described environment, launches the browser on the node, and returns a browser handle.

    [TestMethod]
    public void Grid_LaunchBrowser_BrowserDescWithEnvDesc()
    {
           string nodeOsType = "Windows";                   // Set this to the node OS type
           string nodeOsVersion = "7";                      // Set this to the node OS version
           BrowserType browserType = BrowserType.Chrome;    // Set this to the requested browser type
           string browserVersion = ">57";                   // Set this to the requested browser version

           // Create a browser description object that includes also the required environment
           var browserDesc = new BrowserDescription();
           browserDesc.Set("type", browserType);
           browserDesc.Set("version", browserVersion);
           browserDesc.Set("ostype", nodeOsType);
           browserDesc.Set("osversion", nodeOsVersion);

           // Launch the browser (the required DesktopEnvironment will be locked for you behind the scenes)
           var browser = BrowserFactory.Launch(browserDesc);

           // Your test code goes here

           browser.Close();
    }

Back to top

## Lock a node and then launch a browser on that node

Call EnvironmentFactory.Get with an environment description. This calls locks a node with the described environment and returns an environment handle.

Call BrowserFactory.Launch with a browser description and the environment handle to launch the browser on the locked node.

    [TestMethod]
    public void Grid_LaunchBrowser_OnSpecifiedEnv()
    {
           string nodeOsType = "Windows";               // Set this to the node OS type
           string nodeOsVersion = "7";                  // Set this to the node OS version
           BrowserType browserType = BrowserType.Chrome;    // Set this to the requested browser type
           string browserVersion = ">57";               // Set this to the requested browser version

           // Create an environment description object that includes required environment capabilities
           var envDesc = new EnvironmentDescription();
           envDesc.Set("ostype", nodeOsType);
           envDesc.Set("osversion", nodeOsVersion);

           // Lock a matching desktop environmentvar env = EnvironmentFactory.Get(envDesc);

           // Create a browser description object that includes only browser description
           var browserDesc = new BrowserDescription();
           browserDesc.Set("type", browserType);
           browserDesc.Set("version", browserVersion);

           // Launch the browser on the locked desktop environment
           var browser = BrowserFactory.Launch(browserDesc, env);

           // Your test code goes here

    	browser.Close();
    }

Back to top

## Lock a node and perform operations on a desktop application

Call EnvironmentFactory.Get with an environment description. This calls locks a node with the described environment and returns an environment handle (env).

Call env.Describe to specify the calculator application on the node, and then press its buttons.

    [TestMethod]
    public void Grid_Describe_ClickOnDescribedObject()
    {
           string nodeOsType = "Windows";       // Set this to the node OS type
           string nodeOsVersion = "7";          // Set this to the node OS version

           // Create capabilities map, convert it to environment description and lock a matching desktop environment
           var capabilities = new Dictionary<string, object>();
           capabilities.Add("osType", nodeOsType);
           capabilities.Add("osversion ", nodeOsVersion);
           var env = EnvironmentFactory.Get(EnvironmentDescription.FromDictionary(capabilities));

           // Describe a window hierarchy under the desktop environment
           var calculator = env.Describe<IWindow>(new WindowDescription
    	      {
    	 	IsChildWindow = false,
    		IsOwnedWindow = false,
    		WindowClassRegExp = "CalcFrame",
    		WindowTitleRegExp = "Calculator"
    	      });

           // Your test code goes here.
           // e.g. this is how you click on a button in the desktop application:
           var btn = calculator.Describe<IButton>(new ButtonDescription
    	      {
    		  NativeClass = "Button",
    		  Text = "",
    	  	  WindowId = 135
    	      });

           // Click
           btn.Click();
    }

Back to top

## Lock a node and perform operations using the node's mouse

Call EnvironmentFactory.Get with an environment description. This calls locks a node with the described environment and returns an environment handle (env).

Use env.Mouse to gain access to the node's mouse and use it for operations.

    [TestMethod]
    public void Grid_DeviceReplay_Mouse()
    {
           string nodeOsType = "Windows";  // Set this to the node OS type
           string nodeOsVersion = "7";     // set this to the node OS version

           // Create capabilities map, convert it to environment description and lock a matching desktop environment
           var capabilities = new Dictionary<string, object>();
           capabilities.Add("osType", nodeOsType);
           capabilities.Add("osversion ", nodeOsVersion);
           var env = EnvironmentFactory.Get(EnvironmentDescription.FromDictionary(capabilities));

           // Get Mouse to perform low-level mouse actions
           var mouse = env.Mouse;

           // Your test code goes here.
           // e.g. this is how you right-click on point:
           mouse.Click(new Point(80,80), MouseButton.Right);
    }

Back to top