Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select -> option abstraction

In Python, Java and several other selenium bindings, there is a very convenient abstraction over select->option HTML constructions, a Select class.

For example, imagine there is the following select tag:

<select id="fruits" class="select" name="fruits">
    <option value="1">Banana</option>
    <option value="2">Mango</option>
</select>

Here is how we can operate it in Python:

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_id('fruits'))

# get all options
print select.options

# get all selected options
print select.all_selected_options

# select an option by value
select.select_by_value('1')

# select by visible text
select.select_by_visible_text('Mango')

In other words, it is a very transparent and easy to use abstraction.

Is is possible to manipulate select tag in protractor in a similar manner?


This is not a duplicate of How to select option in drop down protractorjs e2e tests or How to click on option in select box in Protractor test?.

like image 636
alecxe Avatar asked Feb 25 '15 15:02

alecxe


People also ask

What is abstraction with example?

In simple terms, abstraction “displays” only the relevant attributes of objects and “hides” the unnecessary details. For example, when we are driving a car, we are only concerned about driving the car like start/stop the car, accelerate/ break, etc.

What is a real life example of abstraction?

Abstraction in the real worldMaking coffee with a coffee machine is a good example of abstraction. You need to know how to use your coffee machine to make coffee. You need to provide water and coffee beans, switch it on and select the kind of coffee you want to get.

What is code abstraction?

Through the process of abstraction, a programmer hides all but the relevant data about an object in order to reduce complexity and increase efficiency. In the same way that abstraction sometimes works in art, the object that remains is a representation of the original, with unwanted detail omitted.

What is data abstraction in OOPs?

Data abstraction is one of the most essential and important feature of object oriented programming in C++. Abstraction means displaying only essential information and hiding the details.


1 Answers

No such thing in Protractor, but we can write our own:

select-wrapper.js

'use strict';

var SelectWrapper = function(selector) {
    this.webElement = element(selector);
};
SelectWrapper.prototype.getOptions = function() {
    return this.webElement.all(by.tagName('option'));
};
SelectWrapper.prototype.getSelectedOptions = function() {
    return this.webElement.all(by.css('option[selected="selected"]'));
};
SelectWrapper.prototype.selectByValue = function(value) {
    return this.webElement.all(by.css('option[value="' + value + '"]')).click();
};
SelectWrapper.prototype.selectByPartialText = function(text) {
    return this.webElement.all(by.cssContainingText('option', text)).click();   
};
SelectWrapper.prototype.selectByText = function(text) {
    return this.webElement.all(by.xpath('option[.="' + text + '"]')).click();   
};

module.exports = SelectWrapper;


Usage

var SelectWrapper  = require('select-wrapper');
var mySelect = new SelectWrapper(by.id('fruits'));

# select an option by value
mySelect.selectByValue('1');

# select by visible text
mySelect.selectByText('Mango');


Note that Select is a reserved word in JavaScript

like image 77
Delian Mitankin Avatar answered Sep 27 '22 23:09

Delian Mitankin