Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Protractor: select a form element using label

I'm using protractor and I'm trying to select a form using it's label.

Let's take an example. I have this form with several input:

<form>
    <label for="name">Name</label>
    <input type="text" id="name">
    <label for="address">Address</label>
    <input type="text" id="address">
    <label for="email">Email</label>
    <input type="text" id="email">
</form> 

My step definition should be something like that:

this.When(/^I fill input identified by label "([^"]*)" with value "([^"]*)"$/, function(labelName, value, callback){        
   ...
});

I need to write this step definition because I'm using protractor with cucumber.js, so the person who will run the test should be able to fill a form knowing only the label, without inspect html code to retrieve class, id or other attributes.

I've found this question on github but I would like to know if there is a better method to do this.

Can anyone help me to solve this problem? I don't like to ask code snippets to other developers but I really don't know how to proceed and I can't find documentation about this problem.

IMPORTANT: I'm NOT using Angularjs

Thank you in advance.

like image 693
Ema.jar Avatar asked Aug 31 '26 22:08

Ema.jar


1 Answers

You can find the label element by text and then get the following sibling input element:

var input = element(by.xpath("//label[. = '" + labelName + "']/following-sibling::input"));
input.sendKeys(value);
like image 59
alecxe Avatar answered Sep 02 '26 11:09

alecxe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!