Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multi value in index property #453

Open
CognizantCOE opened this issue Oct 17, 2017 · 1 comment
Open

Add support for multi value in index property #453

CognizantCOE opened this issue Oct 17, 2017 · 1 comment

Comments

@CognizantCOE
Copy link

looking for supporting multi value in index property for locators like highlighted in code below.
indexed_property(:cart_contents, [
[:span, :title, {:id => 'cart[%s].title[%s]'}],
[:text_field, :quantity, {:id => 'cart[%s].quantity'}],
[:button, :delete, {:id => 'cart[%s].delete'}]
])

@jkotests
Copy link
Collaborator

jkotests commented Nov 21, 2018

It wasn't obvious, but we actually already have support for this. We are injecting the "index" into the locator Strings. The "index" can be more complex - eg Array or Hash (see https://stackoverflow.com/a/554877/1200545 for String manipulation examples).

For example, given the page:

<html>
  <body>
    <div>
      <span id="cart[1].title[1]">title 1</span>
      <input type="text" id="cart[1].quantity">
      <button id="cart[1].delete">delete 1</button>
    </div>
    <div>
      <span id="cart[2].title[2]">title 2</span>
      <input type="text" id="cart[2].quantity">
      <button id="cart[2].delete">delete 2</button>
    </div>
  </body>
</html>

You can do:

class MyPage
  include PageObject

  indexed_property(:cart_contents, [
    [:span, :title, {:id => 'cart[%s].title[%s]'}],
    [:text_field, :quantity, {:id => 'cart[%s].quantity'}],
    [:button, :delete, {:id => 'cart[%s].delete'}]
  ])
end

page = MyPage.new(browser)
cart_content = page.cart_contents[[1, 1]]
p cart_content.title
#=> "title 1"
p cart_content.quantity_element.id
#=> "cart[1].quantity"
p cart_content.delete_element.text
#=> "delete 1"

If you need to repeat values, you can avoid duplication by using Hash keys:

class MyPage
  include PageObject

  indexed_property(:cart_contents, [
    [:span, :title, {:id => 'cart[%{idx}].title[%{idx}]'}],
    [:text_field, :quantity, {:id => 'cart[%{idx}].quantity'}],
    [:button, :delete, {:id => 'cart[%{idx}].delete'}]
  ])  
end

page = MyPage.new(browser)
cart_content = page.cart_contents[{idx: 1}]
p cart_content.title
#=> "title 1"
p cart_content.quantity_element.id
#=> "cart[1].quantity"
p cart_content.delete_element.text
#=> "delete 1"

I'll leave this open for a bit. At a minimum, it would help to write an example on the wiki and/or documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants