Selenium Framework Page Object Model and Page Navi

2019-06-14 10:30发布

I am learning how to create framework for web applications using Selenium. I am now learning how to create page object models with page navigation. The video I am currently on is showing how to do this, but the code at the end of the video does not work. He is trying to use a return type of the Class to initialize all the objects in that class, and he fails in doing so, because he uses a non-parameterized constructor. So is there a way to initialize my page elements when using a non-parameterized constructor?

Video located here: http://youtu.be/IUBwtLG9hbs

In the above video, he is just creating a simple test to enter the text "Selenium" into the google text box. Click on search. Click on the Selenium link. Then click on the Download tab on the Selenium webpage.

At the end of the video, he is trying to show how to use Page Navigation by initializing the elements after a button or link is clicked that sends you to a new page. The goal of this test is to map all of the elements of the Selenium web page after the click of the Selenium link from the Google page.

So he adds "return new SeleniumPageObjects();" to the following method in the GoogleHomePageObjects class:

public SeleniumPageObjects ClickSelenium()
{
    lnkSelenium.Click();
    return new SeleniumPageObjects();
}

Then in the main/test method in the Test class, he adds the following to click on the Selenium link and then return all of the mapped objects of the Selenium page:

SeleniumPageObjects selPage = page.ClickSelenium();

He then adds a constructor in the SeleniumPageObjects class:

public SeleniumPageObjects()
{
}

It throws a NullReferenceException because it never initialized the selenium page objects (See below code of how it is done without using a return type). How can this be fixed? He says to watch part 7, but I watched part 7 and the erroneous code is never addressed.

NOTE: I already know how to explicitly initialize page objects in my main/test method. For instance, add this to the main/test method:

    SeleniumPageObjects selPage = new SeleniumPageObjects(driver);

And initialize the elements in the SeleniumPageObjects class:

public SeleniumPageObjects(IWebDriver driver)
{
    PageFactory.InitElements(driver, this);
}

I just want to know how to do this when using a return type, as this accomplishes the Page Relation he is talking about.

1条回答
Juvenile、少年°
2楼-- · 2019-06-14 10:47

There are two possible ways to fix this issue.

Method 1 Create a static property for the Driver instance in a class and use the static property in all the pages

E.g.

public SeleniumPageObjects(IWebDriver driver)
{
  PageFactory.InitElements(driver, this);
}

Can be changed to

public SeleniumPageObjects()
{
  PageFactory.InitElements(StaticClass.Driver, this);
}

So the StaticClass can look something like this

public class StaticClass
{
  public static IWebDriver Driver {get;set;}
}

Method 2 Create a BaseClass for page and create the WebDriver property in it, so that while inheriting the Baseclass for the page you will get the reference for Driver as well

E.g Base Class

public class BaseClass
{
  public static IWebDriver Driver {get;set;}
}

So, the Selenium Page Object Class will look like this

public class SeleniumPageObject : BaseClass
{
    public SeleniumPageObjects()
    {
      PageFactory.InitElements(Driver, this);
    }
}

Hope this answers your question !!!

Thanks, Karthik KK http://www.executeautomation.com

查看更多
登录 后发表回答