PageObject Model framework-Selenium C#-Null reference exception

333 views
Skip to first unread message

manoj kumar

unread,
Jul 15, 2014, 8:55:59 AM7/15/14
to webd...@googlegroups.com
I created a basic page object model framework in selenium c#.
In visual studio, created 2 folders, Main and Test.
In Main folder, created HomePage.cs class
In test folder, created RegressionTestSuite.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using SeleniumTests.Main;

namespace SeleniumTests.Test
{
   [TestFixture]
   public class RegressionTestSuite
    {
        public IWebDriver driver;
        private static string URL = "http://www.xyz.com/";

        [SetUp]
        public void SetUp()
        {
            driver = new FirefoxDriver();
            driver.Navigate().GoToUrl(URL);
            driver.Manage().Window.Maximize();
        }
       [Test]
       public void ValidSignUp()
       {
           var homePage = new HomePage();
         
       }

       [TearDown]
       public void TearDown()
       {
           driver.Close();
           driver.Quit();
       }
    }

}

----------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;
using OpenQA.Selenium;
using SeleniumTests.Test;

namespace SeleniumTests.Main
{
    
   public class HomePage
   {
       
           public HomePage()
       {
                   Assert.IsTrue(driver.PageSource.Contains("Login / Sign Up"));
                   driver.FindElement(By.Id("signup-button")).Click();
      }

       
    }
}
----------------------------------------------------

In Homepage, i need to check, Login / Sign Up text is there or not?
Also, click on Sign Up button.

when i run this test, i am getting an System.NullReferenceException.
object is not set to an instance of an object.

In homepage, driver is null.

I can't find a way to over come this.

Please some one help me.

Thanks

Elangovan Ganesan

unread,
Jul 17, 2014, 10:04:00 AM7/17/14
to webd...@googlegroups.com
Hi,

You are creating the WebDriver instance(session) in Test class and not passing that to your Page class. Via Constructor you can pass the driver instance from test class to page class.

I do not know C#, wrote in the way of java. See the below code. 


In test class:
====================
  [Test]
       public void ValidSignUp()
       {
           var homePage = new HomePage(driver);
         
       }

In Page class:
================
Kind of below,

  public class HomePage
   {
       WebDriver driver = null;
           public HomePage(WebDriver driver)
       {
                   this.driver = driver;
                   Assert.IsTrue(driver.PageSource.Contains("Login / Sign Up"));
                   driver.FindElement(By.Id("signup-button")).Click();
      }

       
    }

Thanks & Best regards,
Elangovan G



--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages