UzIT
UzIT

Reputation: 51

How to locate element by type, value or alt attribute in protractor?

Example 1: I have this html code for a button.

<button class="submit_btn" type="submit" value="Login" ng-disabled="LoginCtrl.loginStatus === LoginCtrl.loginCtrlConfig.LOGIN_LOADING">

Using protractor, if I don't want to use class name, but use 'type' or 'value' parameter.

Example 2:

<img src="someSource" alt="Firewall for Code" width="90" height="46">

I want to use the 'alt' parameter for the above img to test that this element is located on the page, hence the test should pass.

How do I achieve above two tasks in Protractor?

Upvotes: 0

Views: 1280

Answers (1)

alecxe
alecxe

Reputation: 473833

You could/should be using CSS selectors to add filter conditions on the element attributes.

In the first example, it might be:

var submitButton = element(by.css("button[type=submit][value=Login]"));

// element is present
expect(submitButton.isPresent()).toBe(true);

// element is displayed
expect(submitButton.isDisplayed()).toBe(true);

In the second example, it would be:

var sourceImage = element(by.css('img[alt="Firewall for Code"]'));

// element is present
expect(sourceImage.isPresent()).toBe(true);

// element is displayed
expect(sourceImage.isDisplayed()).toBe(true);

Note that in this case, we need quotes around the attribute value since there are spaces in the attribute value. Here is more information about when would you need quotes and then would not:

Upvotes: 2

Related Questions