How to insert an option value into a select tag with CasperJS? - select

Well, the question is very self-explanatory.
Right now, I'm front of a form which has a select tag with a couple of options already. But I must insert a new one, with a different value that I will receive from a .json file.
The thing is: I haven't been able to find a suitable solution from the CasperJS documentation.
I've tried something like this:
this.fill('form.coworkerdiscountcode', {
'CoworkerDiscountCode.DiscountCode': ['Value1']
});
But no results. Any ideas?
Thanks in advance.

You can execute any javascript code by passing it to casper.evaluate like this:
casper.evaluate(function() {
var x = document.getElementById("coworkerdiscountcode");
var option = document.createElement("option");
option.text = "Kiwi";
x.add(option);
});

Related

How can I manipulate a string in dart?

Currently I'm working in a project with flutter, but I realize there is a need in the management of the variables I'm using.
Basically I want to delete the last character of a string I'm concatenating, something like this:
string varString = 'My text'
And with the help of some method or function, the result I get:
'My tex'
Am I clear about it? I'm looking for some way which helps me to 'pop' the last character of a text (like pop function in javascript)
Is there something like that? I search in the Dart docs, but I didn't find anything about it.
Thank you in advance.
You can take a substring, like this:
string.substring(0, string.length - 1)
If you need the last character before popping, you can do this:
string[string.length - 1]
Strings in dart are immutable, so the only way to do the operation you are describing is by constructing a new instance of a string, as described above.
var str = 'My text';
var newStr = (str.split('')..removeLast()).join();
print(newStr);
Another way:
var newStr2 = str.replaceFirst(RegExp(r'.$') , '');
print(newStr2);

Protractor - unable to get h2 value

I'm unable to get value from h2 tag using protractor.
Html Code:
<h2 class="ng-binding">7</h2>
I need this 7 value. This is from website "http://juliemr.github.io/protractor-demo/". I am adding 5+2.
Appreciate your help.
Try xpath
Here I am getting the element with ID, moving one position back and then looking for the h2
var result = element(by.xpath('//*[#id=\'gobutton\']/../h2'))
result.getText().then(function(value){
expect(value).toBe('7');
})
Or look directly to the h2:
element(by.xpath('//h2[#class='ng-binding']'))
In case you plan to always have 7 as a result then you can also look for the cssContainingText
var result = element.all(by.cssContainingText('.ng-binding', '7')).first();
in this case I am using .all and .first because this will return 2 elements so I am telling protractor to use always the first one
So your code should be something like this:
it('Should access the page and perform sum', function() {
browser.get('http://juliemr.github.io/protractor-demo/');
element(by.model('first')).sendKeys(5);
element(by.model('second')).sendKeys(2);
element(by.id('gobutton')).click();
var result = element(by.xpath('//*[#id=\'gobutton\']/../h2'))
result.getText().then(function(value){
console.log('result is '+value)
expect(value).toBe('7');
})
});
As per the url given, After clicking Go it loads for some time. So add some wait and then try to getText() with the below locator.
browser.sleep(5000);
element(by.css('form>h2.ng-binding')).getText().then((text: String) =>{
expect(text).toBe("7");
})
Hope it helps you...
First, you find the element, for example with a by.css call. Then you use getText method to retrieve the desired tag content. Finally, you assert it to be equal to the value you expect:
result = element(by.css('h2.ng-binding'))
expect(result.getText()).toEqual('7')

element.all always returning count as 0

I am trying to test a dynamic web table using protractor and trying to find the count of headers, rows and cols but always getting 0 as the count
var x = element.all(by.xpath('//table//thead//tr//th'))
x.count().then(function(c){
console.log(c);
});
I tried using element.all(by.css ) as well and it returns the same , can anyone help?
I used selenium and able to retrieve the value, so xpath is not wrong, but I have to use protractor to fetch the same.
Selenium script which is working
List col = driver.findElements(By.xpath("//div[#class='table-wrapper']//table//thead//tr/th"));
System.out.println(col.size());
html
Try the below code
var x = await element.all(by.css('table[title="status"]'))
//Add wait if the table take more time to load
x.count().then(function(c){
console.log(c);
});
In general, you should avoid xpath since it's very inefficient.
This should work for you:
var table = element(by.css('table.table'));
table
.element(by.css('thead'))
.all(by.css('tr th'))
.count()
.then(function(count) {
console.log('count:',count);
});

error .match expression results null

I am working on a mail merge script. I have used Logger.log to find out that the error is in the expression that tells match what to find. In my case I am trying to pull all the keys that are inside ${xxxxxxx}. Below is what I have and I need help cleaning it up because at this point it returns null.
var template = "This is an example ${key1} that should pull ${key2} both keys from this text."
var templateVars = template.match(/\$\{\"[^\"]+\"\}/g);
Thanks for any guidance anyone can share on this problem.
-Sean
I am not really familiarized with Google Apps Script, but I think this code in Javascript can help you.
It looks for all the ocurences of ${key} and returns each value inside the ${ }. I think that is what you are looking for.
var template = "This is an example ${key1} that should pull ${key2} both keys from this text.";
var matches = template.match(/\$\{[0-9a-zA-Z]*\}/mg);
console.log(matches);
for ( var i = 0; i < matches.length; i++ ) {
console.log(matches[i].replace(/[\$\{|\}]/gm, ""));
}

InnerHTML IE 8 doesn't work properly? Resetting form

Yeah this works in FF and Chrome, but for some reason wont work in IE 8. I'm using a radio button to clear a section of a form.. that section is a select box, but I don't want to leave the area empty - instead I want to reset it to what it was when the page loaded. At the moment IE8 is just leaving me with an empty small select box.
Html:
<select id="city_select" disabled="true" name="location_id" onchange="show_search_button();"><option selected>Select your city</option> </select>
Javascript:
document.getElementById('city_select').innerHTML = "<option selected>Select your city</option>";
I've also tried using location_id instead of city_select in the javascript but to no avail.. innerText and innerContent dont work either.. though the inner.HTML works in IE8 for an earlier function, but that isnt trying to innerHTML into a form. Does anybody know why this works in Chrome and FF but not IE8? and is there a solution to this? Any help appreciated thanks!
Try this:
document.getElementById('city_select').options.length = 0;
Then create a new option and push it onto the options array of the select. The options are a tricky bit that don't behave like other markup.
Edited to show how to create an option:
var sel = document.getElementById('city_select').options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
sel.options.push(opt);
sel.selectedIndex = 0;
There are supposed to be 4 way of assigning new options to a select element. Some work in some scenarios and others work in others. Look here - How to Add options to <SELECT>, in IE Windows Mobile 5
For me, Robusto's solution didn't work due to three reasons:
1) sel variable in the first line is assigned document.getElementById('city_select').options.length = 0; instead of simply holding the select element (for later use in 4th and 5th line) and then deleting options in a next line, like this:
var sel = document.getElementById('city_select');
sel.options.length = 0;
2) the 4th line sel.options.push(opt) (or later suggested sel.options[0] = opt) throws an Object doesn't support this property or method error. Instead use this:
sel.appendChild(opt);
3) apart from assigning values to options you must also assign text to display. You do it like this:
opt.innerText = "Select Your City - displayed";
Therefore, to sum up the whole piece:
var sel = document.getElementById('city_select');
sel.options.length = 0;
var opt = document.createElement('option');
opt.value = "Select Your City";
opt.innerText = "Select Your City - displayed";
sel.appendChild(opt);
sel.selectedIndex = 0;