-1

When I run my selenium tests using .net frame and C# I am getting Message: Object reference not set to an instance of an object.

{
    landingPage = new LandingPageCode(driver);
    driver = new ChromeDriver();

    driver.Url = ("My URL");

    IWebElement element = landingPage.PageTitle; 

    Assert.IsTrue(element.Displayed);
}

My base code is below, which is being referenced within the above Iwebelement:

[FindsBy(How = How.TagName, Using = "PageTitle")] 
public IWebElement PageTitle{ get; set; }

I also get the same issue when I write the test as per below without the element aspect.

{
    driver = new ChromeDriver();
    landingPage = new LandingPageCode(driver);

    driver.Url = ("My URL");

    Assert.IsTrue(landingPage.PageTitle.Displayed);
}     

The landing page base code page is as per below:

   [FindsBy(How = How.TagName, Using = "HomePage")]
   public IWebElement PageTitle { get; set; }
CalAtk
  • 45
  • 1
  • 10
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jonathon Chase Oct 09 '19 at 14:24
  • It's almost certainly `element` that is null, and trying to access the `Displayed` property is throwing your exception. – Jonathon Chase Oct 09 '19 at 14:25
  • Can you post the code in the LandingPageCode constructor? I suspect you need to call `PageFactory.InitElements(driver, this);`. – Greg Burghardt Oct 09 '19 at 22:18

1 Answers1

0

Update: After some research I think you're missing a call to PageFactory.InitElements(driver, landingPage);

Your snippet strongly suggest that PageTitle is null. As @greg-burghardt suggests an initialisation method needs to be called on landingPage in order for it to be filled up.

Let's try this:

{
    driver = new ChromeDriver();
    landingPage = new LandingPageCode(driver);

    driver.Url = ("My URL");
    // landing.PageTitle is null here

    PageFactory.InitElements(driver, landingPage);
    Assert.IsNotNull(landingPage.PageTitle, "PageTitle is null. Failed to load page?");
    Assert.IsTrue(landingPage.PageTitle.Displayed);
} 
tymtam
  • 31,798
  • 8
  • 86
  • 126
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. - [From Review](/review/low-quality-posts/24268473) – NullDev Oct 09 '19 at 22:50
  • @NullDev Yes! A very fair comment. I expanded the answer. Thanks for calling me out. – tymtam Oct 09 '19 at 23:03