VS Selenium - System.ArgumentNullException:text不能为null参数名称:text

问题描述 投票:0回答:1

我正在写(Selenium)单元测试,我遇到了一个N​​ULL问题。此代码以前正在运行并正在运行,但现在失败并且未在所需字段中输入值。任何人都可以给我一些关于可能出错的建议吗?

单元测试目标:导航Web服务UI并输入应显示结果的值。

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();
        }
    }
}

DMV Tests.ca

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();
        }

    }
}

运行单元测试后的错误

  1. 消息:测试方法Tests.WebServiceTestScripts.DMTests.DM_Med引发异常: System.ArgumentNullException:text不能为null参数名称:text
  2. 堆栈跟踪: RemoteWebElement.SendKeys(字符串文本) ValueCommand.SearchValue() DMTests.DM_Med()
  3. CS1717对同一变量进行的赋值;你的意思是分配其他东西吗?

调试时出错

运行Debug时,以下行会中断:

L41    this.valuenum = valuenum;

L47    inputValue.SendKeys(valuenum);
c# unit-testing selenium null argumentnullexception
1个回答
0
投票

你忘了在string valuenum类的构造函数中传入ValueCommand


DMPage类内:

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

ValueCommand类内:

public ValueCommand(string valuenum)
{
    this.valuenum = valuenum;
}
© www.soinside.com 2019 - 2024. All rights reserved.