Note:
To answer this question, you shouldn't have to know anything about Selenium or WebDriver, just jQuery knowledge. That's where I don't have enough knowledge--precisely why I'm asking this question. :-) If you haven't heard of Selenium WebDriver, it's just a way to automate your website or web application from code (I'm using the C# client drivers in my example).
Also note, my FirefoxDriver object has native events turned on
Environment:
Below is a snippet of HTML and JavaScript to text you type in an input field autocomplete when you start typing. When you choose a value, it sets a hidden field with the id of the value chosen based on the name of the record entered into the input field. My goal is to mimic this autocomplete behavior in WebDriver by calling the ExecuteScript method to call some jQuery code. But since I know the exact value we're trying to match on in WebDriver, I want to mimic what the end-user would type into the field with this value. How can this be done?
If I can't get this working, my answer will be to just set the hidden field directly with the id. But I'd rather pass it the text so I can actually mimic what the end-user is doing. The WebDriver script will only have some or all of the text being typed (value being set in ac_variation_id), and will not have the record id being retrieved via AJAX (value being set in variation_id hidden field). Below, I'm setting both values. However, I just want a jQuery script that gets the id and sets the id, or mimics typing the value into the input.
So I have to solve it one of two ways:
- have WebDriver mimic autocomplete 100%
- have WebDriver call a JavaScript script (jQuery AJAX call) that does everything the page does except typing the value, so that the hidden field is set with the id returned for the chosen option
I just don't know how to do either.
Example jQuery script setting hidden field with id and input field with text:
Element.SetValueById(driver, "variation_id", discount.Variation); // set input field with text
Element.SetValueById(driver, "ac_variation_id", "123"); // set hidden field with id
public static void SetValueById(IWebDriver driver, string tagId, string newValue)
{
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("$('#" + tagId + "').val('" + newValue + "')");
}
HTML code and JavaScript code for autocomplete functionality:
<link rel="stylesheet" href="http://localhost/admin/css/vanilla/jquery.ui.autocomplete.css" media="screen" type="text/css" />
<script type='text/javascript' src="http://localhost/admin/js/vanilla/jquery-ui-1.7.1.custom.min.js"></script>
<script type="text/javascript" src="http://localhost/admin/js/vanilla/jquery.ui.autocomplete.ext.js"></script>
<script type="text/javascript" src="http://localhost/admin/js/vanilla/jquery.ui.autocomplete.js"></script>
<input type="text" name="ac_variation_id" id="ac_variation_id" value="" class="autocomplete" autocomplete="off" />
<button type="button" value="clear" name="cl_variation_id" id="cl_variation_id" onclick="$('#variation_id').val('');$('#ac_variation_id').val('');" >clear</button>
<input type="hidden" name="variation_id" id="variation_id" value="" />
<script>
$('#ac_variation_id').autocomplete({
ajax: 'http://localhost/admin/discount/ajax-auto-complete/variation',
match: function(typed) {
return this.name;//.match(new RegExp("^"+typed, "i")); had to comment that out to be able to type integration_id and display name
},
insertText: function(entity) {
return entity.name +' '+ (( entity.integration_id == undefined ) ? '' : entity.integration_id);
}
}).bind("activate.autocomplete",function(e, entity){
var id = '#'+($(this).attr('id').substring(3));//remove ac_ prefix
$(id).val( entity.id );
});
</script>
Screen shot of autocomplete lookup values after typing text into the input field:
There are two things to test with the autocomplete widget: 1) typing into the text field and 2) selecting an item from the auto complete list.
These answers are in ruby, but I would suspect there is a corresponding C# version
Typing into the text field
search_term = "stackoverflow.com"
input = find('#q')
input.click
# this is the same as input.set(search_term[0..5]) I believe
search_term[0..5].chars.each do |key|
input.native.send_key(key)
end
Selecting an item from the autocomplete list (by the text of the item)
search_term = "stackoverflow.com"
selector = ".ui-menu-item a:contains(\"#{#search_term}\")"
page.execute_script " $('#{selector}').trigger(\"mouseenter\").click();"
# I have 4 auto completes on my page, so I just wait until they all gone
wait_until do
autocompletes = all(:css, '.ui-autocomplete')
autocompletes.inject(true) { |x,autocomplete| x && !autocomplete.visible? }
end
I couldn't mimic the auto-complete and selecting the option, so I'm calling the JSON GET request to get the id of the hidden field, and setting the hidden field on the match of the first that it finds (in case there are more than one).
Element.SetHiddenFieldIdViaAutoCompleteJSON(driver, "/admin/discount/ajax-auto-complete/variation", "val", discount.Variation, "id", "variation_id");
public static void SetHiddenFieldIdViaAutoCompleteJSON(IWebDriver driver, string requestPage, string queryParam, string queryParamValue, string jsonObjectProperty, string hiddenFieldId)
{
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("$.ajax({ url: '" + Config.SITE_URL + requestPage + "',data:{'" + queryParam + "':'" + queryParamValue + "'},dataType:'json',type: 'GET',contentType: 'application/json',success: function(jsonObject) { $('#" + hiddenFieldId + "').val(jsonObject[0]." + jsonObjectProperty + "); } });");
}
Using the Selenuim IDE, and exporting to Java code, I adapted the results to following function so that I can choose which of my Autocomplete Comboboxes to change. ( This also in a 'Base' class, that all my PageObjets extend.
public BasicPage selectComboBox(int buttonIndex, String selection) {
driver.findElement(By.xpath("(//button[#type='button'])[" + buttonIndex + "]")).click();
driver.findElement(By.xpath("//html/body/ul/li/a[. = \"" + selection + "\"]")).click();
// delay till the selected element is visible
WebElement duh = (new WebDriverWait(driver, 10)).until( visibilityOfElementLocated(By.xpath("(//button[#type='button'])[" + buttonIndex +"]" )) ) ;
return this;
}
Related
I'm opening a new window to another .aspx page in which I pass a couple of parameters and I wanted to re-pass the parameter ID from the actual page:
<asp:Button ID="Button1" runat="server" CausesValidation="False" meta:resourceKey="btnAddRow2"
OnClientClick="window.open('SecondPage.aspx?type=Usuaris&id=SPECIALID', '_blank')" Text="Miau" />
As you can see, the type parameter works well but I don't have the slightest idea how to get the "specialID" from the current page which would be:
http://blablabla.com/FirstPage.aspx?SPECIALID=36
So i want to get that 36 (which is a dynamic number so I can't actually put a 36 directly over there) in order to open the second page as follows:
http://blablabla.com/SecondPage.aspx?type=Usuaris&SPECIALID=36
As I said at the beginning the user IS at he FirstPage.aspx and upon pressing a button will go to the SecondPage.aspx
hi you can change the OnClientClick to call a javascript function which will get the specialId and then call the window.open with the full string.
for example
function openWindow(){
var specialId = document.getElementById('someElement').value;
window.open('SecondPage.aspx?type=Usuaris&id=' + specialId, '_blank')"
}
I finally could do it doing the following in the FirstPage.aspx:
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
function AddUsuario() {
var id = getParameterByName("id");
window.open('SecondPage.aspx?type=Usuarios&id=' + id, '_blank');
location.reload();
}
On Page_Load() do following
Button1.Attributes.Add("onclick",
String.Format("window.open('SecondPage.aspx?type=Usuaris&id={0}', '_blank');",
Request.QueryString["SPECIALID"]));
So, I am dynamically generating jQuery using C# and sending it to the webpage.
The problem is it appears to be generating correct jQuery according to the file and according to Js Fiddle but it does not actually work on the page.
The jsFiddle is here http://jsfiddle.net/ER2hE/
Now I looked up how to send javacript to the website. It should work like this.
http://msdn.microsoft.com/en-us/library/bb359558.aspx
and my code which does that is this method
private void sendScript(string script)
{
const string someScript = "alertMe";
//send the built script to the website.
ScriptManager.RegisterStartupScript(this.Page, this.GetType(), someScript, script, true);
}
This is super simple it has worked for other pieces of code calling. But it has not for this instance.
The code that calls it is this in my C#
private void populateGroups()
{
//this generates correct javascript according to the file and JS fiddle but unfortunately doees not work.
string splitme = "USE ACES SELECT GroupName, GroupID FROM PrimaryGroup ORDER BY GroupName";
DataTable dt = fillDataTable(splitme);
string script = "";
foreach (DataRow dr in dt.Rows)
{
//add the locations to the <select> box
script += " $('#groupList').append('<option value=\" " + dr.ItemArray[1].ToString() + " \"> " + dr.ItemArray[0].ToString() + " </option>'); ";
}
sendScript(script);
JSErrorLog(script, "GROUPS");
}
The whole thing is being called on startup
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
populateMakes();
populateLocation();
populateGroups();
}
}
The jQuery its generating also works in JSFiddle I am pulling this from a method that writes the javascript it generates in a method calling here is the fiddle JSErrorLog.
http://jsfiddle.net/ER2hE/
Oh and my html in my aspx file looks like this
<div class="row2">
<span>Group</span>
<select id="groupList" multiple="multiple" onclick="setGroups()" class="normalsize">
</select>
</div>
I believe that is everything. I just want my stuff to work. I am willing to post any additional code, just ask. If you have an idea as to why its not working, let me know.
When does it actually execute that code? Before or after the element with id "groupList" exists in the DOM? My guess is before.
Solution? Wrap your code inside a document.ready handler.
jQuery(function($) {
$('#groupList').append('<option value=" 46 "> AC Units </option>');
// etc etc
});
Return simple string js code. And run it with eval()
I have a document library in sharepoint storing a word document.
If I click on the link to the document I get a dialog box with "you want to open this file in readonly or editmode etc" and can open it in edit mode, change it, save it directly in word an the changes are saved in the document library.
The link to the file in the document library looks like this:
<a onfocus="OnLink(this)"
href="/test/DocLib2/wordtest.docx"
onmousedown="return VerifyHref(this,event,'1','SharePoint.OpenDocuments','')"
onclick="return DispEx(this,event,'TRUE','FALSE','FALSE',
'SharePoint.OpenDocuments.3','1', 'SharePoint.OpenDocuments',
'','','','1','0','0','0x7fffffffffffffff','','')"
>wordtest</a>
How do I create this link in my own web part where I have the name of the file and document library? Without just copying the above code, that wouldn't be a good idea...
Is there some "official" method to achieve this?
Unfortunately it doesn't seem like there is a better option. But at least you can sort of figure out what the function definition is. The DispEx function is defined in the core.js file (but it's easier to read in the core.debug.js). Both are in 14\Templates\Layouts\1033 directory.
Here is the function definition:
function DispEx(ele, objEvent, fTransformServiceOn, fShouldTransformExtension,
fTransformHandleUrl, strHtmlTrProgId, iDefaultItemOpen, strProgId, strHtmlType,
strServerFileRedirect, strCheckoutUser, strCurrentUser, strRequireCheckout,
strCheckedoutTolocal, strPermmask)
Here is my guess on what they mean. Please feel free to add comments to correct any mistakes or omissions:
ele - [obj] the element
objEvent - [obj] the event object
fTransformServiceOn - [bool] (unknown functionality) defaults to True
fShouldTransformExtension - [bool] (unknown functionality) defaults to False
fTransformHandleUrl - [bool] (unknown functionality) defaults to False
strHtmlTrProgId - [string] name of the ActiveXControl to try to load defaults to SharePoint.OpenDocuments.3
iDefaultItemOpen - [int] indicator of default to Edit or Read defaults to 1
strProgId - [string] name of the ActiveX Control
strHtmlType [string] (unknown functionality) defaults to empty
strServerFileRedirect - [string] (unknown functionality)
strCheckoutUser [string] the ID of the user who has checked out the document
strCurrentUser - [string] the ID of the current user
strRequireCheckout - [string] indicator whether to force a checkout
strCheckedoutTolocal - [string] indicator of whether to use the Local Drafts folder
strPermmask - [string] permissions mask for the current user defaults to 0x7fffffffffffffff
There are clearly some inconsistencies in terms of using strings and integers to represent boolean values. It's also strange that your code has 17 parameters but I can only find a function definition with 15 parameters, so I'm not sure what those last two empty strings are for. Some of that is the nature of JavaScript, but it also just looks sloppy on the part of Microsoft.
This doesn't really answer the question, hopefully it helps you or someone else.
Chad Schroeder made a blog post on how to construct the javascript function call in C#. Taking into account a couple of settings, like force checkout and open in browser or client for instance.
private string GetFileViewScript(SPFile file)
{
string text = SPUtility.MapToControl(SPContext.Current.Web, file.Name, string.Empty);
string text2 = (file.Item.ParentList.DefaultItemOpen == DefaultItemOpen.Browser) ? "1" : "0";
SPFieldLookupValue sPFieldLookupValue = file.Item["CheckedOutUserId"] as SPFieldLookupValue;
string scriptLiteralToEncode = (sPFieldLookupValue == null) ? string.Empty : sPFieldLookupValue.LookupValue;
string text3 = (SPContext.Current.Web.CurrentUser != null) ? SPContext.Current.Web.CurrentUser.ID.ToString(CultureInfo.InvariantCulture) : string.Empty;
string text4 = file.Item.ParentList.ForceCheckout ? "1" : "0";
return string.Format(CultureInfo.InvariantCulture, "return DispEx(this,event,'{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}')", new object[]
{
"TRUE",
"FALSE",
"FALSE",
text,
text2,
text,
string.Empty,
string.Empty,
SPHttpUtility.EcmaScriptStringLiteralEncode(scriptLiteralToEncode),
text3,
text4,
(string)file.Item["IsCheckedoutToLocal"],
(string)file.Item["PermMask"]
});
}
Using DispEx in a link to a SharePoint document
I end up with adding this code
return DispEx(this,event,'TRUE','FALSE','FALSE',
'SharePoint.OpenDocuments.3','1', 'SharePoint.OpenDocuments','','','',
'1','0','0','0x7fffffffffffffff','','')
to my link tag because I wasn't able to find a better solution.
If there is any, please let me know.
DispEx does not work in Chrome unless the link is within a div that contains the document type in an attribute called app:
<div class="ms-vb itx" ctxname="ctx19" id="2" app="ms-word">
<a onfocus="OnLink(this)"
href="/test/DocLib2/wordtest.docx"
onmousedown="return VerifyHref(this,event,'1','SharePoint.OpenDocuments','')"
onclick="return DispEx(this,event,'TRUE','FALSE','FALSE',
'SharePoint.OpenDocuments.3','1', 'SharePoint.OpenDocuments',
'','','','1','0','0','0x7fffffffffffffff','','')">wordtest</a>
<span class="ms-newdocument-iconouter">
<img class="ms-newdocument-icon" src="/_layouts/15/images/spcommon.png?rev=23" alt="new" title="new">
</span>
</div>
Either you need to wrap it in such a div, and be sure to insert the correct application that will open the file, or make your own list by looking at the file extension:
$('.test_links').click(function(e) {
e.preventDefault();
if (!!window.chrome) {
var extenstion = this.href.substr(this.href.lastIndexOf('.') + 1);
var prefix = '';
switch (extenstion) {
case 'doc':
case 'docx':
prefix = 'ms-word:ofv|u|';
break;
case 'xls':
case 'xlsx':
prefix = 'ms-excel:ofv|u|';
break;
}
window.location.href = prefix + this.href;
} else {
DispEx(this, e, 'TRUE', 'FALSE', 'FALSE', 'SharePoint.OpenDocuments.3', '0', 'SharePoint.OpenDocuments', '', '', '', _spPageContextInfo.userId + '', '0', '0', '0x7fffffffffffffff');
}
});
I don't remember if there is an official ability to do it with JavaScript COM, but you can use the ASP.NET HyperLink control to generate the similar link. For instance put in layout
<asp:HyperLink ID="EditHl" runat="server" Text="Edit document"/>
and in code-behind something like
EditHl.Attributes["attribute name"] = "attribute value";
just use the same values from OOTB link, but change
/test/DocLib2/wordtest.docx
to URL of your document.
Here's the deal. Have a functioning web app using ASP.NET WebForms with a C# backend. The thing works fine, but I'm always looking to improve, as a beginner at this stuff. Right now, to deal with a user's search coming back with no results, I utilize the following, and was wondering if there was any cleaner way to do it, for future reference:
DataClass data = new DataClass();
var searchresults = data.GetData(searchBox.Text);
int datanumber = searchresults.Count();
if (datanumber == 0)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "javascript:alert('There were no records found to match your search');", true);
}
else
{
DropDownList1.Visible = true;
DropDownList1.Items.Clear();
DropDownList1.DataSource = searchresults;
DropDownList1.DataBind();
}
I agree with the not using popups, so you could always do something as simple as having a Label object on your page:
<asp:Label runat="server" id="lblResultMsg" ForeColor="Red" Visible="False" />
And then set the text dynamically (or add it as a property to the code) and set the label to be visible on postback if no results are found:
if (datanumber == 0)
{
lblResultMsg.Text = "There were no records found to match your search.";
lblResultMsg.Visible = true;
}
else
{
lblResultMsg.Text = "";
lblResultMsg.Visible = false;
// do your data binding
}
But there are quite a vast number of ways you could achieve something like this. Regarding your question about using the .Count from the Enumerable collection - there's nothing stopping you doing this as it's perfectly valid. The question is which method do you find more readable?
if you include the jquery ui dialog (http://jqueryui.com/demos/dialog/), you can simply call this to create a nice dialog box:
$('<div>message</div>').dialog({autoOpen:true,title:'Error'});
Personally I prefer to create a helper function for inserting the relevant javascript into the page, and only pass parameters to the function so that I don't need to worry about the messy details every time.
Something like :
public static void GrowlMessage(System.Web.UI.Control pageControl, string header = "", string message = "", bool sticky = false, string position = "top-right", string theme = "", bool closer = true, int life = 8)
{
string _js = "$.jGrowl('" + HttpContext.Current.Server.HtmlEncode(message) + "', { header:'" + header + "', sticky:" + sticky.ToString().ToLower() + ", position: '" + position + "', theme: '" + theme + "', closer: " + closer.ToString().ToLower() + ", life:" + life * 1000 + "});";
ScriptManager.RegisterStartupScript(pageControl, pageControl.GetType(),"Growl",_js, true);
}
The sample I have used also requires jQuery and the jGrowl library available here. And IMHO the messages are pretty. They are unobtrusive, the user does not need to click a button to make them go away, and they fade away after your specified amount of time.
But I agree with Mike, that if you don't have any records, you should just use the built in properties of a GridView (EmptyDataRowStyle and EmptyDataRowText) to display a 'no data matching your query' style message. Assuming that you're using a GridView at all, that is..
When it comes to user feedback, Impromptu is my friend. There is a nice ASP.NET implementation of Impromptu on Aaron Goldenthal's website: http://www.aarongoldenthal.com/post/2009/11/11/Using-jQuery-Impromptu-With-ASPNET.aspx
If you have decided to alert user via alert then please go ahead with light box effect..
http://www.designyourway.net/blog/resources/30-efficient-jquery-lightbox-plugins/
if you are still would like to go ahead with traditional alert then obviously its easy for you to fire it up on page load rather than attaching script to it..
')" ....>
Because if you require any change then you just need to alter the javascript alone and you dont need to build project again to test it...
Hope its useful for you..
Note: I'm using my own DLLs to render content so above coding may requires alteration because i did forget traditional asp codings.. :)
I am working on a donations website. In my page, I have a textbox which accepts a numeric value from the user (that is, money to be donated).
In my code-behind, I have a method which checks whether the value in the textbox is numeric. The method generates an error message if the number is invalid.
I also have a JavaScript which, after checking that the value in the textbox is numeric, opens a new tab to the website confirmation page, thanking the user for his donation. Here is the code of the javascript:
<script type="text/javascript">
function Open_Window()
{
var textbox = document.getElementById('DonationTextBox');
if (textbox.value != "")
{
if (isNan(textbox) == false)
{
window.open("DonationConfirmation.aspx")
}
}
}
</script>
The problem is that the tab is NEVER opened, even if the number is valid. Can you please help me solve this problem? Thank you.
P.S.
Here is the code of the button that initiates the validation:
<asp:ImageButton ID="PayPalButton2" runat="server" ImageAlign="Middle"
ImageUrl="Resources/Icons/PayPalCheckOut.gif"
onclick="PayPalButton2_Click" OnClientClick="Open_Window()"/>
The function name is isNaN. Note: The final 'N' is capital. That should solve your problem.
<script type="text/javascript">
function Open_Window()
{
var textbox = document.getElementById('<%=DonationTextBox.ClientID%>');
if (textbox.value != "" && !isNaN(textbox.value)) {
window.open("DonationConfirmation.aspx");
}
}
</script>
edit
instead of isNan should be isNaN (javascript is casesensitive)
Shouldn't this line...
if (isNan(textbox) == false)
be this instead...
if (isNan(textbox.value) == false)
First, I would recommend explicitly parsing the number, not relying on the implicit ToNumber operation that will be applied when you pass a string into isNaN. Presumably your users are inputting decimal, so if it's meant to be a whole number (e.g., 10), use:
var num = parseInt(textbox.value, 10);
If it's meant to be a number with a fractional component (e.g., 10.5), use:
var num = parseFloat(textbox.value);
You probably want parseFloat for a currency value.
Then your if condition becomes isNaN (note that the final N is capped) on num:
<script type="text/javascript">
function Open_Window()
{
var textbox = document.getElementById('DonationTextBox');
var num = parseInt(textbox.value, 10);
if (!isNaN(num))
{
window.open("DonationConfirmation.aspx")
}
}
</script>
And lastly, are you sure that the client-side ID of the textbox really is 'DonationTextBox'? ASP auto-generates client-side IDs, you may need to use ClientID instead, e.g.:
var textbox = document.getElementById('<%=DonationTextBox.ClientID%>');
Here is a stripped down working jsFiddle example:
http://jsfiddle.net/pjgalbraith/QZeSF/
The html:
Open
<textarea id="donationTextBox">1</textarea>
And the js:
function openWindow() {
if($('#donationTextBox').val() && isNaN($('#donationTextBox').val()) === false)
window.open("http://www.google.com/", "mywindow");
}
$(document).ready(function() {
$('#PayPalButton2').click(function(){
openWindow();
});
});