Smithy7876
Smithy7876

Reputation: 344

Selenium PageObjects variable handling

I'm fairly sure this is a simple one but I'm not finding the answer.

I have a script which is written using PageObjects. This is all controlled by a master "Run Test" class and runs off to several Page Object Classes.

Now the problem I am incurring is I need to pick up a system generated variable in Step 3 of my script and then use it in Step 5 however this variable is not being passed to that class.

What am I doing wrong?

Main 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
        }

Variable is set by

//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);

With in public void AddNewContract(string testName, string PlanID)

First line in the StartCheck class is a Console.Writeline for the plan ID but it always returns nothing

Upvotes: 0

Views: 202

Answers (1)

Guy
Guy

Reputation: 50809

When doing PlanID = PlanReference.Substring(StringLength - 7, 7); you set the value of the local variable passed to this method. It has no effect on the PlanID variable in RunTest. You nee to return the new value and assign it

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

And in RunTest

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

Upvotes: 2

Related Questions