Selenium + MVC4: how to find element contained in a specific parent element - asp.net-mvc

Im doing MVC4 application and Im creating Selenium tests for it. My problem is that I want to find element <ul class="connectors ui-sortable"></ul> but only contained in <li class="empty zoneLi ui-droppable" data-order="X">
My solution was one added below, but it's not working:
private static IWebElement FindZoneByDataOrder(IWebDriver webDriver, int dataOrderId)
{
var parentForDropElement = webDriver.FindElement(By.XPath("//li[#data-order='" + dataOrderId + "']"));
var dropElement = parentForDropElement.FindElement(By.XPath("//ul[#class='connectors ui-sortable']"));
return dropElement;
}
part of my HTML page:
....
<div class="zones-system-creator" style="min-height: 430px;">
<ul id="zonesCreateSystem" class="">
<li data-zoneid="24829" class="empty zoneLi ui-droppable" data-order="1">
<div id="warningInfoBoxContainer"></div>
<ul class="products ui-sortable"></ul>
<ul class="connectors ui-sortable"></ul>
</li>
</ul>
</div>
....
UPDATE1: I changed zoneID to dataOrderId, because we care only about dataOrderId.

I see few issues in your code. You talked about data-order, but passed zoneId in the method. So I have added another parameter called dataOrder and searching for the LI element which has both zone and data order attributes:
"//li[#data-zoneid='" + zoneId + "'][#data-order='"+dataOrder+"'"]
Then searching for ul element of having specified class. Also combined finding the element into a single line:
Code:
private static IWebElement FindZoneByDataOrder(IWebDriver webDriver, int zoneId, int dataOrder)
{
return driver.findElement(By.xpath("//li[#data-zoneid='" + zoneId + "'][#data-order='"+dataOrder+"']//ul[#class='connectors ui-sortable']"))");
}
Edited Answer following question edit:
return driver.findElement(By.xpath("//li[#data-order='"+dataOrderId+"']//ul[starts-with(#class, 'connectors')]"));

I found 2 working solutions if you want to find one element inside the other with Selenium:
result = webDriver.FindElement(By.XPath("//li[#data-order='" + dataOrderId+ "']"));
var result2 = result.FindElement(By.XPath(".//ul[#class='connectors']"));
result = webDriver.FindElement(By.CssSelector("li[data-order='" + dataOrderId+ "']"));
result = result.FindElement(By.CssSelector("ul[class='connectors']"));

Related

Implementing preview for markdown text

I am working on Ruby on Rails project and I have implemented markdown syntax for some text descriptions in my project using redcarpet gem.
It works like charm allowing to convert markdown text to HTML as simply as
<%= markdown some_text_variable %>
But now I want to implement preview feature rendering just small part of the full text.
The following naive construction
<%= markdown some_text_variable[0..preview_length] %>
will not work because it can easily break down MD syntax resulting in confusing constructions (imagine, for example, spliting original string on the half of image link).
I came up with
<%= markdown some_text_variable[0..preview_length].split(/\r?\n/)[0..-2].join("\r\n")) %>
but it does not deal, for example, with code blocks.
Is there any way to implement such kind of preview for MD text?
Using markdown.js and / or showdown should work. Here's a StackO with the same question and answer. I personally have used showdown in an Ember app before to render a live preview of the text as it's being typed (via 2-way data binding), and it worked flawlessly.
In the fiddle below, I wrote a little Showdown parser that takes in a string of markdown, splits it on a newline (returns an array of tags), and iterates through the array. On each iteration, it removes the tags, checks the length of the resulting string, and then compares it to the max character count for the preview. Once the next iteration surpasses the max character count, it returns the preview. The do loop ensures that you will always get at least one blob of html as a preview.
Fiddle
$(function() {
var converter = new Showdown.converter();
var previewMax = 200;
$('button').click(function() {
var content = $('#markdown').val(),
charCount = 0,
i = 0,
output = '';
if (!content) {
return $('div.preview').html("Please enter some text.");
}
var mark = converter.makeHtml(content);
var mark_arr = mark.split('\n');
while (charCount < previewMax) {
var html = mark_arr[i];
var text = htmlStrip(html);
if ((charCount + text.length) > previewMax) {
var overflow = (charCount + text.length) - previewMax;
var clipAmount = text.length - overflow;
html = jQuery.truncate(mark_arr[i], { length: clipAmount });
}
output += html;
charCount += text.length;
i++;
};
$('div.preview').html(output);
$('div.full').html(mark);
});
function htmlStrip (html) {
var div = document.createElement('div');
div.innerHTML = html;
var text = div.textContent || div.innerText || "";
return text;
}
});
REVISION
I updated the function using jQuery Truncate to cut the final string into an elipses so that all your previews are the same length as the others. Also, I realized that the original function returned a long string of undefined' over and over when no text was entered, so there is a check to eliminate that. Since this loop will always return at least one html item now, I changed the do loop to a while loop for easier reading. Finally, if you want your truncation to always end at a word boundary, pass the words: true option when you call it. Obviously, this will not give you the same level of truncation for every preview, but it will improve legibility. That's it!
I want to share my preview version it was quite simple with showdown.js and prism.js syntax highlighting.
Prism.js is syntaxing easily with JavaScript and CSS. All you need to pick specific languages and download it to assets folder. Or you can specify it to specific pages.
This is going to happen in realtime preview, in a form.
In Rails form:
<div class="col-md-12">
<div class="form-group">
<%= f.label :body %>
<%= f.text_area :body, class: "form-control", rows: 10 %>
</div>
</div>
<div class="col-md-12">
<h1> Preview Markdown </h1>
<div class="form-group markdownOutput"></div>
</div>
And add this script right below a form page.
<script>
function mkdown(){
var converter = new showdown.Converter(),
$post_body = $("#post_body");
// This line will keep adding new rows for textarea.
function postBodyLengthDetector(post_body){
var lines = post_body.val().split("\n");
post_body.prop('rows', lines.length+5);
}
// Textarea rows in default '10', when focusing on this. It will expand.
$post_body.focus(function(){
postBodyLengthDetector($(this));
$('.markdownOutput').html(converter.makeHtml($post_body.val()));
});
// All simple magic goes here, each time when texting anything into textarea
//it will be generated to markdown. You are able to see preview right below of textarea.
$post_body.keyup(function() {
postBodyLengthDetector($(this));
var value = $( this ).val(),
html = converter.makeHtml(value);
$('.markdownOutput').html(html);
});
}
$(mkdown);
$(document).on("turbolinks:load", mkdown);
</script>

Typoscript wrap a wrapper

I'm fairly new to typoscript and the manual (which I tried to read) isn't that helpful.
I have the following typoscript:
30 = TEXT
30 {
typolink {
parameter = 1079
returnLast = url
}
wrap (
<li class="mod-metanav--item">
<a class="mod-metanav--link" href="|">
Help
</a>
</li>
)
}
Now, the word «Help» needs to be translated.
I found out that I can use something like this to translate text in TS
5 = TEXT
5.data = LLL:fileadmin/content/translation.xml:help
5.wrap(
<li class="mod-metanav--item">
<span style="font-size: 10px; color: #777;">|</span>
</li>
)
But, I need to kind of double-wrap the text (5) into the link (30) and that's where I fail.
I found some documentation on wrap2/wrap3 and innerWrap/outerWrap, but there aren't any examples that match my case and I can't adapt the examples I find to my case...
The first part (30) was made by someone else and I have the suspicion that there's another way to achieve this...
Any hints?
For all who run into the same problem - here's what I came up with:
30 = TEXT
30 {
typolink {
parameter = 1079
returnLast = url
}
wrap (
<a class="mod-metanav--link" href="|">
)
prepend = COA
prepend{
10 = TEXT
10.value = <li class="mod-metanav--item">
}
append = COA
append{
10 = TEXT
10.data = LLL:fileadmin/content/translation.txt:hilfe
10.wrap(
|
</a>
</li>
)
}
}
The trick is to pre-/append the additional parts.
Still it feels like an ugly hack and I'm sure I'm supposed to put the HTML-part into the template.

add collapsible into another collapsibles (nested collapsibles) in JQM

I want to get this:
But I need to create the HTML dynamically because it's loaded via AJAX from an external ressource.
I already asked this question here (JQM: Dynamic nested collapsibles - $(collapsible).collapsible() & $(collapsible).trigger('create')) but I got it completly wrong with lists and collapsibles, so I figured a new question would be better.
Here is what I got so far:
function loadTipps() {
console.log("Lade Tipps..");
var categoriesURL = tippsURL+"?type=kategorien&callback=?"; // url for the categories
$.getJSON(categoriesURL,function(data) {
console.log("Lade Kategorien..");
var DIV_tipps_komplett = $("#tipps_komplett");
$.each(data, function(key,value){
var kategorie_ID = value.id;
var kategorie_NAME = value.name;
var collapsible_HTML = $('<div data-role="collapsible"></div>');
var kategorie_Ueberschrift_HTML = $('<h3>'+kategorie_NAME+'</h3>');
var tipps_kategorie_HTML = $('<div id="tipps_kategorie'+kategorie_ID+'" data-role="collapsible-set"></div>');
var tippURL = tippsURL+"?type=tipp&kat_id="+value.id+"&callback=?"; // url for the tipps of the current category
$.getJSON(tippURL,function(data2) {
$.each(data2, function(key2,value2){
var tipp_Ueberschrift_Text_HTML = '<div data-role="collapsible"><b>'+value2.name+'</b><p>'+value2.text+'</p><br></div>';
tipps_kategorie_HTML.append(tipp_Ueberschrift_Text_HTML);
}); //<--each
});//<--getJSON
collapsible_HTML.append(kategorie_Ueberschrift_HTML);
collapsible_HTML.append(tipps_kategorie_HTML);
DIV_tipps_komplett.append(collapsible_HTML);
});//<--each
DIV_tipps_komplett.trigger('create');
});//<--getJSON
}
This results in:
As you can see, the items in the first collapsible set are not in another collapsible set. Any ideas why?
Try changing the line:
var tipp_Ueberschrift_Text_HTML = '<div data-role="collapsible"><b>'+value2.name+'</b><p>'+value2.text+'</p><br></div>';
to
var tipp_Ueberschrift_Text_HTML = '<div data-role="collapsible"><h3>'+value2.name+'</h3><p>'+value2.text+'</p><br></div>';
The Collapsible needs the <h3> element instead of <b> to render.
Here is a demo of your exact code with the AJAX calls removed: http://jsfiddle.net/ezanker/RkLuV/
By the way, this is pretty much the answer I gave you here: collapsible list with collapsed items in jqm, it would probably have been better to continue the conversation there.

Angular pagination not updating when bound list changes due to filtering on an input text box

Here's the scenario:
I am using an ASP.NET MVC site with Angular JS and Boostrap UI. I have a dynamic ul list populated by data fed through a controller call to AngularJS, filtering on that list through an input search box. The list is also controlled through pagination (UI Bootstrap control) that I've setup to show 10 results per page for the list of 100 or so items. This list is filtered as the user types in the search box, however I would like the pagination to update as well so consider the following example:
The list has 10 pages of items (100 items), the user types some text in the input search box which filters the list down to 20 or so items, so the pagination should be updated from 10 pages to two pages.
I figure there must be a $watch setup somewhere, perhaps on the list items after it has been filtered and then update the pagination page count, however I'm pretty new to AngularJS so can someone please explain to me how this could be done?
Thanks very much. I have posted my code below:
<div data-ng-app="dealsPage">
<input type="text" data-ng-model="cityName" />
<div data-ng-controller="DestinationController">
<ul>
<li data-ng-repeat="deals in destinations | filter: cityName |
startFrom:currentPage*pageSize | limitTo:pageSize">{{deals.Location}}</li>
</ul>
<br />
<pagination rotate="true" num-pages="noOfPages" current-page="currentPage"
max-size="maxSize" class="pagination-small" boundary-links="true"></pagination>
</div>
var destApp = angular.module('dealsPage', ['ui.bootstrap', 'uiSlider']);
destApp.controller('DestinationController', function ($scope, $http) {
$scope.destinations = {};
$scope.currentPage = 1;
$scope.pageSize = 10;
$http.get('/Deals/GetDeals').success(function (data) {
$scope.destinations = data;
$scope.noOfPages = data.length / 10;
$scope.maxSize = 5;
});
});
destApp.filter('startFrom', function () {
return function (input, start) {
start = +start; //parse to int
return input.slice(start);
};
});
Because your pagination is a combination of chained filters, Angular has no idea that when cityName changes, it should reset currentPage to 1. You'll need to handle that yourself with your own $watch.
You'll also want to adjust your startFrom filter to say (currentPage - 1) * pageSize, otherwise, you always start at page 2.
Once you get that going, you'll notice that your pagination is not accurate, because it's still based on destination.length, and not the filtered sub-set of destinations. For that, you're going to need to move your filtering logic from your view to your controller like so:
http://jsfiddle.net/jNYfd/
HTML
<div data-ng-app="dealsPage">
<input type="text" data-ng-model="cityName" />
<div data-ng-controller="DestinationController">
<ul>
<li data-ng-repeat="deals in filteredDestinations | startFrom:(currentPage - 1)*pageSize | limitTo:pageSize">{{deals.Location}}</li>
</ul>
<br />
<pagination rotate="true" num-pages="noOfPages" current-page="currentPage" max-size="maxSize" class="pagination-small" boundary-links="true"></pagination>
</div>
JavaScript
var destApp = angular.module('dealsPage', ['ui.bootstrap']);
destApp.controller('DestinationController', function ($scope, $http, $filter) {
$scope.destinations = [];
$scope.filteredDestinations = [];
for (var i = 0; i < 1000; i += 1) {
$scope.destinations.push({
Location: 'city ' + (i + 1)
});
}
$scope.pageSize = 10;
$scope.maxSize = 5;
$scope.$watch('cityName', function (newCityName) {
$scope.currentPage = 1;
$scope.filteredDestinations = $filter('filter')($scope.destinations, $scope.cityName);
$scope.noOfPages = $scope.filteredDestinations.length / 10;
});
});
destApp.filter('startFrom', function () {
return function (input, start) {
start = +start; //parse to int
return input.slice(start);
};
});
The version shared on jsfiddle is compatible with ui-bootstrap 0.5.0 but from 0.6.0 onwards there have been breaking changes.
Here is a version that uses the following libraries:
angular 1.2.11
angular-ui-bootstrap 0.10.0
bootstrap 3.1.0
Here is a plunker for the same:
Angular UI Bootstrap Pagination
Hello I tried to hook this up with Firebase using Angular Fire and it only works after I type something in the search input. In the $scope.$watch method, I used Angular Fire's orderByPriorityFilter to convert the object to an array.
$scope.$watch('search', function(oldTerm, newTerm) {
$scope.page = 1;
// Use orderByPriorityFilter to convert Firebase Object into Array
$scope.filtered = filterFilter(orderByPriorityFilter($scope.contacts), $scope.search);
$scope.lastSearch.search = oldTerm;
$scope.contactsCount = $scope.filtered.length;
});
Initial load doesn't load any contacts. It's only after I start typing in the input search field.

using local storage to store and retrieve current clicked item on listview jquery mobile

I have a dynamic list view and what I am trying to do is store the list that is CURRENTLY being clicked to local storage and retrieve it. I have the following code, it seems to work but I'm not sure so could someone give me a heads up if it's correct as I need to be doing more things with the data.
The html for list view:
<ul data-role="listview" id="newsfeedposts" data-theme="a" data-overlay-theme="a" data-inline="true"data-inset="true"></ul>
the markup for each list item:
markup += '<li><img src="' + thumb_url + '">' +'<h4>' + name + '</h4><p>' +'posted this photo....</p><p>'+likes+'<img src="images/facebook-like-16.png"></p></li>';
$('#newsfeedposts').append(markup);
addPostToLocalStorage(posts);
$('#newsfeedposts').listview("refresh");
The method to add the item to storage and retrieve it;
function addPostToLocalStorage(facebookPost){
$(".item").on('click', function (){
var i = $('.item').index(this);
console.log(i);
localStorage['results'] = JSON.stringify(facebookPost[i]);
storedItem = (localStorage['results']) ;
console.log(storedItem);
alert("this post was added to local storage");
//retrieve list item
var retrievedItem = localStorage.getItem(['results']);
console.log( 'retrieved: ' + retrievedItem);
});

Resources