Search code examples
c#unit-testingseleniumnullargumentnullexception

VS Selenium - System.ArgumentNullException: text cannot be null Parameter name: text


I'm writing (Selenium) Unit Tests and am running into a NULL issue. This code was previously running and working, but now is failing and not entering values into the needed fields. Can anyone give me some suggestions as to what may be wrong?

Unit Test Objective: Navigate Web Service UI and enter a value that should display results.

DMPage.cs

using System;
using OpenQA.Selenium;

namespace Framework.WebServicePages
{
    public class DMPage
    { 
        public static void GoTo()
        {
            WebServiceDriver.Instance.Navigate().GoToUrl("URL");
        }

        public static void Wait()
        {
            WebServiceDriver.Instance.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
        }

        public static string IsAt
        {
            get
            {
                var title = WebServiceDriver.Instance.FindElement(By.XPath(""));
                if (title != null)
                    return title.GetAttribute("Title");
                return string.Empty;
            }
        }

        public static ValueCommand EnterValue(string valuenum)
        {
            return new ValueCommand();
        }
    }

    public class ValueCommand
    {
        private readonly string valuenum;

        public ValueCommand()
        {
            this.valuenum = valuenum;
        }

        public void SearchValue()
        {
            var inputValue = WebServiceDriver.Instance.FindElement(By.XPath(""));
            inputValue.SendKeys(valuenum);

            var submitValue = WebServiceDriver.Instance.FindElement(By.XPath(""));
            submitValue.Click();
        }
    }
}

DMTests.cs

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ValueMedAutomation.WebServicePages;

namespace Tests.WebServiceTestScripts
{
    [TestClass]
    public class DMTests
    {

        [TestInitialize]
        public void Init()
        {
            WebServiceDriver.Initialize();
        }

        [TestMethod]
        public void DM_Meds()
        {
            DMPage.GoTo();
            DMPage.Wait();
            DMPage.EnterValue("123456789").SearchValue();
            DMPage.Wait();

            Assert.AreEqual(DMPage.IsAt, "TEST", "Value failed.");
        }

        [TestCleanup]
        public void Cleanup()
        {
            WebServiceDriver.Close();
        }

    }
}

Errors After Running Unit Test

  1. Message: Test method Tests.WebServiceTestScripts.DMTests.DM_Med threw exception:
    System.ArgumentNullException: text cannot be null Parameter name: text
  2. StackTrace:
    RemoteWebElement.SendKeys(string text)
    ValueCommand.SearchValue()
    DMTests.DM_Med()
  3. CS1717 Assignment made to same variable; did you mean to assign something else?

Errors When Debugging

The following lines break when running Debug:

L41    this.valuenum = valuenum;

L47    inputValue.SendKeys(valuenum);

Solution

  • You forgot to pass in string valuenum in the constructor of the ValueCommand class:


    Within DMPage class:

    public static ValueCommand EnterValue(string valuenum)
    {
        return new ValueCommand(valuenum);
    }
    

    Within ValueCommand class:

    public ValueCommand(string valuenum)
    {
        this.valuenum = valuenum;
    }