Protractor Memo - Javascript sort() case insensitive etc.
2017. 4. 13. 09:08 |
var arr = ["Banana", "Orange", "Apple", "Mango", "apple", "1apple"];
arr.sort();
["1apple", "Apple", "Banana", "Mango", "Orange", "apple"]
It is case sensitive.
To make it case insensitive
var arr = ["Banana", "Orange", "Apple", "Mango", "apple", "1apple"];
arr.sort(function(a,b) {
a = a.toLowerCase();
b = b.toLowerCase();
if( a == b) return 0;
return a < b ? -1 : 1;
});
["1apple", "Apple", "apple", "Banana", "Mango", "Orange"]
or
var arr = ["Banana", "Orange", "Apple", "Mango", "apple", "1apple"];
arr.sort(
function(a, b){
if (a.toLowerCase() < b.toLowerCase()) return -1;
if (a.toLowerCase() > b.toLowerCase()) return 1;
return 0;
}
);
["1apple", "Apple", "apple", "Banana", "Mango", "Orange"]
Protractor
Check not exist
expect(element(by.css('.switch')).isPresent()).to.become(false).and.notify(next);
expect(items.length).toBe(0);
element.all(locator).map(mapFunction)
<ul class="items"> <li class="one">First</li> <li class="two">Second</li> <li class="three">Third</li> </ul>
let items = element.all(by.css('.items li')).map(function(elm, index) { return { index: index, text: elm.getText(), class: elm.getAttribute('class') }; }); expect(items).toEqual([ {index: 0, text: 'First', class: 'one'}, {index: 1, text: 'Second', class: 'two'}, {index: 2, text: 'Third', class: 'three'} ]); // Or using the shortcut $$() notation instead of element.all(by.css()): let items = $$('.items li').map(function(elm, index) { return { index: index, text: elm.getText(), class: elm.getAttribute('class') }; }); expect(items).toEqual([ {index: 0, text: 'First', class: 'one'}, {index: 1, text: 'Second', class: 'two'}, {index: 2, text: 'Third', class: 'three'} ]);
Example
step.Then(/^Verify Search Result search by "([^"]*)"$/, (searchName:string, done:Callback)=>{ let cName = customerViewPage.customerNameList();
cName.map(function(eachName){
return eachName.getText().then(function(cNameList){
return cNameList;
});
}).then(function(cNameList){
for(let i = 0; i < cNameList.length; i++){
console.log(cNameList[i] + ' contains ' + searchName);
expect(cNameList[i]).is.contains(searchName);
}
done();
});
});
element.all(locator).each(eachFunction)
<ul class="items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
element.all(by.css('.items li')).each(function(element, index) { // Will print 0 First, 1 Second, 2 Third. element.getText().then(function (text) { console.log(index, text); }); }); // Or using the shortcut $$() notation instead of element.all(by.css()): $$('.items li').each(function(element, index) { // Will print 0 First, 1 Second, 2 Third. element.getText().then(function (text) { console.log(index, text); }); });
Example
step.Then(/^Check sorting$/, (done: Callback)=> {
let sorted = [] , unSorted = [];
let cName = customerViewPage.customerNameList();
let i = 0;
cName.each(function(eachName){
eachName.getText().then(function(name){
// unSorted[i] = name.toLowerCase();
unSorted[i] = name;
i++;
});
}).then(function(){
sorted = unSorted.slice();
// sorted.sort();
sorted.sort(function(a, b) {
a = a.toLowerCase();
b = b.toLowerCase();
if ( a === b) {return 0;}
return a < b ? -1 : 1;
});
for(let i = 0; i < sorted.length; i++){
console.log(' Array1 : ' + sorted[i] +' is equals to Array2 : ' + unSorted[i]);
expect(sorted[i]).is.equal(unSorted[i]);
}
done();
});
});
element.all(locator).then(thenFunction)
ElementArrayFinder.prototype.then
Retrieve the elements represented by the ElementArrayFinder. The input function is passed to the resulting promise, which resolves to an array of ElementFinders.
<ul class="items"> <li>First</li> <li>Second</li> <li>Third</li> </ul>
element.all(by.css('.items li')).then(function(arr) { expect(arr.length).toEqual(3); }); // Or using the shortcut $$() notation instead of element.all(by.css()): $$('.items li').then(function(arr) { expect(arr.length).toEqual(3); });
'TDD Project > Protractor_Cucumber' 카테고리의 다른 글
[Protractor] Asynchronous Testing with Protractor’s ControlFlow (0) | 2017.05.11 |
---|---|
[Protractor] Protractor-Perf - Performance Testing using Protractor (0) | 2017.05.04 |
[Cucumber] Automation Testing Using Cucumber Tool and Selenium (0) | 2017.04.25 |
Protractor Memo - treat array, sorting check (0) | 2017.04.12 |
Protractor For Beginners Part 1 (0) | 2017.04.07 |