Coded UI test - how to programmatically set a Select Option - c#

I'm writing some coded UI tests to automate testing of some webpages. I've chosen to write the code by hand rather than record a manual testing session.
I can modify controls on the page, for example an input box:
var edit = new HtmlEdit(browser);
edit.SearchProperties.Add(HtmlEdit.PropertyNames.Id, id);
edit.Text = value;
or click on a button:
var btn = new HtmlInputButton(browser);
btn.SearchProperties.Add(HtmlInputButton.PropertyNames.ValueAttribute, "Enter");
Mouse.Click(btn);
I can't work out how to set the value of a dropdown, e.g.
var select = new HtmlControl(browser);
select.SearchProperties.Add(HtmlControl.PropertyNames.Id, "Country");
select.????
the ValueAttribute is read only.
Any ideas?

Use an HtmlComboBox instead. HtmlControl is generic and has limited properties.
HtmlComboBox combox = new HtmlComboBox(browser);
combox.SearchProperties.Add(HtmlControl.PropertyNames.Id, "Country");
//Select by Index
combox.SelectedIndex = 0;
//Select by ITem
combox.SelectedItem = "India";

Related

asp.net Keep selected value on dynamically populated / created radiobuttonlist

I hope this question hasn't been asked before in this manner, if it has, I sadly can't find it!
I'm currently trying to create a User Control in which I want a client to select a questionnaire from a dropdownmenu which is dynamically populated on page_load like this:
DiscoverConnectData(); //gets the items from a database
var formItems = 0;
using (var osContext = new OrganizationServiceContext(_crmAccessor.OrganisationService))
{
var forms = (from f in osContext.CreateQuery("form") select f);
foreach (var form in forms)
{
var name = formulier.GetAttributeValue<string>("name");
var formId = formulier.GetAttributeValue<Guid>("formid");
DropDownList_RP.Items.Insert(formItems, new ListItem(naam, formId.ToString()));
formItems++;
}
}
I've also tried putting the above code inside an if (!Page.IsPostBack), however this didn't fix my problem... Okay, moving on. When the client has selected a questionnaire and hit a button, I have a piece of code that runs through all the questions and answers related to that specific questionnaire in a for each loop and creates Labels and RadioButtonLists for each question. First it loads the questions and answers related to the selected formId:
var curQuestionName = string.Empty;
var lblId = 0;
_formGuid = new Guid(DropDownList_RP.SelectedValue);
DiscoverConnectData();
using (var osContext = new OrganizationServiceContext(_crmAccessor.OrganisationService))
{
_answerList =
(from a in osContext.CreateQuery("answer")
join v in osContext.CreateQuery("question")
on a["question"] equals v["questionid"]
where ((EntityReference)v["form"]).Id == _formGuid
select new AnswerClass
{
AnswerQuestionId = v.GetAttributeValue<Guid>("questionid"),
QuestionName = v.GetAttributeValue<string>("name"),
Name = a.GetAttributeValue<string>("ansname"),
Score = a.GetAttributeValue<int>("score")
}).ToList();
}
After picking up this data and adding it to a list, I create the Questionnaire items:
foreach (var ant in _answerList)
{
if (ant.QuestionName != curQuestionName)
{
var vLabel = new Label();
var qid = "Q" + lblId;
vLabel.ID = qid;
vLabel.Text = ant.QuestionName;
Questionnaire.Controls.Add(vLabel);
_aRadioList = new RadioButtonList();
var rblId = "list" + lblId;
_aRadioList.ID = rblId;
Questionnaire.Controls.Add(_aRadioList);
Questionnaire.Controls.Add(new LiteralControl("<br />"));
curQuestionName = ant.QuestionName;
lblId++;
}
_aRadioList.Items.Add(new ListItem((" " + ant.Name), ant.Score.ToString()));
}
This all works fine and I'm getting a list with questions and answers neatly put underneath each other. So far so good!
However, the big problem I'm having is the losing of data on page postback. Whenever the user selects certain answers and I then want to show them a score, or do anything else - the page reloads and all my dynamically added controls are lost. Now I have fixed that issue by putting the relevant functionality above into a function and calling that again when I press the button to show a score, and this recreates the questions and answers, but I lose all the selectedvalues. I don't specifically even need to keep the controls on the page or anything, I just need to keep the selectedvalues in order to do stuff with them!
I can't create the controls on page_load or page_init because I need user input to know exactly which questionnaire to pull from the database. I can't use "hard-coded" values for the dropdownlist items either, because that's not the functionality we're aiming for. We need to pull it all from the database - which I can't access directly, I need to use the DiscoverConnectData() function.
Please, if anyone has any ideas in order to get the functionality I want (also if it means exploring a whole new 'avenue'), please let me know!

Panel Does not update TreeView

I have a TreeView that is displayed inside a panel. The data in the TreeView is based on data returned from the database. The first time, the data is correct. The second time, the TreeView is not refreshed, and the previous data is still showing in the tree. I checked the list that contain the data. The list returned the correct data. I've Google the issue, and could not resolved it with some of the answers that were posted. Here is a sample code of how the TreeView is being created and added to the Panel.
ReportGroups gr = new ReportGroups();
var Name = gr.GetReportName(groupID);
TreeView tr = new TreeView();
tr.BeginUpdate();
tr.Size = new Size(570, 600);
tr.Name = "Home";
tr.Nodes.Add("Reports Name");
tr.CheckBoxes = true;
if (Name.Count() > 0)
{
foreach (var item in Name)
{
if (item != null)
{
tr.Nodes[0].Nodes.Add(item.reportName);
}
}
}
tr.Nodes[0].ExpandAll();
tr.EndUpdate();
this.pDisplayReportName.Width = tr.Width * 2;
this.pDisplayReportName.Height = 300;
this.pDisplayReportName.Controls.Add(tr);
this.pDisplayReportName.Refresh();
What am I doing wrong?
try to add this.pDisplayReportName.Clear(); so data will not double up. :)
The easy option would be to use this.pDisplayReportName.Controls.Clear(); just after tr.EndUpdate();. But, this would cause an issue if you have other controls within the same Panel.
The best option would be to use this.pDisplayReportName.Controls.RemoveByKey("MyTree"); instead of this.pDisplayReportName.Controls.Clear();
And, another option would be to add a TreeView in design time (with name tr) rather than dynamically to the panel. Then, use tr.Nodes.Clear(); before tr.BeginUpdate(); and remove following two lines from your code.
TreeView tr = new TreeView();
.
.
.
this.pDisplayReportName.Controls.Add(tr);
Cheers

ASP.NET Chart Click, Pass Data and in New Tab

I am trying to get an asp.net chart and it's legend to allow me to open up another page in another tab passing the values of the piece of the chart I clicked on with it. I have been able to get it to open up another tab when clicking on the chart by doing the following but it does not pass the data.
Chart2.Series[0].LegendUrl = "chartdetails.aspx";
Chart2.Series[0].Url = "chartdetails.aspx";
Chart2.Series[0].LegendMapAreaAttributes="target=\"_blank\"";
Chart2.Series[0].LegendPostBackValue = "#VALY-#VALX";
Chart2.Series[0].MapAreaAttributes="target=\"_blank\"";
Chart2.Series[0].PostBackValue = "#VALY-#VALX";
If I leave out the urls and mapareaattributes I can then get it to go to the onclick where I am able to get the data, put it in a session variable and use Reponse.Redirect to open the new page where it sees the session variable data,however it doesn't open in another tab, it opens in the same tab.
Chart2.Series[0].LegendPostBackValue = "#VALY-#VALX";
Chart2.Series[0].PostBackValue = "#VALY-#VALX";
protected void Chart2_Click(object sender, ImageMapEventArgs e)
{
HttpContext.Current.Session["VAL"] = e.PostBackValue;
Response.Redirect("chartdetails.aspx", false);
}
How can I get it to do both? Does Response.Redirect have a way to open a new tab? Some research leads me to believe it does not. Is there a way to get both the server side onclick event to run, so I can set the session variable and the chart.series.url to fire after the server side click runs so the session variable would be set before I open the new tab?
I'm feeling like this may be a case of "I can't have my cake and eat it too."
As it turns out I can have my cake and eat it too. If I set the url, postbackvalues, and legendmapareaattributes in my Page_Load and set up the click for the chart to put the PostBackValue in the session variable when you click on the chart it saves the value in the session variable that is listed in the PostBackValue of the Series of the chart. It then opens in a new tab chartdetails.aspx where I can access the information from the session variable.
Chart2.Series[0].LegendUrl = "chartdetails.aspx";
Chart2.Series[0].LabelUrl = "chartdetails.aspx";
Chart2.Series[0].Url = "chartdetails.aspx";
Chart2.Series[0].LegendPostBackValue = "#VALY-#VALX";
Chart2.Series[0].LabelPostBackValue = "#VALY-#VALX";
Chart2.Series[0].PostBackValue = "#VALY-#VALX";
Chart2.Series[0].LegendMapAreaAttributes = "target=\"_blank\"";
Chart2.Series[0].LabelMapAreaAttributes = "target=\"_blank\"";
Chart2.Series[0].MapAreaAttributes="target=\"_blank\"";
protected void Chart2_Click(object sender, ImageMapEventArgs e)
{
HttpContext.Current.Session["VAL"] = e.PostBackValue;
}
I can't use postback to get the value on the series for some reason. So, I want to share my way inspired by #Adam that is loop over the series points after data binding the chart and set URL.
GET:
Series s=new Series("Test");
/*
* After Data bind to the series s
*/
for (int p = 0; p < s.Points.Count; p++)
{
s.Points[p].Url ="test.aspx?name1=value1&name2=value2";
s.Points[p].MapAreaAttributes = "target=\"_blank\"";
}
POST:
(I put javascript function in url. So, It will execute the javascript for me to send a form I created in the function to the text.aspx.)
Series s=new Series("Test");
/*
* After Data bind to the series s
*/
for (int p = 0; p < s.Points.Count; p++)
{
s.Points[p].Url ="javascript:(function(){" +
"var mapForm = document.createElement('form');mapForm.target = '_blank';" +
"mapForm.method = 'POST';mapForm.action = 'test.aspx';" +
"var mapInput = document.createElement('input');mapInput.type = 'hidden';" +
"mapInput.name = 'partID';mapInput.value = 'put any value you need';" +
"mapForm.appendChild(mapInput);document.body.appendChild(mapForm);" +
"mapForm.submit();document.body.removeChild(mapForm);})();";
}
Reference:
Javascript pass values using POST

AspxGridView selection at runtime

I have an aspxGridView on my page, and i create the columns at runtime. I also add CommandColumn with ShowSelectCheckBox set to true. But after i select one row and click a button to get the row values, AspxGridView.Selection.Count returns 0. I create this GridView on AspxButtons Click event also for paging, create the gridView on Page_Init. Here is the code to create the AspxGridView:
Colenter code hereorCollection colorCol = ConfAttributesManager.Instance
.GetColors();
if (colorCol.Count > 0)
{
GridViewDataTextColumn grdColorCodeColumn = new GridViewDataTextColumn();
grdColorCodeColumn.FieldName = "ColorCode";
GridViewDataTextColumn grdDescriptionColumn = new GridViewDataTextColumn();
grdDescriptionColumn.FieldName = "Description";
gv_Attributes.Columns.Clear();
gv_Attributes.Columns.Add(grdColorCodeColumn);
gv_Attributes.Columns.Add(grdDescriptionColumn);
GridViewCommandColumn grdCmdColumn = new GridViewCommandColumn();
grdCmdColumn.ShowSelectCheckbox = true;
grdCmdColumn.VisibleIndex = 0;
gv_Attributes.Columns.Add(grdCmdColumn);
gv_Attributes.DataSource = colorCol;
gv_Attributes.DataBind();
}
I don't know where do i make a mistake?
Thanks for your help.
It seems that you do not specify the ASPxGridView.KeyFieldName property that is required for Row Selection operation:
gv_Attributes.KeyFieldName = "ColorCode";
//gv_Attributes.KeyFieldName = Unique Key Field;

about toolStripDropDownButton

I have a form. I've added the strip down button using drag and drop in the form. How can I (in the program) create and fill the toolStripMenu Item? My menu could contain different element...with different names.
If you want to add items programmatically to a ToolStripDropDownButton just do:
var item1 = new ToolStripButton("my button");
toolStripDropDownButton1.DropDownItems.Add(item1);
var item2 = new ToolStripComboBox("my combo");
toolStripDropDownButton1.DropDownItems.Add(item2);
// etc ...
Instead, if you need to add other ToolStripDropDownButton or other elements directly to you menu (ToolStrip), just do:
var item1 = new ToolStripDropDownButton("my dropdown button");
toolStrip1.Items.Add(item1);
var item2 = new ToolStripProgressBar("my progress bar");
toolStrip1.Items.Add(item2);
// etc ...
EDIT:
You must do it after InitializeComponent() otherwise you won't be able to access to design-time added components, e.g.:
InitializeComponent();
// we're after InitializeComponent...
// let's add 10 buttons under "toolStripDropDownButton1" ...
for (int i = 0; i < 10; i++)
{
var item = new ToolStripButton("Button_"+i);
toolStripDropDownButton1.DropDownItems.Add(item);
}
For the DropDown property you need a ContextMenuStrip. The easiest way to find out how to fill it up is to drag&drop one from the toolbox onto your form, fill it up, select it in the DropDown property and afterwards take a look into the Designer.cs file to see how all the stuff is glued together.
The drawback of using the DropDownItems property is that you can't alter some properties like ShowImageMargin.

Categories