Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:58+00:00 2026-05-23T15:08:58+00:00

I’ve got a form which allows me to add/edit categories and sub categories within

  • 0

I’ve got a form which allows me to add/edit categories and sub categories within the one form. This form uses AJAX and to test it I’ve been using Capybara with some selectors.

The problem is with the selectors there seems to be subtle differences between when I’m creating a new category with sub categories to when I’m editing a category with sub categories.

Here is my create scenario:

  @javascript @wip
  Scenario: Create new category with sub categories
    Given I am on the new category page
    When I fill in "Name" with "Main" within the parent fields
    And I follow "Add sub category"
    And I fill in "Name" with "Sub1" within the 1st sub category fields
    And I follow "Add sub category"
    And I fill in "Name" with "Sub2" within the 2nd sub category fields
    And I follow "Add sub category"
    And I fill in "Name" with "Sub3" within the 3rd sub category fields
    And I press "Save"
    Then I should be on the "Main" category page
    And I should see "Main"
    And I should see "Sub1"
    And I should see "Sub2"
    And I should see "Sub3"

This works with selectors:

when /the parent fields/
  "table tr:nth-child(1)"

when /the (\d+)(?:st|nd|rd|th) sub category fields/
  pos = $1.to_i + 2
  "table tr:nth-child(#{pos})" 

On form:

= form_for @category do |f|
    %table
        %tr
            %td= f.label :name
            %td= f.text_field :name

        %tr
            %td(colspan=2)
                %b Sub categories

        - f.fields_for :children  do |child|
            = render "child_fields", :f => child

        %tr
            %td= link_to_add_fields "Add sub category", f, :children
        %tr
            %td= f.submit 'Save'

child_fields partial:

%tr.subs
    %td= f.label :name
    %td= f.text_field :name

When I use the same selectors though in my edit scenario I cannot select the 2nd category. Here is my edit category feature:

  @javascript @wip
  Scenario: Edit category with sub categories
    Given a category exists
    And category "Books" has sub category "Fiction"
    And category "Books" has sub category "Non-Fiction"
    And I am on the edit page for category "Books"
    When I fill in "Name" with "Cars"
    And I fill in "Name" with "Coupe" within the 1st sub category fields
    And I fill in "Name" with "Sports" within the 2nd sub category fields
    And I press "Save"
    Then I should be on the "Cars" category page
    And I should see "Cars"
    And I should see "Coupe"
    And I should see "Sports"

If I change my selector to:

when /the (\d+)(?:st|nd|rd|th) sub category fields/
  pos =  $1.to_i * 2 + 1
  "table tr:nth-child(#{pos})" 

Then it works for edit but not the new scenario.

Is there a way to use the same selector for both new & edit scenarios in my case?

Am I better using a different type of selector on my form? If so does anyone have any recommendations?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:08:59+00:00Added an answer on May 23, 2026 at 3:08 pm

    Use an id on the unique elements, and class combinations on repeated elements. With the right combination of class and id selectors you will always arrive at a unique child. Keep in mind you can group selectors on the one element.

    So

    Given a category exists

    wait_for_xpath = '//element(@class = "categoryClass")'
    

    And category “Books” has sub category “Fiction”

    wait_for_xpath = "//element(contains (@class, 'categoryClass') and (@id, 'bookId'))//element(@id='fiction')"
    

    etc

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.