Clear() not actually clearing the input element - c#

I ran into a scenario today where clear() wasn't actually clearing the input element and I'm hoping someone can shed some light on this as I've never ran into this before.
It looks like the method executes successfully without throwing an exception but it just doesn't clear.
Clear Method - this is in a separate framework that is being used by the calling project
public void Clear(By element)
{
try
{
driver.FindElement(element).Clear();
}
catch (Exception ex)
{
DTAF.Helpers.Screenshot.TakeScreenshot(driver);
throw ex;
}
}
Element HTML - you can see it has value that I was trying to clear
<input aria-invalid="false" id="txtCustomerEditFirstName" type="text" data-testid="firstname" class="MuiFilledInput-input MuiInputBase-input css-1ncak0i" value="test123">
I was able to execute just raw selenium in the Immediate window and it looked like it cleared the input but when I clicked the actual element in the UI the value just came back so it didn't actually clear it.
I did find a workaround where I just get the input value attribute and loop through that pressing the Backspace key until theres nothing left. It works just fine but I'd prefer to use the selenium clear method if possible.
EDIT
Element Locator
public static By FirstNameTextbox = By.Id("txtCustomerEditFirstName");
Calling method
public EditUnregisteredCustomerProfileModal ClearAndEnterFirstName(string firstName)
{
try
{
ElementActions.Clear(EditUnregisteredCustomerProfileModalElements.FirstNameTextbox);
ElementActions.Type(EditUnregisteredCustomerProfileModalElements.FirstNameTextbox, firstName);
}
catch (Exception e)
{
DTAFLogger.GetLogger().Error($"Failed to clear and enter unregistered customer first name '{firstName}'. {e.Message}");
Assert.Fail($"Failed to clear and enter unregistered customer first name '{firstName}'. {e.Message}");
}
return this;
}

Related

Cannot modify the document for either a read-only external command is being executed, or changes to the document are temporarily disabled

I want to set parameter values of the elements when the DocumentChanged event is triggered. I know that the event is read-only.
Is there any another way (except IUpdater) to do it? Except IUpdater because I want every changes in the document.
Here is my code
public Result OnStartup(UIControlledApplication application)
{
try
{
// Event handler for document changing
application.ControlledApplication.DocumentChanged += new EventHandler<DocumentChangedEventArgs>(Application_DocumentChanged);
}
catch (Exception)
{
return Result.Failed;
}
return Result.Succeeded;
}
private void Application_DocumentChanged(object sender, DocumentChangedEventArgs e)
{
Document doc = e.GetDocument();
// Record added elements
if (e.GetAddedElementIds().Count() > 0)
{
using (Transaction transaction = new Transaction(doc))
{
try
{
transaction.Start("Set TCP Parameters");
foreach(ElementId el_id in e.GetAddedElementIds())
{
doc.GetElement(element_id).get_Parameter(new Guid("6558f207-e777-0758-2023-2f34e722cb01")).Set(200)
}
}
catch (Exception ex)
{
TaskDialog.Show("Error: Transaction", ex.Message);
}
transaction.Commit();
}
}
TaskDialog.Show("Document Changed", e.Operation.ToString());
}
I will really appreciate any ideas
You ask: Is there any another way?
Yes. You can subscribe to a one-off shot of the Idling event, cf. Reacting to Changes and Setting Parameters using DMU or DocumentChanged
This has been discussed repeatedly by The Building Coder and in the Revit API discussion forum. Some related articles are listed in the topic group on Idling and External Events for Modeless Access and Driving Revit from Outside.

Revit API AddInCommandBinding for ID_PROCESS_DROP

I'm trying to bind the drag/drop of a family into the project and disable it.
My code is based on the Revit 2014 SDK Sample DisableCommand
My code has the .CanHaveBinding test and I have a dialog that displays success or failure. When I run the command it shows success, but I'm still able to drag drop families. Any ideas?
RevitCommandId commandId2 = RevitCommandId.LookupCommandId("ID_PROCESS_DROP");
if (!commandId2.CanHaveBinding)
{
TaskDialog.Show("Error", "Drag/Drop cannot be overridden.");
}
else
{
TaskDialog.Show("Success", "Drag/Drop can be overridden.");
}
try
{
AddInCommandBinding dropBinding = uiapp.CreateAddInCommandBinding(commandId2);
dropBinding.Executed += new EventHandler<Autodesk.Revit.UI.Events.ExecutedEventArgs>(dragDropDisable);
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}",ex.ToString());
}
private void dragDropDisable( object sender, Autodesk.Revit.UI.Events.ExecutedEventArgs args)
{
TaskDialog.Show("Disabled", "Never Drag/Drop families into your project!");
}
I think your method (and class) may need to be static - I've had weird things happen with instance methods in the past. Also, I'm not sure what your implementation of the method does exactly but it may need to return a CommandData.Result in order for the command to fully complete
Try this
dropBinding.Executed += dragDropDisable;

Getting error "Internet Explorer cannot display the webpage" in asp.net

I have a code block that leads to a "Internet Explorer cannot display the webpage" error. When I click the submit button, with NONE of the radio buttons checked, the web page status bar displays "waiting for response from host" and then display the "Internet Explorer cannot display the webpage". When I walk through the code in visual studio, the code executes fine, and none of the catch blocks are executed.
How can I trap the error and determine why the error page is being displayed?
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (rbtnSearchBy1.Checked)
{
Server.Transfer("ViewEmpHistory.aspx");
}
if (rbtnSearchBy2.Checked)
{
Server.Transfer("SearchEmp.aspx");
}
if (rbtnSearchBy3.Checked)
{
Server.Transfer("ViewEmpCard.aspx");
}
}
catch (ThreadAbortException)
{
throw;
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
Whatever .cs page your "btnSubmit_Click" is on, put a breakpoint on that page_load event.
Also, put a breakpoint on the page_load event of "ViewEmpHistory.aspx", "SearchEmp.aspx" & "ViewEmpCard.aspx". (so now you have four breakpoints).
Step through the project again and make sure all parameter values are being passed correctly, also make sure that you have correct logic (if applicable) for If (!PostbacK) conditions etc.
HTH
if you don't select one radiobutton it's normal that you don't enter in your catch , because your application no throw exception.
but you can view your eventlog
Enter in your cmd : eventvwr to access your event log
To debug these kind of issues, I often find it easier to use Tracing.
You can turn on tracing at the application level, or at the page level.
Your method call will then become:
protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
if (rbtnSearchBy1.Checked)
{
Server.Transfer("ViewEmpHistory.aspx");
}
if (rbtnSearchBy2.Checked)
{
Server.Transfer("SearchEmp.aspx");
}
if (rbtnSearchBy3.Checked)
{
Server.Transfer("ViewEmpCard.aspx");
}
}
catch(Exception ex)
{
Trace.Warn("Exception Caught", "Exception: btnSubmit_Click", ex);
}
}
You can look at the trace log by then navigating to the Trace Viewer.
What you've done is not exactly well structured. It's cleaner if the blocks are exclusive - which is why I've added the else statements to the code below. I've also indicated where you would want to handle the state where no button is checked in comments.
But you're right, there isn't any exception being thrown. Your code didn't throw one, and when you end processing a request without returning any type of response it doesn't cause an exception.
if (rbtnSearchBy1.Checked)
{
Server.Transfer("ViewEmpHistory.aspx");
}
else if (rbtnSearchBy2.Checked)
{
Server.Transfer("SearchEmp.aspx");
}
else if (rbtnSearchBy3.Checked)
{
Server.Transfer("ViewEmpCard.aspx");
}
else
{
// Here's where the logic will flow to if no radio button is clicked.
// We could
// * Server.Transfer to a default location
// * Throw an exception
// * Do nothing, which returns no response, and causes
// IE to complain that it could not display the webpage.
}

Calling upon a method to change textbox value

I have a method called BuyShares() that is supposed to take a value in a textbox and add another user input value to it. I would like to use a messagebox that sets off the method by the user clicking okay. The only problem is that I can't seem to call upon the method.
This is the method.
public void BuyShares(int anAmount)
{
int newShares;
newShares = GetInvestmentShare() - anAmount;
SetInvestmentShare(newShares);
}
And this is the messagebox I have set up
private void button1_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("Your transaction is complete", "Success", MessageBoxButtons.OK);
if(result==DialogResult.OK)
{
txtStockSharesTab3.Text=??????
}
This is a windows form application and the program has several different classes
Without giving away the answer because you probably want to learn something from this...
I'm guessing you want to get the amount of shares to buy from one textbox, then call BuyShares and finally update txtStockSharesTab3 to this value.
Your BuyShares method returns void meaning it won't return a value. Somewhere in that method is where you're going to update your txtStockSharesTab3 textbox. Is that exact method signature for BuyShares required?

In C# what is the best way to close a newly opened form if there is a failure in CTOR/Load event?

What is the best method [pattern] to close a newly opened Windows form in C#/.NET if there is a trappable error in the CTOR/_Load event ?
i.e.,
bool loadError;
MyForm_Load(...) {
try {
} catch (SomeException ex) {
// Alert the user
MessageBox.Show("There was a critical error. The form will close. Please try again.");
// There was some sort of error, and I just want the form to close now.
loadError = true;
return;
}
}
Now I want to act on loadError.
I've tired using the Activate event, but that yielded no results. Any suggestions on what the best way to go about doing this is ?
Update: Perhaps I should have been a little more explicit. Doing a "this.Close();" during the forms Load event will cause an exception to be thrown as a form can't be closed and the exception will be "Cannot call Close() while doing CreateHandle()".
As I mentioned in the comments, I tried with a sample example and called this.Closed() inside of the catch block. It worked just fine. The application showed the message box and didn't show me the form. I am using .NET3.5 SP1, though.
Suppose this error happens in earlier version of .NET Framework, can you try to see whether any of the followings works for you or not? They, again, seem to work fine on my machine, yet, cannot guarantee whether they will work on yours, though.
Put this.Close() in the finally block to see it works
finally {
if (this.loadError)
this.Close();
}
Defer the Form.Close() after Form.OnLoad event handler completes.
See the sample below: this does not contain any anonymous delegate or lambda expression for older versions of .NET FW. Please forgive me for partial class :)
using System;
using System.Windows.Forms;
namespace ClosingFormWithException
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public delegate void InvokeDelegate();
private void Form1_Load(object sender, EventArgs e)
{
try
{
MyTroublesomeClass myClass = new MyTroublesomeClass();
}
catch (ApplicationException ex)
{
MessageBox.Show("There was a critical error. The form will close. Please try again.");
this.BeginInvoke(new InvokeDelegate(CloseTheForm));
}
}
private void CloseTheForm()
{
this.Close();
}
}
class MyTroublesomeClass
{
public MyTroublesomeClass()
{
throw new ApplicationException();
}
}
}
Use the combination of 1 and 2 if 2 does not work:
finally {
if (this.loadError)
this.BeginInvoke(new InvokeDelegate(CloseTheForm));
}
Have you tried calling this.Close() in the FormShown event? That event is fired once when your form is first shown. You could check your error condition there and then call close.
I think you should call form.Dispose() and then set form = null. This should cover for the main usage scenario.

Categories

Resources