Unit Testing

NUnit Testing Tutorial | How To Write NUnit Tests In Selenium C#

In this video the presenter describes the running of selenium tests using the in-unit test framework in both local machine and in Lambda test platform. The UI based app test is going to be run in Lambda test via NUnit framework, assuming the app is already available via public network.

Setup & Pre-requisite

  • .NET 4 or above / .NET Core 2 or above
  • Visual studio 2017 or above
  • Selenium (dependency)
  • NUnit test framework (dependency)

In this lecture a simple selenium test will be written and run on the selenium grid server on the local machine and same test would be run on the Lambda test platform. So firstly the selenium dependency would be installed within the project. 

Local Machine Test

For this purpose the selenium will be searched in selenium web driver. Also the selenium server would be invoked for role node and hub so that test could be run on the selenium server. So the selenium hub is started by using the following command:

C:\driver > java –jar .\selenium-server-stanalone-3.141.59.jar –role hub

The jar –role hub is the hub command which is going to accept the request and java –jar is the node of selenium jar file. This going to execute operation which has been specified within out client code. It is clear that selenium dependency is added in the project and the selenium grid server is also running.

The next step is to run the following URL to run the test that would be written. 

http://192.168.1.33:4444/wd/hub

 Now a very simple test would be run here on the Lambda platform as a test sample and run it on the local machine and then on the Lambda test platform. The sample codes can be taken for the following language bindings:

  • Java
  • JavaScript
  • C#
  • Python
  • PHP
  • Ruby
  • Tesbo Framework

So for running the test sample the java code is copied that is already present at the Lambda test platform and pasted in the test platform but for running the selenium test also the selenium driver is required. 

 

// Click on First Check box

IWebElement firstCheckBox = driver.FindElement(By.Name(“li1”));

firstCheckBox.Click();

 

// Click on Second Check box

IWebElement secondCheckBox = driver.FindElement(By.Name(“li2”));

secondCheckBox.Click();

 

// Enter Item name

IWebElement textfield = driver.FindElement(By.Id(“sampletodotext”));

textfield.SendKeys(itemName);

 

// Click on Add button

IWebElement addButton = driver.FindElement(By.Id(“addbutton”));

addButton.Click();

 

// Verified Added Item name

IWebElement itemtext = driver.FindElement(By.XPath(“/html/body/div/div/div/ul/li[6]/span”));

String getText = itemtext.Text;

Assert.IsTrue(itemName.Contains(getText));

 

The browser is also required for this purpose as it would be executing the test. If the following test is simply run on the chrome browser then the chrome options would be needed to specified for the iweb driver. For this a private variable for iweb driver and the reference would be added as well by hitting control dot. Another reference would be added using the equivalent selenium and chrome options will be set up as follows:

public class Tests

private IWebDriver _driver;

[SetUp]

public void Setup ( )

CromeOptions … chromeOptions = new CromeOptions ( ) ;

Now the selenium grid needs to invoke the remote web driver and the following command is given in which the reference would be added along with the capability:

_driver = new RemoteWebDriver (new Uri( “http://localhost:4444/wd/hub”) , chromeOptions.ToCapabilities ( ));

In this way the actual test could be run in such a way on the chrome drive but since the underscore driver is specified and the copy based record is actually of driver. In this command the code is to be replaced with the driver from “_driver” so that the test could be run easily. So the new code would be as:

driver = new RemoteWebDriver (new Uri( “http://localhost:4444/wd/hub”) , chromeOptions.ToCapabilities ( ));

Now this is the test which is going to be run in the local machine eight now and as there is a setup then a teardown would also be performed here that would close the driver. The teardown would look like as:

[Tear-Down]

public void Cleanup ( )

Console.Writeline( “Selenium webdriver quit”);

driver.Quit ( );

Now the code is fully ready but before running the test it is needed to first navigate to the URL which is actually interested that has nothing to do with the Lambda test sample to do app as the URL is as follows:

driver.Manage ( ) .Window.Maximize ( );

driver.URL = http://lambdatest.github.io/sample-todo-app/”;

So now after running the test, the capability would be received and the browser would be launched and the selenium test has been  executed. This shows that the test is actually running fine within our local machine.

Lambda Test

Now the next step is to run the same test on the Lambda test platform. In order to do this procedure one needs to sign up for a free account within a Lambda test platform and start running the test or a concurrent session of two, that means you can run the same test parallel for two browsers in two different environment. There are different features that Lambda test offers at the site. 

In order to run the first test in the Lambda test platform the local test is needed to be connected to the Lambda test platform using the access keys which could be obtained in the profile settings and easily the access token can be grabbed. Adding to these features the information for how to connect the local machine code with the Lambda platform can also be acquired easily by going to the automation tab and after clicking the get started option and whole information could be enhanced there.

According to the information the local machine code could be connected with the Lambda platform with the help of:

  • Username
  • Access Key
  • Additional Desired Capabilities

Within this capability a lot of information is already available like the platform which is being run and the test for browser name, the version and the resolution and the build name and the network. Al the additional capabilities could be specified within the chrome option and the test could be run. These specific things can actually be get as template within the Lambda test in order to run it. The same test could also be run on different browsers not just on the chrome browser as the firefox and safari browser. Different platforms can also be utilized such as mac os bixer. 

The capability changes and the value of capability could be grabbed easily through the platform. If the person wants to have the information for the Lambda test without being signed in, the Lambda test capability generator could be searched on the browser and the same information would be provided as that of the Lambda platform including all the features. 

Now heading towards the coding in the test on the Lambda platform the URL needs to be changed from the previous test as it was being run on the local machine and new URL is stated as: 

http://USERNAME;ACCESS_KEY@hub.lambdatest.com/wd/hub

The first thing that is required to run the test is the capabilities and username and the access key properties. The code for the URL can also be copied from website as well and pasted in the Lambda test platform such as:

driver = new Uri ( http:// + username +”:” + accesskey + :@hub.lambdatest.com/wd/hub”) , capability

driver = new Uri (http://localhost:4444/wd/hub)  , chromeOptions. ToCapabilities ( ));

driver.Manager ( ) .Window.Maximize ( );

driver.URL = “https;//lambdatest.github.io/sample-todo-app/”;

This code would be commented for now from driver to (//driver) and the test would be commanded from the beginning. The first thing that was specified for the test is the capabilities and also the username and the access key properties. In this test a series of properties would be used which will be required to run the test. Following are the properties that would be required here:

private string ,,browser;

private string ,,version;

private string ,,os;

private string ,,name;

private string ,,ltUsername;

private string ,,ltAppKey;

These properties would be utilized to connect to the Lambda test platform and these values would be initialized from within the test using a constructor as follows:

//Constructor

public Tests (string browser, string version, string os, string name)

_browser = browser;

_version = version;

_os = os;

_name = name;

_ltUserName = “kartmcad”;

_ltAppKey = q/jaLB17csfaKQ7X6T5ZHS0m1XHoiyvIfb54ZOy6oj/pRxyAbym”;

Now the capabilities would be created now that are required for running the test on the Lambda test platform  which are as follows. The true is specified as the global capability because tis is a global property that is going to be assigned within the capability.

ChromeOptions chromeOptions = new ChromeOptions ( ) ;

chromeOptions.AddAdditionalCapability (“platform”, _os, true);

chromeOptions.AddAdditionalCapability (“version”, _version, true);

chromeOptions.AddAdditionalCapability (“name”, _name, true);

chromeOptions.AddAdditionalCapability (“build”, _ParallelTesting, true);

chromeOptions.AddAdditionalCapability (“user”, _ltUserName, true);

chromeOptions.AddAdditionalCapability (“accesskey”, _ltAppKey, true);

Now the particular ChromeOptions would now be set as the capability within the Lambda test URL. The URL would now be changed and added the username and access key in it and the new URL command would look like as:

//Lambda Test Connectivity

driver = new RemoteDriver (new Uri ( http:// + ltUserName +”:” + ltAppKey + :@hub.lambdatest.com/wd/hub”) , chromeOptions.ToCapabilities)

Now as it is all set to run the test on the Lambda Test platform. 

The information about the properties for the browser, version, os, name are all obtained from the test fixture of the end unit. This is the same test which can be used for the parallel test execution such as:

[TestFixture(“Chrome”, “88.0”, “Windows 10”, “Chrome87)]

The test fixture is present to run the test on different environments that have been connected with the Lambda test platform. using the username and the access token and the capabilities are also being run within the remote web driver that would be connected in Lambda test platform

Once the test has been started to execute if the lambda disk platform is seen that in the automation the new test is coming in which is the Parallel Testing which is currently being executed within the machine. By clicking that parallel test it shows that the chrome browser is being used and window 10 machine as well as it is super fast. 

By clicking the test results it shows that test has already been executed and all the steps have been recorded as well. This shows that the Lambda test can be executed in this simpler way being an easy test.

8 Comments

  1. After reading your article, it reminded me of some things about gate io that I studied before. The content is similar to yours, but your thinking is very special, which gave me a different idea. Thank you. But I still have some questions I want to ask you, I will always pay attention. Thanks.

  2. I am a website designer. Recently, I am designing a website template about gate.io. The boss’s requirements are very strange, which makes me very difficult. I have consulted many websites, and later I discovered your blog, which is the style I hope to need. thank you very much. Would you allow me to use your blog style as a reference? thank you!

  3. I am a student of BAK College. The recent paper competition gave me a lot of headaches, and I checked a lot of information. Finally, after reading your article, it suddenly dawned on me that I can still have such an idea. grateful. But I still have some questions, hope you can help me.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button