Selenium Framework页面对象模型和页面导航 [英] Selenium Framework Page Object Model and Page Navigation

查看:141
本文介绍了Selenium Framework页面对象模型和页面导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何使用Selenium为Web应用程序创建框架.我现在正在学习如何使用页面导航创建页面对象模型.我当前正在播放的视频显示了如何执行此操作,但是视频末尾的代码不起作用.他正在尝试使用Class的返回类型来初始化该类中的所有对象,但由于使用了非参数化的构造函数,因此他失败了.那么,使用非参数化构造函数时,有没有办法初始化我的页面元素?

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?

位于此处的视频: http://youtu.be/IUBwtLG9hbs

在上面的视频中,他只是在创建一个简单的测试,以便在Google文本框中输入文本"Selenium".点击搜索.单击硒链接.然后单击Selenium网页上的下载"选项卡.

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.

在视频的结尾,他试图通过单击按钮或链接将您重定向到新页面后初始化元素来展示如何使用页面导航".该测试的目的是在单击Google页面中的Selenium链接后,映射Selenium网页的所有元素.

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.

因此他添加了"return new SeleniumPageObjects();".转到GoogleHomePageObjects类中的以下方法:

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

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

然后,在Test类的main/test方法中,他添加以下内容以单击Selenium链接,然后返回Selenium页面的所有映射对象:

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

然后,他在SeleniumPageObjects类中添加了一个构造函数:

He then adds a constructor in the SeleniumPageObjects class:

public SeleniumPageObjects()
{
}

它抛出NullReferenceException,因为它从未初始化硒页面对象(请参见下面的代码,了解如何在不使用返回类型的情况下完成此操作).如何解决?他说要看第7部分,但我看了第7部分,并且从未解决过错误的代码.

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.

注意:我已经知道如何在我的main/test方法中显式初始化页面对象.例如,将其添加到main/test方法:

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

并初始化SeleniumPageObjects类中的元素:

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.

推荐答案

有两种方法可以解决此问题.

There are two possible ways to fix this issue.

方法1 在类中为Driver实例创建一个静态属性,并在所有页面中使用该静态属性

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

例如

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

可以更改为

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

所以StaticClass可以看起来像这样

So the StaticClass can look something like this

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

方法2 为页面创建一个BaseClass并在其中创建WebDriver属性,以便在继承该页面的Baseclass的同时,您也将获得Driver的引用

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

例如 基类

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 !!!

谢谢, 卡尔提克(Karthik KK) http://www.executeautomation.com

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

这篇关于Selenium Framework页面对象模型和页面导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆