Disable clicking on tabPanel in TabContainer - c#

I want to disable clicking and going from one tabPanel to another. Like here http://www.eldan.co.il/en/ the user can't select another step. I tried something like this
if (Tabs.ActiveTabIndex == 0)
{
Tabs.Tabs[1].Enabled = false;
}
but it hides the tab...

I found something that helped me.
...prevent switching to the tab on click depending on form validation
Returning false in the tabs select handler prevents the clicked tab from becoming selected.
$('#example').tabs({
select: function(event, ui) {
var isValid = ... // form validation returning true or false
return isValid;
}
});

Related

If statement condition where element equals element

I am testing a web page that contains a table. You can click a "Create new" link to add records to the grid. Once "Create New" is clicked, a dialog appears with some text boxes, another grid and a Cancel and Save button. You can then click a link to add a record to the dialog's grid which makes another dialog appear with text boxes and a Cancel and Save button. At my test level class, I currently click on these buttons then wait for the dialog's to open or close. I need to formulate a generic method which encompasses both the click and the wait, for each and every button. So instead of 2 lines of code at my test level to click an element and wait for a window, I would have one line of code that handles that. Below is my dilemma:
I need to be able to apply an If condition where a passed parameter of IWebElement equals a certain IWebElement, but it does not allow me to do this. The if statement doesn't find a match for some reason, so code inside the if statement never gets reached. Am I missing something? If not, is there a workaround?
NOTE: Using button.text == SaveOrganizationBtn.Text is a workaround, but would fail in my specific case, because some of these buttons might not have been loaded into the HTML for a certain test (i.e. A form has not been invoked), so the if statement fails. It would never be able to grab the Text property because it cant find the element in the first place.
Example code:
ClickButton(SaveNetworkBtn);
public void ClickButton(IWebElement button)
{
if (button == SaveOrganizationBtn)
{
SaveOrganizationBtn.Click();
WaitForOrganizationFormToClose();
}
if (button == SaveNetworkBtn)
{
SaveNetworkBtn.Click();
WaitForNetworkFormToClose();
}
Use the Equals() method for your scenario. == will not work for this. you need to check it as if(button.Equals(SaveOrganizationBtn)). The result for this will be true, if it is the same object else it will return false.
I hope, it will help you.
I found a solution, however awkward it might look. I will mark this as the answer if no one else provides a better solution.
ClickButton(SaveNetworkBtn);
public bool ClickButton(IWebElement button)
{
bool buttonClicked = false;
if (Driver.FindElements(By.Id("SaveOrg-Button-ID")).Count > 1)
{
if (button == SaveOrganizationBtn)
{
SaveOrganizationBtn.Click();
WaitForOrganizationFormToClose();
buttonClicked = true;
return buttonClicked;
}
}
if (Driver.FindElements(By.Id("SaveNet-Button-ID")).Count > 1)
{
if (button == SaveNetworkBtn)
{
SaveNetworkBtn.Click();
WaitForNetworkFormToClose();
buttonClicked = true;
return buttonClicked;
}
}
return buttonClicked;
}
I don't fully understand your scenario but it seems like something like this might work... you will have to decide. Instead of passing in button, just check if the button exists on the page, if it does... click it.
ClickButton();
public bool ClickButton()
{
IReadOnlyCollection<IWebElement> saveOrg = Driver.FindElements(By.Id("SaveOrg-Button-ID"));
if (saveOrg.Any())
{
saveOrg.ElementAt(0).Click();
WaitForOrganizationFormToClose();
return true;
}
IReadOnlyCollection<IWebElement> saveNet = Driver.FindElements(By.Id("SaveNet-Button-ID"));
if (saveNet.Any())
{
saveNet.ElementAt(0).Click();
WaitForNetworkFormToClose();
return true;
}
return false;
}
NOTE: .Any() works the same as .Count > 0, I found it a while back and like it a little better but it's a personal preference.

How to check if class attribute contains something?

I have two buttons, back and next:
By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[1]")
By.XPath("/html/body/div/div[3]/main/div/div/form/div[1]/div[2]/div[1]/nav/div/button[2]")
First goes to previous page, second goes to the next page of my list. When I open my page, first button will be disabled until I get to another page or both will be disabled if my list is short or emty. I need to click those buttons if they are not disabled. Only diference between disabled and not is class attribute:
class="disabled btn btn-plain btn-default-hover"
class="btn btn-plain btn-default-hover"
So how can I check if buttons class atribute contains 'disabled'? scenario goes like this - if second button active click 'next' and then if first button active click 'back'
//el is the web element
if(el.getAttribute("class").split(" ").contains("disabled"))
{
//your code
}
Let's say you have two buttons with the ids "button1" and "button2"..
You can check with something like this:
if(document.getElementById("button1").className.indexOf("disabled") > 1) {
// what to do if disabled...
} else {
// what to do if active...
}
And of course, you can do the same for the other button...
by using jquery write the following code
var ClassAtttr = $("#YourElementId").attr("class");
if (ClassAtttr.indexOf('Your Expression'))
{
//do somthing
}
Why not try the '.Enabled'
WebElement backButton = driver.FindElement('Your Locator');
if(backButton.Enabled)
{
'Your action to be performed
}

LightSwitch hide delete button until record is selected

I'm using Visual Studio 2013 and LightSwitch. I figured out how to create a delete record button and it works great. The only problem I have is on my main page where all records are shown.
When the page loads, The View & Edit buttons only appear once I select a record. the Add and Delete are visible all the time. The problem is, while the delete function works, it only works when a record is selected. So, if the page loads and you click delete it errors out. I would like to hide the delete button until a record is clicked on. By default the Edit and View buttons that Lightswitch creates do this, however since you have to write your own Delete function I have not figured out how to do this.
Here is an example of the C# i'm working with which works fine provided a record is selected..
myapp.BrowseGiftRegistries.DeleteRegistry_execute = function (screen) {
screen.GiftRegistries.deleteSelected();
return myapp.commitChanges().then(null, function fail(e) {
myapp.cancelChanges();
throw e;
});
};
In the delete button's _canExecute() method just put the following code:
myapp.MyScreen.DeleteButton_canExecute = function (screen) {
return screen.GiftRegistries.selectedItem != null;
};
You can also control whether the button is visible when disabled by checking or unchecking the "Hidden if disabled" checkbox in the properties for the selected button.
You should do it on client side using javascript(it seems that you have provided javascript code).
For example, if you have delete button named 'DeleteRegistry':
Add code below to your BrowseGiftRegistries.lsml.js
myapp.BrowseGiftRegistries.created = function (screen) {
screen.findContentItem('DeleteRegistry').isEnabled = false;
//screen.findContentItem('DeleteRegistry').isVisible = false;
};
// Function created by clicking List( Gift Registries)->properties window->
// ->Actions->Item tap->None->edit execute code
myapp.BrowseGiftRegistries.GiftRegistry_ItemTap_execute = function (screen) {
screen.findContentItem('DeleteRegistry').isEnabled = true;
//screen.findContentItem('DeleteRegistry').isVisible = true;
};
// Modification of your function
myapp.BrowseGiftRegistries.DeleteRegistry_execute = function (screen) {
screen.GiftRegistries.deleteSelected();
screen.findContentItem('DeleteRegistry').isEnabled = false;
//screen.findContentItem('DeleteRegistry').isVisible = false;
return myapp.commitChanges().then(null, function fail(e) {
myapp.cancelChanges();
throw e;
});
};
You can replace line containing isEnabled field with commented line containing isVisible field to reach result you need.

Show/hide buttons based on a dropdown list

Currently I have an aspx page that contains a dropdown list and four buttons.
Based on the selection made in the dropdown list then a combination of the buttons are displayed.
I currently have this implemented so that when the user makes a selection I am using AutoPostBack and the selectedChanged server side event to determine which buttons to display and then set the Visible property of these buttons in this method.
Due to the fact that this posts back I don't think its a nice solution as the whole page is posting back. I would prefer to do this using JSON.
I made the following attempt but it doesn't seem to work:
$(document).ready(function () {
jQuery("#<%= MyDropdownList.ClientID %>").change(function () {
updateAvailableButtons(jQuery(this).val());
});
});
function updateAvailableButtons(selectedItemId) {
jQuery("h2").html("selectedItemId:" + selectedItemId);
jQuery.getJSON("MyPage.aspx/GetAvailableButtons?" + Id, function (data, textStatus) { debugger; });
}
Server side:
protected void GetAvailableButtons(int selectedItemId)
{
//based on the id here then then I show hide certain buttons.
button1.Visible = true;
button2.Visible = false;
button3.Visible = false;
button4.Visible = false;
}
I've never worked with JSON before so apologies if this is way off.
Similar task can be done using JavaScript. The problem is that you'll need to use a html control instead of an asp.net button control so that you can manipulate form the client side.

DynamicRadio Button

I'm using C# in VS2005. For my application I need to create four radio buttons. My form looks like:
A(Radio Button)
B(Radio Button)
C(Radio Button)
D(Radio Button)
Submit (Button)
When a user clicks the submit button I need to know which radio button is checked. How can I determine this?
I would add all the radio buttons to a List<RadioButton> which would help you innumerate through them when the submit is checked to figure out which one is checked.
You can use the Checked Property of a RadioButton to see if it is checked:
bool isChecked = radA.Checked;
I often use the following helper function to get exactly the RadioButton which is checked:
public RadioButton GetCheckedRadioButton(Control container)
{
if (container == null) {
return null;
}
else if (container is RadioButton) {
return GetCheckedRadioButton(container.Parent);
}
else {
foreach (Control childControl in container.Controls) {
if (childControl is RadioButton) {
RadioButton radioBtn = (RadioButton) childControl;
if (radioBtn.Checked) {
return radioBtn;
}
}
}
return null;
}
}
Then, you can simply call this function using one of your controls or it's container, and do a switch statement, as such:
switch(GetCheckedRadioButton(radA)) {
case radA:
// radA is checked
break;
case radB:
// radB is checked
break;
}
Personally, I find it less verbose than the usual:
if(radA.Checked) {
//radA is checked
}
else if(radB.Checked) {
//radB is checked
}
If you know for sure that you will only need 4 RadioButtons, then I'd just write code similar to this.
if (Radio1.Checked) ...
if (Radio2.Checked) ...
if (Radio3.Checked) ...
...
If you know that only one of them can be checked at the same time (using a group box or some other mechanism) then you can optimise this by using "else if" instead.
However if there is a possibility that you will need more, this can get wasteful, so I agree with other answers, you should put them in a collection of your choice and use it to loop through them, to see which ones are checked.

Categories