现在的位置: 首页 > 综合 > 正文

Using NUnit to Execute Selenium WebDriver Tests

2018年02月14日 ⁄ 综合 ⁄ 共 4039字 ⁄ 字号 评论关闭

In this article, I will explain the basics of using NUnit as the unit/regression testing tool for Web browsers. NUnit is developed using C#. In the example, you will see methods using certain attributes from NUnit Framework, which uses an attribute based programming
model. A great thing about NUnit is that its very easy to learn and allows us to abstract away our tests and enable us to focus on the test code.

Prerequisites

  • I am assuming you have Visual Studio (atleast Visual Express C#) installed and basic understanding of C#.
  • NUnit is a unit-testing framework that supports .NET languages. For further reading, refer to its documentation.
    If you do not have NUnit installed, navigate to the download section, download and install
    the most current .msi file.

Creating a Test in Visual Studio

This section has been described in detail in the previous article: Introduction to Selenium. To
create the test using the WebDriver API and NUnit attributes, following the steps below.

  1. Start Visual Studio
  2. File > New Project > Class Library > Name: SeleniumNUnitTest
  3. Add the following references
    • NUnit.Framework (NUnit.Framework.Dll)
    • WebDriver (WebDriver.Dll)
    • WebDriver.Support (WebDriver.Support.Dll)
  4. Finally, paste the below code to the newly created project
namespace SeleniumNUnitTest
{
    using NUnit.Framework;
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using System;
 
    [TestFixture]
    public class GmailTests
    {
        private IWebDriver driver;
 
        public GmailTests() { }
 
        [SetUp]
        public void LoadDriver() 
        { 
            Console.WriteLine("SetUp"); 
            driver = new InternetExplorerDriver(); 
        }
 
        [Test]
        public void Login()
        {
            Console.WriteLine("Test");
 
            driver.Navigate().GoToUrl("http://gmail.com");
            driver.FindElement(By.Id("Email")).SendKeys("test");
            driver.FindElement(By.Id("Passwd")).SendKeys("test");
            driver.FindElement(By.Id("Passwd")).Submit();
 
            Assert.True(driver.Title.Contains("Inbox"));
        }
 
        [TearDown]
        public void UnloadDriver() 
        { 
            Console.WriteLine("TearDown"); 
            driver.Quit(); 
        }
    }
 
    [TestFixture, Description("Tests Google Search with String data")]
    public class GoogleTests
    {
        private IWebDriver driver;
 
        public GoogleTests() { }
 
        [SetUp]
        public void LoadDriver() { driver = new InternetExplorerDriver(); }
 
        [TestCase("Google")]   // searchString = Google
        [TestCase("Bing")]     // searchString = Bing
        public void Search(string searchString)
        {
            // execute Search twice with testdata: Google, Bing
 
            driver.Navigate().GoToUrl("http://google.com");
            driver.FindElement(By.Name("q")).SendKeys(searchString);
            driver.FindElement(By.Name("q")).Submit();
 
            Assert.True(driver.Title.Contains("Google"));
        }
 
        [TearDown]
        public void UnloadDriver() { driver.Quit(); }
    }
}

I have used the below attributes to create my test code.

  • [TestFixture] – marks a class that contains tests and, optionally, setup or teardown methods
  • [SetUp] – used to provide a common set of functions that are performed just before each test method is called
  • [Test] – marks a method inside a TestFixture class as a test
  • [TestCase] – serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method
  • [TearDown] – used to provide a common set of functions that are performed after each test method is run

In order to verify if the navigation is successful for both TextFixtures (test classes), an assertion Assert.True is used.

Save and compile (build) the solution. If you chose Class Library as the project type, a DLL would be created in the bin/Release or bin/Debug folder of the project. If the project type is other than a Class Library, an .EXE file would be created. Load either
the DLL or the EXE into NUnit by selecting File > Open Project.

Executing Tests from NUnit

Once the project is been loaded to NUnit from File > Open Project, you will see 3 tests in the tests TreeView: Login, Search(“Google”) and Search(“Bing”).

You see 3 tests is because GmailTests.Login will run once whereas GoogleTests.Search will run twice with Google and Bing as the input parameters (notice the TestCase attribute). NUnit will execute methods corresponding to the number ofTestCase attributes
defined. Also see the Values and Repeat attributes.

All settings are complete. Click Run to execute all tests. Once the run session is complete, the following output log should be displayed.

In the example, the GmailTests class uses Console.WriteLine in LoadDriver, Login and UnloadDriver methods. This will be printed to the Text Output tab of NUnit
as shown below:

By reading the output from the print log, it is also verified that NUnit will run the SetUp method first, followed by Test and TearDownmethods.

http://relevantcodes.com/using-nunit-to-execute-selenium-webdriver-tests/

抱歉!评论已关闭.