Why select list does not show selected option - c#

When user selects value from dropdown list, the selected value is not shown after page reload. Only "All" is shown as selected. If I give an attribute in the as :
<option value="#trChannel.TrCode" selected = "#trChannel.TradeChanelDisplayText">#trChannel.TradeChanelDisplayText</option>
The value All is never shown as selected and when page is loaded the last option from list is shown. How can I fix this?
<section>
<table id="filters">
<tr>
<td style="width:180px">
<span class="snsSmallTextBold">Filter by Trade Channel: </span>
</td>
<td>
<select name="TradeChannelCode" style="width:200px" id="selectedTradeChannel">
<option value="" id="all">All</option>
#foreach (var trChannel in Model.FilterableTrChannels)
{
<option value="#trChannel.TrCode">#trChannel.TradeChanelDisplayText</option>
}
</select>
</td>
<td>
<div>
<button id="addTrChannel" style="background-color:white">Add Trade Channel</button>
</div>
</td>
<td style="width:410px"></td>
<td style="width:210px; float:right">
<span class="snsSmallTextBold">Filter by Subtrade Channel: </span>
</td>
<td>
<select name="SubtradeChannelCode" style="width:200px" id="selectedSubtradeChannel">
<option value="">All</option>
#foreach (var subtrChannel in Model.FilterableSubTrChannels)
{
<option value="#subtrChannel.SubTrCode">#subtrChannel.SubtradeChanelDisplayText</option>
}
</select>
</td>
<td>
<button id="addSubtrchannel" style="background-color:white">Add Subtrade channel</button>
</td>
</tr>
</table>
</section>
<script type="text/javascript">
$(document).ready(function () {
$("#filters select").change(function () {
load();
});
});
function load() {
var url = window.location.href.split('?')[0];
url = url + '?TradeChannelCode=' + $("#selectedTradeChannel").val() + '&SubtradeChannelCode=' + $("#selectedSubtradeChannel").val();
window.location.href = url;
}
</script>

Related

Reusability of XPath with different name

I have a modal, within that modal there are various card. Those cards have dropdown. It looks some what like the screenshot below. They all have different select name. Is there any way to write a method in C# so I dont have to do //select[#name='test1a']or //select[#name='test1b'] for all dropdown?
HTML Code below
<table class="row-inner-table" border="0" cellpadding="1" cellspacing="1">
<tbody>
<tr class="row-content">
<td class="field-name">test1a</td>
<td class="field-value">
<select name="test-4" onchange="calc(document.myForm.TOTAL_8,this,24)">
<option value="" score="0"> </option>
<option value="118" score="0">test1b </option>
<option value="124" score="1">test1c</option>
<option value="120" score="2">test1d</option>
</select>
<input type="HIDDEN" name="PREVIOUS_24" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test2</td>
<td class="field-value">
<select name="test-5" onchange="calc(document.myForm.TOTAL_8,this,25)">
<option value="" score="0"> </option>
<option value="121" score="0">hi</option>
<option value="113" score="1">hello</option>
<option value="114" score="2">C ya </option>
</select>
<input type="HIDDEN" name="PREVIOUS_25" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test 3</td>
<td class="field-value">
<select name="test-6" onchange="calc(document.myForm.TOTAL_8,this,26)">
<option value="" score="0"> </option>
<option value="123" score="0">hehe</option>
<option value="112" score="1">haha</option>
<option value="119" score="2">001</option>
</select>
<input type="HIDDEN" name="PREVIOUS_26" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test4</td>
<td class="field-value">
<select name="test-7" onchange="calc(document.myForm.TOTAL_8,this,27)">
<option value="" score="0"> </option>
<option value="111" score="0">404</option>
<option value="122" score="1">402/option>
<option value="117" score="2">403</option>
</select>
<input type="HIDDEN" name="PREVIOUS_27" value="0">
</td>
</tr>
<tr class="row-content">
<td class="field-name">test 5 </td>
<td class="field-value">
<select name="test-8" onchange="calc(document.myForm.TOTAL_8,this,28)">
<option value="" score="0"> </option>
<option value="115" score="0">Relaxed</option>
<option value="125" score="1">Tense, rigid</option>
<option value="116" score="2">Very tense or rigid</option>
</select>
<input type="HIDDEN" name="PREVIOUS_28" value="0">
</td>
</tr>
<tr class="row-content-total">
<td class="field-name total-label">Total</td>
<td class="field-value">
<input type="TEXT" size="5" name="TOTAL_8" value="0">
</td>
</tr>
</tbody>
</table>
It should be as easy as something like this
public SelectElement dropdownElement(By locator)
{
return new SelectElement(driver.FindElement(locator));
}
and you call it like
SelectElement select = dropdownElement(By.CssSelector("select[name='test-5']"));
Here is another version of C.Peck's solution which will cut down the repetitive Xpath (or CSS) string code, assuming the base of the Xpath is always the same which in the OP it appears to be
public SelectElement SelectMenuItem(string menuValue)
{
var menuString = $"//select[#name='{menuValue}']";
return new SelectElement(driver.FindElement(By.XPath(menuString)));
}
SelectElement selectOne = SelectMenuItem("test-4");
SelectElement selectTwo = SelectMenuItem("test-5");
EDIT:
To show you the A-Z on reusability for this, it would look something like this...
Method to return Select Element with dynamic XPath string:
public SelectElement SelectMenuItem(string menuValue)
{
var menuString = $"//select[#name='{menuValue}']";
return new SelectElement(_driver.FindElement(By.XPath(menuString)));
}
Method to set value for dropdown, by XPath:
public void SetMenuValue(string menuValue, string menuDropDown)
{
SelectElement select = SelectMenuItem(menuDropDown);
select.SelectByText(menuValue);
}
Finally, method to set the specific values you want, passing in value first, then XPath next:
public void Input_Values()
{
SetMenuValue("118", "test-4");
SetMenuValue("121", "test-5");
}
You don't need that last method, you could call SetMenuValue from anywhere, that was just another example of how you could do this

Unable to click button which is inside iframe and table in Selenium webDriver with c#

I am new to c# and I am working with Selenium chrome webdriver in c#. I am trying to click button which inside table. I am not able to identify and click button.The hierarchy of button is (Page > PopOver > PopOverFrame > Table1 > Table2 > Button to click )
Any help with this would be much appreciated thank you.
my code is :
*/
// Closing old tab, keeping control in new tab and trying to perform click operation
var currentWindow = BaseTest.Driver.CurrentWindowHandle;
var availableWindows = new List<string>(BaseTest.Driver.WindowHandles);
foreach (string mywindows in availableWindows)
{
if (mywindows != currentWindow)
{
Driver.SwitchTo().Window(mywindows).Close();
}
else
{
Driver.SwitchTo().Window(currentWindow);
// performing click action on RUN REPORT button
IWebElement popOver = Driver.FindElement(By.Id("popOver"));
IWebElement popOverFrame = popOver.FindElement(By.Id("popOverFrame"))
IWebElement table1 = popOverFrame.FindElement(By.XPath("//*[#id='form1']/table"));
IWebElement Table2 = table1.FindElement(By.XPath("//*[#id='tblReport']"));
Table2.FindElement(By.Id("contentPlaceholder_btnPrint")).Click();
}
}
Please refer to attached screenshot of what html page looks like.
<html xmlns="http://www.w3.org/1999/xhtml" ><title>
<div id="divReportHeader">
<span id="contentPlaceholder_lblReportDescHeader" class="reportHeader">Description</span>
<br>
<span id="contentPlaceholder_lblReportDescription">Offer</span>
</div>
<table>
<tbody><tr>
<td style="vertical-align: top;">
<div style="margin: 2px; padding: 8px;">
<table width="1000px" style="border-spacing: 0; padding: 0" id="tblReport">
<tbody><tr>
<td></td>
</tr>
<tr>
<td><div id="contentPlaceholder_generalInformation" class="reportHeader">Information</div></td>
</tr>
<tr>
<td>
<span id="contentPlaceholder_lblRptInfo">
This report may require more information. Click "Run Report" to view the report inline.
</span>
<br>
</td>
</tr>
<tr>
<td></td>
</tr>
<tr>
</tr>
<tr>
</tr>
<tr>
<td>
<span id="contentPlaceholder_lblfrom">abc </span>
<select name="ctl00$contentPlaceholder$ddlfromYear" onchange="javascript:setTimeout('__doPostBack(\'ctl00$contentPlaceholder$ddlfromYear\',\'\')', 0)" id="contentPlaceholder_ddlfromYear">
</select>
<span id="contentPlaceholder_lblFilter5" style="display: block; margin-top: 10px; margin-bottom: 4px;">Additional columns to be included:<br>Due to potential page size limitations, additional custom columns should only be selected when intending to view inline or export as Excel.</span>
<input type="submit" name="ctl00$contentPlaceholder$btnSelectAll" value="Select All" id="contentPlaceholder_btnSelectAll" class="roundedButton">
<input type="submit" name="ctl00$contentPlaceholder$btnDeselectAll" value="Deselect All" id="contentPlaceholder_btnDeselectAll" class="roundedButton" style="margin-bottom:5px;">
<table id="chkList2" class="correctCheckboxes">
<tbody><tr>hkList2_0" value="Select"><label for="chkList2_0">Help</label></td>
</tr>
</tbody></table>
</td>
</tr>
<tr>
<td>
<br>
<div id="contentPlaceholder_parameterNotes" class="reportHeader">
Notes
</div>
<br>
<span id="contentPlaceholder_txtParameterNotes">None</span>
</td>
</tr>
<tr>
<td>
</td>
</tr>
<tr>
<td>
<input type="submit" name="ctl00$contentPlaceholder$btnPrint" value="RUN REPORT" onclick="disable('contentPlaceholder_btnPrint');__doPostBack('ctl00$contentPlaceholder$btnPrint','');" id="contentPlaceholder_btnPrint" class="roundedButton" style="width:130px;">
</td>
</tr>
</tbody></table>
</div></td>
</tr>
</tbody></table>
<iframe id="ifrmDownload" style="display: none;"></iframe>
<iframe id="ifrmStatus" src="statuscheck.aspx" style="display: none;"></iframe>
</form>
</body></html>
Try this:
//remove this line IWebElement popOver = Driver.FindElement(By.Id("popOver"));
Driver.SwitchTo().DefaultContent();
IWebElement popOverFrame = Driver.FindElement(By.Id("popOverFrame"))
Driver.SwitchTo().Frame(popOverFrame);
//remove this line IWebElement table1 = popOverFrame.FindElement(By.XPath("//*[#id='form1']/table"));
IWebElement Table2 = Driver.FindElement(By.XPath("//*[#id='tblReport']"));
Table2.FindElement(By.Id("contentPlaceholder_btnPrint")).Click();
Try the below one:
driver.switchTo().frame(driver.findElement(By.xpath(iframeXpath)));
And once the operations are completed inside iFrame, switch back to default content.
driver.switchTo().defaultContent();

not able to hover on multiple tabs in a for each loop using webdriver

I have a form editor web application for creating forms and workflows. The home page of the application contains a menu on the left side.
The menu looks like .
when I mouseover any tab, I get a tool tip.
I am currently trying to verify this using selenium webdriver and c#. Following is the HTML for the menu
<div class="tabmenu">
<div class="tab-list">
<div class="mask"></div>
<ul class="level1" style="left: 0px;">
<li class="l1-item top" title="">
<span class="selection-top"></span>
<table class="mover">
<tbody>
<tr>
<td class="tooltip">
<span>Create Flow</span>
</td>
<td>
<span class="logo logo12">
<span class="status status-none"></span>
</span>
</td>
</tr>
</tbody>
</table>
<span class="selection-bottom"></span>
<div class="hr"></div>
</li>
<li class="l1-item prev" title="">
<span class="selection-top"></span>
<table class="mover" style="left: -110px;">
<tbody>
<tr>
<td class="tooltip">
<span>Dashboard</span>
</td>
<td>
<span class="logo logo11">
<span class="status status-none"></span>
</span>
</td>
</tr>
</tbody>
</table>
<span class="selection-bottom"></span>
<div class="hr"></div>
</li>
</ul>
</div>
</div>
When I mouseover any tab, the <li class="l1-item> have another class added <li class="l1-item tooltip">
Following is the code that I have written
[FindsBy(How = How.CssSelector, Using = "ul.level1 > li")]
[CacheLookup]
private IList<IWebElement> LeftMenuItems;
public bool isToolTipPresentOnEachTab()
{
foreach (IWebElement ele in LeftMenuItems)
{
Actions action = new Actions(driver);
action.MoveToElement(ele).Perform();
if((ele.FindElement(By.CssSelector("td.tooltip > span")).GetAttribute("textContent")) != "Create Flow"
&& !ele.GetAttribute("class").Contains("showtooltip")){
return false;
}
}
return true;
}
I am getting the tooltip only in the first iteration of the loop and the test fails in the 2nd iteration.
Can someone help me with this ?

Tab index is not working from select to next button in asp.net

<td >Club City</td>
<td > <input style="width: 98%" id="SnapI_txtCity" title="* Club City" class="boxStyle" onclick="" onchange="" maxlength="50"> </td>
<td>Sprinkler system present in club</td>
<td > <select id="SnapII_ddlSprinklerSys" title="* Please select Sprinkler system present in club " onclick="" onchange="">
<option selected="" value="SEL">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<tr>
<td style="text-align: right">
<img id="imgNext" onclick="ShowDiv('N')" name="imgNext" src="../_gfx/next.gif" runat="server"></td>
</tr>
Upto select button tab index is working,after select it is circulating from first.How i will solve it in asp.net?without adding tabindex it works upto last textbox.but it doesot goes to next image.
$(document).ready(function () {
$("input,select,img").each(function (i) { $(this).attr('tabindex', i + 1); });
});

MVC #Html.DropDownList Getting Error with SelectList in ViewBag

I have a list which I created in the controller:
var PayList = new[] {
new ListEntry { Id = 1, Name = "" },
new ListEntry { Id = 2, Name = "Yes" },
new ListEntry { Id = 3, Name = "No" }
};
ViewBag.PayList = new SelectList(PayList, "Id", "Name");
In the view I have the following:
#Html.DropDownList("Pay", new SelectList(ViewBag.PayList,"Id","Name"))
When I try to display it, it says the following:
DataBinding: 'System.Web.Mvc.SelectListItem' does not contain a property with the name 'Id'. Not sure why this is not working.
Also how do I default a value to the select list. I like to default it to "Yes". I thought there was a way to do do this from the controller.
Your ViewBag.PayList is already the type of SelectList. I don't see a reason to create the SelectList twice, so shouldn't it just be:
#Html.DropDownList("Pay", ViewBag.PayList)
or
#Html.DropDownList("Pay", ViewBag.PayList as SelectList)
(I don't ever use the ViewBag, so I'm not sure if your version is strongly typed).
Just use
#Html.DropDownList("Pay", ViewBag.PayList)
In your view
Try this Way:
<div id="divmsg" style="color: green; font-weight: bold">
#ViewBag.Msg
</div>
<div id="divmsg2" style="color: red; font-weight: bold">#ViewBag.Msg2</div>
<div id="quality" style="width: 80%;" align="center">
<input type="hidden" value="#ViewBag.ProjectId" id="ProjectId_" class="projectId"/>
<input type="hidden" value="#ViewBag.ProjectName" id="ProjectName_" class="projectName"/>
<input type="hidden" value="#ViewBag.UserId" class="UserId_" id="UserId"/>
<input type="hidden" value="#ViewBag.TempId" class="TempId_" id="TempId" />
<div class="toggle-contents">
<table width="100%" id="qualitygoal">
<tr>
<td class="even" align="left">
#Html.Label("Project Id")
</td>
<td class="even" align="left">
#ViewBag.ProjectId
</td>
</tr>
<tr>
<td class="projectname" align="left">
#Html.Label("Project Name")
</td>
<td class="projectname" align="left">
#ViewBag.ProjectName
</td>
</tr>
</table>
<table width="100%" id="qualitygoal1" class="tbl">
<tbody>
<tr>
<th align="center">DestinationColumns</th>
<th align="center">SourceColumns</th>
</tr>
#foreach (var data in Model)
{
<tr>
<td>
<span class="spanStatus" id="lblStatus_#data.TempId" destinationID = "#data.Destination">#data.Destinationvalue</span>
<select class="status" id="ddlStatus_#data.TempId">
<option value="0">--Select--</option>
<option value="4">TICKET ID</option>
<option value="5">DESCRIPTION</option>
<option value="6">TICKET CATEGERY</option>
<option value="7">SEVIORITY/PRIORITY</option>
<option value="8">STATUS</option>
<option value="9">CREATED DATE</option>
<option value="10">CREATED BY</option>
<option value="11">ASSIGNED TO</option>
<option value="12">ASSIGNED DATE</option>
<option value="13">REPSONSE ETA</option>
<option value="14">RESOLUTION ETA</option>
<option value="15">RESPONSE DATE</option>
<option value="16">RESOLUTION DATE</option>
<option value="17">ROOT CAUSE/MODULE</option>
<option value="18">REOPEN FLAG (Y/N)</option>
<option value="19">CLOSE DATE</option>
<option value="20">SLA MET (Y/N)</option>
</select>
</td>
<td>
<span class="spanSource" id="lblSource_#data.TempId" >#data.Source</span>
<input class="Source" id="txtSource_#data.TempId" type="text" value="#data.Source" maxlength="30" />
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="edit" id="Edit_#data.TempId" type="button" value="Edit" />
<input class="update" id="Update_#data.TempId" type="button" value="Update" />
</td>
<td class="Gcancle" id="Canc_#data.TempId">
<input class="gridcancel" id="Cancel_#data.TempId" type="button" value="Cancel" />
</td>
</tr>
</table>
</td>
<td>
<table style="width: 50%;">
<tr>
<td>
<input class="delete" id="Delete_#data.TempId" type="button" value="Delete" />
</td>
</tr>
</table>
</td>
</tr>
}
</tbody>
</table>
</div>
<div align="right">
<input type="button" value="Add New Row" class="Add" />
<input type="button" value="Save" class="saved" />
<input type="button" value="Close" class="cancel" />
</div>
</div>

Categories

Resources