Selenium Test Script using NUnit

Last updated on Nov 24 2021
Swaminathan M

Table of Contents

Selenium Test Script using NUnit

To write a Selenium test script using NUnit, follow the below process:
For our testing purpose, we’ll perform Login operation on the Facebook Application.

Steps Actions Input Expected Result
1. Open the Google Chrome browser. The Google Chrome browser should be opened.
2. Navigate to the Facebook login page. https://www.facebook.com The Facebook login page must be displayed.
3. Identify the username text box and pass the worth Username=xyz11@gmail.com The username text box should be identified, and the value should be entered.
4. Identify the password text box and pass the worth Password=####### The Password text box should be identified, and the value should be entered.
5. Identify the Log in button and click on it. The Login button should be identified and clicked.
6. Close the browser. The browser should be closed.

We are creating our test script step by step to offer you an entire understanding of how we write the test script with the assistance of the NUnit framework in Visual studio.
Step1
To access the Google Chrome browser, we’ll create the IWebDriver as a worldwide variable.
Here the sample code:

1. //create the reference for the browser
2. IWebDriver driver = new ChromeDriver();
Note:
Global variable: the worldwide variable is said outside any function which is accessible to all or any functions throughout the program.
We will divide our code into different parts while writing the code using NUnit with the assistance of NUnit test methods like:
• For Open the browser: Initialize
• Perform browser operations: ExecuteTest
• Close the browser: EndTest
1. The syntax for the NUnit test method:
2. public void MethodName()
Example:
1. public void Initialize()
2. {
3. //open the browser
4. }
5. public void ExecuteTest()
6. {
7. //perform browser operations
8. }
9. public void EndTest()
10. {
11. //close the browser
12. }
Step2
In the next step, we'll navigate to the given URL under the Initialize() method.
Here the sample code:
1. public void Initialize()
2. {
3. //navigate to URL
4. driver.Navigate().GoToUrl("https://www.facebook.com/");
5. //Maximize the browser window
6. driver.Manage().Window.Maximize();
7. Thread.Sleep(2000);
8. }

Step3
In this step, we’ll identify the username text box of the Facebook login page under the ExecuteTest() method.
Follow the below process:
• Right-click on the username text box, and click on on the Inspect Element as we will see within the below screenshot:

Page 3 Image 1
Inspect

• The developer tool window will open having all the precise codes, which are utilized in the event of the username text box.

Page 3 Image 2 1
developer tool window

• Copy the worth of its id attribute as “email” and paste it into the code.
Here the sample code:

1. public void ExecuteTest()
2. {
3. //identify the username text box
4. IWebElement ele = driver.FindElement(By.Id("email"));
5. //enter the username value
6. ele.SendKeys("xyz11@gmail.com");
7. Thread.Sleep(2000);
8. Console.Write("username value is entered \n");

Step4
After that, we’ll identify the password text box of the Facebook login page, so for this follow the below process:
• Right-click on the password text box, and click on on the Inspect Element option within the pop-up menu as we will see within the below image:

Page 4 Image 3 1
password

• It will launch a developer tool window that contains all the precise codes, which are utilized in the event of the password text box.

Page 5 Image 4
developer tool window

• Copy the worth of its name attribute as “pass” and paste it into the code.

Here the sample code:
1. //identify the password text box
2. IWebElement ele1 = driver.FindElement(By.Name("pass"));
3. //enter the password value
4. ele1.SendKeys("########");
5. Console.Write("password is entered");

Step5
Once we identified the username or password textbox, we’ll find the Log in button and perform click operation.
• Right-click on the Log in button, and choose the Inspect option from the given pop-up menu as we will see within the below image:

Page 6 Image 5
Log in button

• The developer tool window are going to be launched with having all the precise codes, which are utilized in the event of the log in button as we will see within the below screenshot:

Page 6 Image 6 1
developer tool window

• Copy the worth of its Id attribute “u_0_b” and paste it into the code.

Here the sample code:
1. //click on the Log in button
2. IWebElement ele2 = driver.FindElement(By.Id("u_0_b"));
3. ele2.Click();
4. Thread.Sleep(3000);
5. Console.Write("login button is clicked")

Step7
In the last step of our test script, we’ll close the browser under the EndTest() method.

Here, the sample code for closing the browser:
1. public void EndTest()
2. {
3. //close the browser
4. driver.Close();
5. }
After combining all steps together, our script will appear as if this:
1. using OpenQA.Selenium;
2. using OpenQA.Selenium.Chrome;
3. using System;
4. using System.Collections.Generic;
5. using System.Linq;
6. using System.Text;
7. using System.Threading.Tasks;
8. using System.Threading;
9. namespace SeleniumTest
10. {
11. class Sample1
12. {
13. //create the reference for the browser
14. IWebDriver driver = new ChromeDriver();
15. public void Initialize()
16. {
17. //navigate to URL
18. driver.Navigate().GoToUrl("https://www.facebook.com/");
19. //Maximize the browser window
20. driver.Manage().Window.Maximize();
21. Thread.Sleep(2000);
22. }
23. public void ExecuteTest()
24. {
25. //identify the username text box
26. IWebElement ele = driver.FindElement(By.Id("email"));
27. //enter the username value
28. ele.SendKeys("xyz11@gmail.com");
29. Thread.Sleep(2000);
30. Console.Write("username value is entered");
31. //identify the password text box
32. IWebElement ele1 = driver.FindElement(By.Name("pass"));
33. //enter the password value
34. ele1.SendKeys("########");
35. Console.Write("password is entered");
36. //click on the Login button
37. IWebElement ele2 = driver.FindElement(By.Id("u_0_b"));
38. ele2.Click();
39. Thread.Sleep(3000);
40. Console.Write("login button is clicked");
41. }
42. public void EndTest()
43. {
44. //close the browser
45. driver.Close();
46. }
47. }
48. }
Note: within the above code, use your Facebook id at place: xyz11@gmail.com and password: ########

Execute the Test Script

To run the test scripts, follow the below process:
• Go to the Test within the toolbar and choose Test Explorer as we will see within the below screenshot:

Page 9 Image 7
Test Explorer

• The Test Explorer window will appear on the screen, to look at all the available tests, we’ve to create our solution.

Page 9 Image 8 1
create solution

• To build the answer , attend Build and choose Build Solution from given pop-up menu as we will see within the below screenshot:

Page 9 Image 9
Build Solution

• Once we’ve through with the Build solution, we cannot see our Tests on the test explorer window because we didn’t add the NUnit attributes.
• The NUnit provides some inbuilt attributes, but here we are using SetUp, Test, and TearDown
• So, we’ll add Test attribute in our code a bit like below:
Note:
Test attribute: it’s wont to mark the methods as callable from the NUnit test runner.
1. [Test]
2. public void ExecuteTest()

• While adding the Test attribute, it’ll give the below error.
• To overcome this error, we’ll use the reference of the NUnit framework as we will see within the below image:

Page 10 Image 10
NUnit framework

• Now, we attend the Test Explorer and Build the answer once more , and that we will get the ExecuteTest on the Test Explorer
• After that, we’ll Run the chosen test as we will see within the below image:

Page 10 Image 11
ExecuteTest

• After running the test script, it’ll ready to open the browser as we will see within the below screenshot:

Page 11 Image 12
browser

• The test will Fail because we’ve not called the Initialize method anywhere within the code.
• So we’d like to inform NUnit Framework by adding two more attributes called SetUp and TearDown into the code.

Page 13 Image 14
NUnit Framework

Note:
SetUp: The SetUp attribute is employed to spot a way to be called immediately; each test runs.
TearDown: this attribute is employed to spot a way that’s to be called immediately after each test is executed. And this method is bound to be called, albeit an exception is thrown.
Our final code will appear as if this after adding all three attributes:

1. using OpenQA.Selenium;
2. using OpenQA.Selenium.Chrome;
3. using System;
4. using System.Collections.Generic;
5. using System.Linq;
6. using System.Text;
7. using System.Threading.Tasks;
8. using System.Threading;
9. using NUnit.Framework;
10. namespace SeleniumTest
11. {
12. class Sample1
13. {
14. //create the reference for the browser
15. IWebDriver driver = new ChromeDriver();
16. [SetUp]
17. public void Initialize()
18. {
19. //navigate to URL
20. driver.Navigate().GoToUrl("https://www.facebook.com/");
21. //Maximize the browser window
22. driver.Manage().Window.Maximize();
23. Thread.Sleep(2000);
24. }
25. [Test]
26. public void ExecuteTest()
27. {
28. //identify the username text box
29. IWebElement ele = driver.FindElement(By.Id("email"));
30. //enter the username value
31. ele.SendKeys("xyz11@gmail.com");
32. Thread.Sleep(2000);
33. Console.Write("username value is entered \n");
34. //identify the password text box
35. IWebElement ele1 = driver.FindElement(By.Name("pass"));
36. //enter the password value
37. ele1.SendKeys("########");
38. Console.Write("password is entered \n");
39. //click on the Login button
40. IWebElement ele2 = driver.FindElement(By.Id("u_0_b"));
41. ele2.Click();
42. Thread.Sleep(3000);
43. Console.Write("login button is clicked \n");
44. }
45. [TearDown]
46. public void EndTest()
47. {
48. //close the browser
49. driver.Close();
50. }
51. }
52. }

• After that, once more , do the build solution and run the chosen test.
• Our test got passed within the Test Explorer window as we will see within the below screenshot:

Page 13 Image 14 1
test  passed

• And the above test script will launch the Google Chrome browser and automate all the test scenarios as we will see within the below screenshot:

Page 14 Image 15
browser

• Or if we would like to ascertain the output, we’ll pass some value and see the output by clicking on the output link.
• And we will see the output for all the operations that we perform on the console as we will see within the below screenshot:

Page 14 Image 16
output

So, this brings us to the end of blog. This Tecklearn ‘Selenium Test Script using NUnit’ blog helps you with commonly asked questions if you are looking out for a job in Selenium and Automation Testing. If you wish to learn Selenium and build a career in Automation Testing domain, then check out our interactive, Selenium Certification Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

Selenium Certification Training

Selenium Certification Training

About the Course

Tecklearn’s Selenium Certification Training enables you to master the complete Selenium suite. The Selenium Training is designed to train developers and manual testers to learn how to automate web applications with a robust framework, and integrate it within the DevOps processes of an organization. This Selenium Certification Training will also help you master important concepts such as TestNG, Selenium IDE, Selenium Grid, Selenium WebDriver, etc. Get hands-on experience on widely used automation frameworks such as Data-Driven Framework, Keyword-Driven Framework, Hybrid Framework, and Behavior Driven Development (BDD) Framework. Throughout this online Instructor-led Selenium Certification Training, you will be working on real-life industry use cases.

Why Should you take Selenium Certification Training?

• The average salary of a Selenium Test Automation Engineer is $94k per year – Indeed.com.
• Automation Testing Market is expected to grow at a Compound Annual Growth Rate (CAGR) of 18.0% in the next three years.
• Global software testing market to reach $50 billion by 2020 – NASSCOM. Selenium tool supports more browsers and languages than any other testing tool.

What you will Learn in this Course?

Getting started with Selenium

• Introduction to Selenium testing
• Significance of automation testing
• Comparison of Manual and Automation Testing
• Installation of Java JDK, JRE and Eclipse

Setting the environment in Eclipse for Selenium

• Java Introduction
• Creating a Java function and executing
• Concepts of Java
• Properties File
• Reading Data from Excel File
• Database Connection
• Hands On

Advantages of Selenium automation testing

• Selenium Features
• Concept of Selenium Integrated Development Environment
• Understanding of the Selenium IDE features
• Addition of Script assertions and general commands
• Deploying the first Selenium Script
• Sample project IDE
• Recording Selenium test case
• Hands On

Selenium Web driver Automation

• Architecture of Selenium Web Driver
• Download and installation
• Creating a Java function using Selenium and execution
• Hands On

Deploying Web Drivers for scripting

• Getting the HTML source of Web Element
• Table and Form Elements
• Firebug extension and Fire Path installation
• Advance User Interactions and Cross Browser Testing
• Hands On

Deep dive into Selenium Web Driver

• Action Commands
• Web Table / Date Picker
• How to Implement Switching Commands in WebDriver
• Alerts
• Frames
• Hands On

Switching Operations in WebDriver using Window

• Selenium Webdriver Wait
• Implicit wait, Explicit wait
• Deploying searching elements using the link text, name, using XPath
• Calendar
• Hands On

Introduction to TestNG Framework

• Introduction to TestNG
• Advantages of TestNG
• Installing TestNG on Eclipse
• Rules to write TestNG
• TestNG Features
• Annotations
• Grouping
• Sequencing: Prioritization and Dependency
• Enable/Disable a test case
• Parameterization: Using Xml file and DataProvider
• Parallel Testing & Cross Browser Testing
• TestNG Report: HTML Report, Console Report, XML Report

JUnit Operations and Test Framework

• Annotations, Methods in JUnit
• Junit Test Suites, ANT Build and JUNIT reporting
• Types of Test Automation Framework
• Module Based Testing Framework
• Data Driven Testing Framework
• Keyword Driven Testing Framework
• Hybrid Driven Testing Framework
• How to implement Testing Framework in Project

Object Repository

• Understanding of Object Repository
• Learning sample scripts using object repository
• Page Object Modelling
• Page Factory

JavaScript Functions

• Autosuggestion
• Headless Browser
• Sikuli
• XPath

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "Selenium Test Script using NUnit"

Leave a Message

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