Protractor Chained Elements by Using Variables?

前端 未结 3 670
半阙折子戏
半阙折子戏 2020-12-16 01:39

I am trying to keep my pageObjects in Protractor as clean as possible, but have run up against some behavior in selecting sub-elements.

This works:

v         


        
3条回答
  •  清酒与你
    2020-12-16 02:01

    I have a lot of the following code:

    var parent = element(by.css('.parent-class'));
    var child = parent.element(by.css('.child-class'));
    
    child.getText();
    

    But as far as I understood you have a lot of children and you don't want to list all the variants.

    Additionally to Nathan Thompson answer you can have a helper function in page object to find subelement:

    function getCard(parent, child) { // Or getChild()
      return element(by.css(parent)).element(by.css(child));
    }
    
    function getCardText(parent, child) { // Or getChildText
      return getCard(parent, child).getText();
    }
    

    So then you can just write in spec:

    expect(cardPage.getCardText('.parent-class', '.child-class')).toBe('...');
    

提交回复
热议问题