protractore2e-testingangular-e2e

Protractor: How to write a locator to select the desired div and click the button on it?


There are three blocks on the page, for example. example scructare Each block has a title and div.action with a button example The headings are different for everyone. I need to select a div with a Text heading and click a button

I find this element like this

return browser.element(by.css('.group-list-item:last-child')).element(by.css('.action'));

But the necessary block will not necessarily be the last, it can be 1 or the second. The title of the block does not change

show element show structure


Solution

  • I am supposing the title of three blocks as Block1, Block2 and Block3

    Now the button can be clicked for each block by passing the block title as the title of block is not changing and that can be passed run time. Assuming "group-list-item" is the class name say under tag div and also class name of button is "action" under tag button

    clickButton(blockTitle: string): ElementFinder { return element(by.xpath("*//div[contains(@class,'group-list-item')]//div(text()='" + blockTitle+ "']//following-sibling::div")); }