0

I have the following feature file for this page:

@checkbox
Feature: Check the checkbox
  Webdriver should be able to check the checkbox lists

  Scenario: Check the checkbox
    Given I am in checkbox task page 
    Then I can check the hobbies from the checkbox


  Scenario Outline: Title of your scenario outline
    Given I am in "http://suvian.in/selenium/1.6checkbox.html"
    Then I can check the <id> from the checkbox

    Examples: 
       | id |
       |  1 |
       |  2 |
       |  3 |
       |  4 |

The problem is, I have involved thecnical details like id in my scenario outline. I would like to change it as

 Examples: 
           | hobby      |
           |  dancing   |
           | sporting   |
           |  singing   |
           |  PC Gaming |

But i don't know, how to address the element:

<input style="width:20px;height:20px;" type="checkbox" id="2">

in my selenium webdriver?

2 Answers 2

1

You can use xpath preceding-sibling

//label[contains(.,'Singing')]/preceding-sibling::input

This will locate the label Singing and from there the previous sibling <input> tag.

Sign up to request clarification or add additional context in comments.

1 Comment

this is a better solution.
0

you can try to find out all the labels under class='intro-message'

List<WebElement> labels = driver.findElements(By.xPath("//div[@class='intro-message']/label"));

then try to loop each element in the list to read its text and if that matches the value you are passing in your cucumber Examples then you can fetch the attribute value of 'for' for that element

public WebElement getCheckboxLocator(string checkboxName){
    int id = 0;
    foreach(WebElement ele in labels){
        if(ele.getAttribute("textContent").equalsIgnoreCase(checkboxName)){
            id = Integer.parseInt(ele.getAttribute("for"));
        }
    }
return driver.findElement(By.xPath(string.format("//div[@class='intro-message']/input[id='%s']", id)));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.