Selenium PageObjects 变量处理

Selenium PageObjects variable handling

我相当确定这是一个简单的问题,但我没有找到答案。

我有一个使用 PageObjects 编写的脚本。这一切都由主 "Run Test" class 控制,并运行到多个页面对象 Classes。

现在我遇到的问题是我需要在我的脚本的第 3 步中获取一个系统生成的变量,然后在第 5 步中使用它,但是这个变量没有传递给 class。

我做错了什么?

主要Class

class RunTest
    {

        static IWebDriver driver;
        string PlanID; <- Tried setting here?

        //Opens Chrome, Navigates to CTSuite, Logs in and Selects desired environment. 
        [TestFixtureSetUp]
        public void Login()
        {
         ... Login Code
        }
        [Test]
        public void Test5()
        {


            //Set back to home page
            driver.FindElement(By.XPath("//*[@id='ajaxLoader']")).Click();

            var NetChange = new SwapNetwork(driver);
            NetChange.ChangeTheNetwork("LEBC");

            var SearchForClient = new GoTo(driver);
            SearchForClient.GoToClient("810797");

            var NewContract = new NewContracts(driver);
            NewContract.AddNewContract("Test5", PlanID); //PlanID is set in this class

            var NewFee = new NewFee(driver);
            NewFee.AddNewFee("Test5");

            var StartChecks = new NewFee(driver);
            StartChecks.ExpectationChecks("Test5", PlanID); //Then needs to be used here
        }

变量由

设置
//Collect plan reference number
            string PlanReference = driver.FindElement(By.XPath("//*[@id='ctl00_MainBody_Tabpanel1_lblRecord']")).Text;
            Console.WriteLine("Plan Details: " + PlanReference);
            var StringLength = PlanReference.Length;
            PlanID = PlanReference.Substring(StringLength - 7, 7);

与在 public void AddNewContract(string testName, string PlanID)

StartCheck 中的第一行 class 是计划 ID 的 Console.Writeline,但它总是 returns 没有任何内容

在执行 PlanID = PlanReference.Substring(StringLength - 7, 7); 时,您设置传递给此方法的局部变量的值。它对 RunTest 中的 PlanID 变量没有影响。您需要 return 新值并分配给它

string planID = PlanReference.Substring(StringLength - 7, 7);
return planID;

// or if you don't need it in the method just
return PlanReference.Substring(StringLength - 7, 7);

并且在RunTest

PlanID = NewContract.AddNewContract("Test5"); // no need to send PlanID