Get attributes of a Web Element - c#

Is there any technique that can be used to get attributes of an element in a web page? I'm trying to get properties of elements such as Links, Buttons, check boxes and Radio Buttons for test automation purposes to make it more easier. But I couldn't find any solution. I heard that CODEDUI can be used. If so, how can I implement it?

As you have mentioned html tag in your question, one solution is using jQuery like this:
$( elemement ).attr( "attribute" )

you do not get property of html page with program language php and c# you should useing jquery and Similar libraries or JavaScript .
for this problem you must using this code in select html with jquery :
<select id="selectVehicle">
<option value="1" data-year="2011">Mazda</option>
<option value="2" data-year="2015">Honda</option>
<option value="3" data-year="2008">Mercedes</option>
<option value="4" data-year="2005">Toyota</option>
</select>
$("#selectVehicle").change(function () {
alert($(this).find(':selected').data("year"));
});
or get attribute of elements :
$(this).attr('data-fullText');

Related

ASP.NET MVC OnChange Razor

I have searched if there is any way that ASP.NET MVC can handle OnChange event without using neither Javascript but all the results return the OnChange of DropdownList...
Is there any way to achieve this? I want to OnChange an file input to post file data.
<select id="myElement">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
you can use javascript code
document.getElementById("myElement").addEventListener("change",function(e){
e.currentTarget.value
});
Without JavaScript, no, if you want to do anything dynamically, you need JavaScript.
As HTML is just for markup, and nothing else, you'll have to have the page refresh, or use JavaScript.

ASP MVC3 - Marking Option as Selected

I have a form that has 2 select boxes (Country and State) that have their available options contained in partial HTML files, due to their size.
In the event of a validation error, I want to mark the option the user originally selected as "selected".
Since the options are contained in a partial HTML file, I am not sure that this is even possible:
Any suggestions would be great. Thanks
// Inside the View
<select name="State" id="State">
#Html.Partial("States", Model);
</select>
#Html.ValidationMessageFor(model => model.State)
The partial simply contains the HTML:
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
<option value="AA">Armed Forces Americas</option>
<option value="AE">Armed Forces Europe, Middle East</option>
[...]
Instead of using strange Partial, why you just do not use helper DropDownList. You can find many examples, like this one http://peternewhook.com/2013/02/asp-net-mvc-selectlist-selectedvalue-dropdownlistfor/

html escape characters

I have the following:
<form name="input" method="get" action="http://site:8083/Default.aspx?DC=" target="foo" onSubmit="window.open('', 'foo', 'width=1100 height=500,status=no,resizable=yes,scrollbars=yes')">
<select name="DC">
<option value="1&Type=type1">1</option>
<option value="2&Type=type2">2</option>
<option value="3&Type=type3">3</option>
<option value="4&Type=type4">4</option>
<option value="5&Type=type5">5</option>
<option value="6&Type=type6">6</option>
<option value="7&Type=type7">7</option>
</select>
<input type="submit" value=">>"/>
</form>
Basically my querystring should be something like DC=1&Type=type1 the problem I have is that when I click the >> button above the html screws up the stirng by changing & to %26 and = to %3D
How can I make the value stay as I have it in the code above?
Yes, that's the expected behaviour, the HTTP GET works this way, values are encoded to difference from keys. In GET, browsers append key=value pairs separated by '&' and starting with a '?'; you shouldn't specify them by yourself in the url.
I think you should change your code in this way, maybe it's what you want to get:
<form name="input" method="get" action="http://site:8083/Default.aspx" target="foo" onSubmit="updateType()">
<select name="DC">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<!-- etc... -->
</select>
<input type="hidden" name="Type"/>
<input type="submit" value=">>"/>
</form>
and in Javascript...:
function updateType(){
// set up 'Type'
document.forms[0].Type.value = 'Type' + document.forms[0].DC.value;
// open popup
window.open('', 'foo', 'width=1100 height=500,status=no,resizable=yes,scrollbars=yes')
return true;
}
Forms are designed to submit key/value pairs of data where the key is the name of the control and the value is the value. They will not mash arbitrary strings together.
Design your server side program to expect input in the format the forms are designed to use.
...and this is correct. Request-Parameters [get] should be encoded.
When you get the Request, your Server should decode them [apache/php] or i don't know ASP but in php it gaves a function called urldecode() ...
Try using:
HttpUtility.UrlDecode()
Further reading
So what you're trying to do is have two different query string set to two different values when a single item on your form is set. While your attempted solution is a neat idea, clearly the browser is actively preventing you from using that method (for security reasons) and rightly so. I see two solutions that would work well for you.
Use the form to send just one query string (i.e. DC) and on the server side, determine what the value of 'Type' should be. If it's really, really important to have both values as query strings (for example you're re-using a Control that only looks at those query strings) then use Server.Transfer on the server side to add the additional query string(s).
Provide two types of inputs on the form. One being the select box that you have there, another being either a hidden select box, a hidden input field, or whatever else fits for you. Add Javascript to the page such that when the selection is changed on the visible select box you set the hidden field to the correct value, then when the form is submitted both inputs are each submitting only one query string, as the browser is designed to support.
Here's an implemention of choice #2:
<script type="text/javascript">
var DCMapping = new Array();
DCMapping["1"] = "type1";
DCMapping["2"] = "type2";
DCMapping["3"] = "type3";
DCMapping["4"] = "type4";
DCMapping["5"] = "type5";
DCMapping["6"] = "type6";
DCMapping["7"] = "type7";
function selectionChanged(selectBox)
{
alert(DCMapping[selectBox.value]);
document.getElementById('hidType').value = DCMapping[selectBox.value];
}
</script>
<form name="input" method="get" action="http://site:8083/Default.aspx" target="foo"
onSubmit="window.open('', 'foo', 'width=1100 height=500,status=no,resizable=yes,scrollbars=yes')">
<select name="DC" onchange="selectionChanged(this)">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
<input type="hidden" name="Type" id="hidType" value="1" />
<input type="submit" value=">>"/>
</form>
You should use:
HttpUtility.UrlDecode();
To convert escape characters in the url
You can remove the method and action from the <form>. Instead extend the JavaScript/VBScript to pick up the value of the selected option and catenate it with the url root.
You don't need to mess with url decoding, the %.. should work fine as-is.

Using databinding to populate a HtmlSelect with optgroup elements in asp.net

I am using asp.net and have a HtmlSelect element on my page (with runat="server"). Normally I will just set the DataSource to some loaded data and DataBind. However in this case the data has one level of hierarchy and I want to represent this in the HTML with an optgroup. Google hasn't come up with any joy - is this even possible?
I've had a similar problem to solve and this is how I did it. Before I continue I did find all sorts of re-fracturing methods for lists, which probably do work. However, I did not think this the best route to go for my solution. Here's what I did:
I also needed to know which option a user selected in the drop down list but found that you can't build drop down list with option groups in ASP.net. So to do this I just built a simple <select> with <optgroup>'s and <option>'s in each group. Then I placed a <div> with a hidden textbox, which had the runat="server" set. I set the "onchange" event, on the select, to change the text value of the hidden textbox to whatever the value the option was, which was selected by the user, using javascript. Then, when the post back occurs, the code behind had access to the value the user selected, via the hidden textbox's runat="server". Problem solved! The code looks something like this:
Code in the HTML (aspx):
<div id="selectDiv">
<select id="selectItems" onchange="document.getElementById('hiddenselectDiv').getElementsByTagName('input')[0].value = this.options[this.selectedIndex].value;">
<optgroup label="Johannesburg">
<option value="Wilropark">Wilropark</option>
<option value="Bryanpark">Bryanpark</option>
<option value="Hurlingham">Hurlingham</option>
<option value="Midrand ">Midrand </option>
<option value="Glenvista">Glenvista</option>
<option value="Sunninghill">Sunninghill</option>
<option value="Edenvale ">Edenvale </option>
<option value="Parkhurst">Parkhurst</option>
</optgroup>
<optgroup label="Cape Town">
<option value="Tokai">Tokai</option>
<option value="Durbanville">Durbanville</option>
</optgroup>
<optgroup label="Durban">
<option value="Musgrave">Musgrave</option>
</optgroup>
<optgroup label="Pretoria">
<option value="Hatfield">Hatfield</option>
</optgroup>
</select>
</div>
<div id="hiddenSelectDiv">
<!--
Note: You probably want to set the hidden value to the first item you have seleced when you build the <select> object.
-->
<input type="hidden" id="selectedItem" value="Wilropark">
</div>
In the Code behind (C#):
if (!string.IsNullOrEmpty(selectedItem.Value))
{
//validation or whatever you want....
}
I've been looking around for the same thing, and it doesn't look like ASP supports this natively. One Google search found someone recommending a library at http://www.codeplex.com/SharpPieces, but I've never used it and I don't know how good it is. Other people talk about writing your own renderer for optgroup support.

Call JQuery from C# PageLoad

Is there any way how to call JQuery function from PageLoad C# file?
Basically I have some selects, some inputs which are not generated by C# code but are defined in .aspx file manually. When I send form get query to another page I would like to set same variables that are defined in querystring. I know how to do that when I use runat="Server" but I want pure JQuery solutions without having runat="server" objects.
Example:
Select input:
<form method="get" action="/list/search">
<select id="txtSearchFullTyp" name="typ">
<option value="all">Sell, Rent</option>
<option value="1">Sell</option>
<option value="2">Rent</option>
<select>
</form>
Now after sending querystring to another page I am analyzing querystring and running function with defined parameter.
I want to be able to set form to querystring defined as "typ". I dont know how to do it from C# when I dont have runat="Server" option.
Is there a way to do this?
Thanks.
Here's a code for it. It's quite hacky but nevertheless it answers your question:
<form method="get" action="/list/search">
<select id="txtSearchFullTyp" name="typ">
<option value="all">Sell, Rent</option>
<option value="1">Sell</option>
<option value="2">Rent</option>
<select>
</form>
<asp:Literal runat="server" ID="litJqueryCode"/>
and from the code behind you can do the following
litJqueryCode.Text = "<script type='text/javascript'>$(function(){ $('#txtSearchFullTyp').val('"+ Request.QueryString["dropdownValue"] +"') })</script>";

Categories