Select -> Option Abstraction

Select - option abstraction

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

How to return a list of available options in a select element in Protractor?

From what I understand, you are looking for map():

Apply a map function to each element within the ElementArrayFinder.
The callback receives the ElementFinder as the first argument and the
index as a second arg.

getOptions = function () {
var self = this;

return this.selector.click().then(function () {
return self.selectorOptions.map(function (option) {
return option.getAttribute("value").then(function (value) {
return value;
});
});
});
};

New to Protractor and difficulty with collecting Select options

First of all, use element notation - would at least look cleaner.

If you want to see the option text or value on the console, you need to resolve promises:

var weightUnitSelect = element(by.name("wu"));
var options = weightUnitSelect.all(by.tagName("option"));

options.first().getText().then(function (text) {
console.log(text);
});

Also, I recommend to abstract select->option HTML constructions with the help of this answer:

  • Select -> option abstraction

Using Protractor, how do I get a random select option other that the currently selected one?

First, let's filter non-selected options using .filter():

var nonSelectedOptions = $$("option").filter(function (option) {
return option.isSelected().then(function (isSelected) {
return !isSelected;
});;
});

Now, we need a list of indexes to choose a random one from, let's use .map():

var indexes = nonSelectedOptions.map(function (option, index) {
return index;
});

Now, we need to resolve the indexes promise to get the array of indexes and use the "picking a random item from an array" solution from this answer. We are going to use .get() to get a specific option for the randomly chosen index:

indexes.then(function (indexes) {
var randomIndex = indexes[Math.floor(Math.random()*indexes.length)];

var randomOption = nonSelectedOptions.get(randomIndex);

// now select the randomOption
});

If you would use the wrapper suggested here, the option selection code would be:

randomOption.getAttribute("value").then(function (optionValue) {
mySelect.selectByValue(optionValue);
});

Not tested.

Select option is added or removed event

You could try calling a checking function for a given select Element, using a timeout to check periodically.

function periodicallyCheckSelect(){
var jSelect = $(this)
,opts = this.options
,prevLen = +jSelect.attr('data-previousLen') || opts.length
,isEq = false
,isAdded = false
,isRemoved = false
,state = '';
isEq = opts.length === prevLen;
isAdded = opts.length > prevLen;
isRemoved = opts.length < prevLen;
state = isEq ? 'no change'
: isAdded ? opts.length-prevLen + ' option(s) added'
: prevLen-opts.length+' option(s) removed';
jSelect.attr('data-previousLen',opts.length);
log(state+'<br>');
}

Here is a jsfiddle playing around with the idea.

Getting Select Options From Array

You can try -

<select name="menu-item-visibility[<?php echo $item_id; ?>][]" id="edit-menu-item-visibility-<?php echo $item_id; ?>" class="chzn-select" multiple="true"
<?php
$vals = get_post_meta( $item_id, 'locations', true );
foreach($countries as $key => $value) { // Loop through countries
?>
<option value="<?php echo $key;?>"
<?php echo (is_array( $vals ) && in_array( $key, $vals )) ? 'selected' : ''; ?> >
<?php echo $value;?>
</option>
<?php
}
?>
</select>

How can I inspect the contents of a DropDown List with Protractor?

You can approach it differently, but here is one idea - use map() to get all the option texts and then use toContain() jasmine matcher to check if there is a desired option:

public expectDropdownListOptionPresence(dropdownListID: string, optionText: string) {
var allOptions = element(by.id(dropdownListID)).$$('option').map(function (option) {
return option.getText();
});

expect(allOptions).toContain(optionText);
}

Or, you may filter() out the desired options by text and then expect you found something:

public expectDropdownListOptionPresence(dropdownListID: string, optionText: string) {
var foundOptions = element(by.id(dropdownListID)).$$('option').filter(function (option) {
return option.getText().then(function (text) {
return text === optionText;
});
});

expect(foundOptions.count()).toBeGreaterThan(0);
}

There is also that Select -> option abstraction, that may also help here.

SQL::Abstract generate insert with select query

You can do this:

use strict;
use warnings;
use SQL::Abstract;

my $sa = SQL::Abstract->new;
my $select = $sa->select('table1', "MAX(id) + 1, 'Something'");
my $full_sql = "INSERT INTO table1 (id, text) $select";

But really, why would you? Your example is trivial and, as pointed out by others, better served by just writing the SQL. If you intended "something" to be a variable, you could do this:

my $new_id = $sa->select('table1', 'MAX(id) + 1');
my $data = {
id => \["($new_id)"],
text => $some_value_from_elsewhere
};
my ($sql, @binds) = $sa->insert('table1', $data);


Related Topics



Leave a reply



Submit