将browser.element获取到包含子项的页面对象中 [英] Get browser.element into page objects with children

查看:125
本文介绍了将browser.element获取到包含子项的页面对象中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我分叉驱动程序实例以在浏览器之间发送消息时,我正在进行测试。我还使用页面对象来映射视图。在我的页面对象中,我有其他页面对象,其中一些是从另一个页面对象继承的。但是,当我分叉驱动程序实例并使用两个浏览器时,从页面对象中的分叉驱动程序获取元素函数很麻烦。特别是对于其他PO继承自的页面对象。这是我的代码中的一个示例:

I am doing testing when I fork a driver instance to send messages between browsers. I also use page objects for mapping the view. In my page objects I have other page objects, some of who inherits from another page object. Though, when I have forked a driver instance and have two browsers to work with, it's troublesome to get the element function from the forked driver in the page objects. Especially for the page object who other PO inherits from. This is a sample from my code:

var NavBar = function () {  
    this.button = element(by.buttonText('Click'));
};

var MySiteElement = function () {
    this.someElement = element(by.name('hiddenElement'));
};

NavBar.prototype = new MySiteElement();

var HomeView = function (element) {
    this.navbar = new NavBar()
};

module.exports = HomeView;

假设我的规格如下:

describe('Home View', funciton () {
    it('should get text from right browser', function () {
        browser.get('http://localhost:3000/#/home');
        var browser2 = browser.forkNewDriverInstance(true);
        var view = new HomeView(browser.element);
        var view2 = new HomeView(browser2.element);
        view2.navbar.content.click()
        expect(view.navbar.someElement.getText()).toBe('unchanged')
        expect(view2.navbar.someElement.getText()).toBe('changed')
    });
});

假设我的网站是合成的,这样当单击导航栏按钮时,someElement的文本将更改为改变。但是我必须单击右边的元素,当我有一个我想要与之交互的分叉浏览器时,使用just元素是不对的。但我不知道如何在HomeView页面对象中设置正确的元素变量。我现在说browser2.element从分叉驱动程序中获取了元素变量,但是如何将它暴露给所有页面对象。目前我将浏览器实例的元素属性作为参数传递给页面对象,但我不知道如何更进一步。

Let's say that my site is composed so that when the navbar button is clicked, the someElement's text change to 'changed'. But I have to click on the right element, and it's not right to use just element when I have a forked browser that I want to interact with. But I don't know how to set the right element variable inside the HomeView page object. I now that browser2.element get me the element variable from the forked driver, but how do I expose that to all the page object. Currently I pass the browser instance's element property as an argument to the page object, but I don't know how to go further.

请帮忙,这让我抓狂!

推荐答案

我想出了一个解决方案。由于元素函数只应该传递一次(当实例化HomeView时,因为每个其他PO都是HomeView的子节点)我只传递一次元素函数。然后,类的所有声明都嵌套在HomeView对象函数中:

I came up with a solution. Since the element function only should be passed once (when instantiating the HomeView since every other PO is a child of the HomeView) I only pass the element function once. All declarations of the classes are then nested inside the HomeView object function:

var HomeView = function (element) {
    var NavBar = function () {  
        this.button = element(by.buttonText('Click'));
    };

    var MySiteElement = function () {
        this.someElement = element(by.name('hiddenElement'));
    };

    NavBar.prototype = new MySiteElement();

    this.navbar = new NavBar(element);
};

module.exports = HomeView;

但是,如果您有多个View对象,这意味着重复自己(没有好处)。我使用节点模块导入页面对象,并且模块的范围不会从它们所需​​的位置继承。但对此我也有一个解决方案。在页面对象所在的模块中,导出一个包含页面对象声明的函数并将元素传递给它。

However, if you have several View objects, this means repeating yourself (no good). I use node modules for importing the page objects, and the modules' scope don't inherit from where they're required. But to this I have a solution as well. In the module where the page object lies, export a function holding the page object declaration and pass the element to it.

var HomeView = function (element) {
    var NavBar = require('./navBar.po.js')(element):

    this.navbar = new NavBar();
};

module.exports = HomeView

。 /navBar.po.js 看起来像这样:

module.exports = function (element) {
    require('./mySiteElement.po.js')(element);

    var NavBar = function () {
        this.button = element(by.buttonText('Click'));
    }

    NavBar.prototype = new MySiteElement();

    return NavBar;
};

这篇关于将browser.element获取到包含子项的页面对象中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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