Cucumber/Capybara Selecting Button from Specific C

2019-07-08 22:51发布

So i've ran into an issue, im trying to click a button...which unfortunately has the same text (IE there are 2 buttons on the page which the same text, basically a save button)

Lets pretend the button text is just "Save".

I did notice that they had different classes.

<button data-action="submit" class="btn btn-primary btn-save">Save</button>

whereas the other button is:

<button name="button" type="submit" class="btn btn-primary set-right">
      <i class="glyphicon glyphicon-floppy-disk"></i> Save
</button>

I know glypicon is just an icon set....but they both seem to belong to the same class? but have different class names? (Sorry im not familiar with Rails)

It doesn't honestly matter which one I select as they both have the same function. I've seen where you can use xpath? but aren't we supposed to use css selectors or something now? (As in thats the newest way?) I may be wrong....

Could I use something like:

find(:xpath, '//button[@class="btn-save"]').click

Im trying to avoid "rails" only solutions, as not all the websites I test on are rails based.

2条回答
劳资没心,怎么记你
2楼-- · 2019-07-08 23:50

You have various possibilities:

1) Find the button by its class

find(button.btn.btn-primary.btn-save).click

2) Find the button by its css selector or xpath (you can copy them using Google Chrome: right click -> Inspect -> right click on your element -> copy css or xpath)

find(:css, "your button css selector").click

find(:xpath, "your button xpath").click
查看更多
beautiful°
3楼-- · 2019-07-08 23:50

If both buttons really do the same thing then you could always just do

first(:button, 'Save', minimum: 1).click

which will be less prone to breakage as your page structure changes than using an xpath selector. The minimum:1 option will just make #first wait a bit until at least one button is on the page (like find would) - it may not be necessary depending on your test structure

查看更多
登录 后发表回答