2017-06-13 55 views
0

我很确定这是一个简单的问题,但我找不到答案。Selenium PageObjects变量处理

我有一个使用PageObjects编写的脚本。这全部由主“运行测试”类控制,并运行到多个页面对象类。

现在我所面临的问题是我需要在脚本的第3步中选取系统生成的变量,然后在第5步中使用它,但是此变量未被传递给该类。

我在做什么错?

主类

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类

第一行是计划ID的Console.Writeline,但它始终没有返回

回答

2

在做PlanID = PlanReference.Substring(StringLength - 7, 7);时,您设置了传递给此方法的局部变量的值。它对RunTest中的PlanID变量没有影响。你娘家返回新值,并为它分配

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

​​
+0

我试过这个解决办法,但我在“自” NewContracts.AddNewContract回线(字符串得到这个错误)'返回void,返回关键字不能跟一个对象表达式' – Smithy7876

+0

@ Smithy7876所以更改'void'为'string' – Guy

+0

啊,我不知道我可以这样做。开启一个全新的可能性世界!谢谢! – Smithy7876