c# showing error while browsing a webpage - c#-4.0

This is a test program. I just created a simple Windows application form with one button, and if the button is clicked, I need it to do something. So, I wrote my code as:
IWebDriver driver;
public Form1()
{
InitializeComponent();
}
public void SetupTest()
{
driver = new FirefoxDriver();
}
private void button1_Click(object sender, EventArgs e)
{
driver.Navigate().GoToUrl("webaddress");
driver.FindElement(By.TagName("Atlast")).Click();
Thread.Sleep(5000);
}
I have included all of the dependencies (both code and references), but I am getting the following error when I click the button:
Object reference not set to an instance of an object. in driver.navigate part of my code..
What mistake did I make here? Can anyone please help me out with this?

private void button1_Click(object sender, EventArgs e)
{
SetupTest()
driver.Navigate().GoToUrl("webaddress");
driver.FindElement(By.TagName("Atlast")).Click();
Thread.Sleep(5000);
}
You need to be calling SetupTest in your button click code. Why? This is where you are creating your new instance of the IWebDriver, therefore it needs to be called otherwise any references to driver will simply refer to null (by default).

Related

VSTO: which event cannot hooked to the object directly?

For some event, the subscription by hooking the event directly object is failing but working when you use a local variable.
I haven't figured out why yet, who does?
I came across this mystery as I start working with CommandBarsEvents.OnUpdate, in order to detect interaction with a shape, what can comes with certain problems as described here.
There are other events with that problem?
to keep clear here comes an (incomplete) overview of events which are affected and which not
events what DO need a local variable
this.Application.CommandBars.OnUpdate
.
Their code has to be like this:
using Office = Microsoft.Office.Core;
namespace Project
{
public partial class AddIn
{
private Office.CommandBars commandBars; //declaration of local variable
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
commandBars = this.Application.CommandBars; //initialization of local variable
commandBars.OnUpdate += new Office._CommandBarsEvents_OnUpdateEventHandler(commandBars_OnUpdate); //"indirect" subscription to event`
}
}
}
events what NOT need a local variable
((InteropExcel.AppEvents_Event)Application).NewWorkbook
this.Application.WorkbookNewSheet
this.Application.WorkbookOpen
.
Their code can be like this:
using Office = Microsoft.Office.Core;
namespace Project
{
public partial class AddIn
{
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
((InteropExcel.AppEvents_Event)Application).NewWorkbook += new InteropExcel.AppEvents_NewWorkbookEventHandler(Excel_NewWorkbook_EventHandler); //"direct" subscription to event`
}
}
}

Action listeners not firing

I've been developing with codenameone for over a year, and I never ran into this problem before, I feel like I'm losing my mind. I just redesigned one part of an app I'm working on, and now the ActionListeners aren't firing. I'm attaching them to a Button and a SpanButton in the code:
ActionListener goToDoc = new ActionListener() {
String mDocId = docId;
#Override
public void actionPerformed(ActionEvent evt) {
mStateMachine.currentExpertId = mDocId;
mStateMachine.showForm("DoctorDetails", null);
}
};
name.addActionListener(goToDoc);
Util.downloadImageToStorage(mStateMachine.URL_PREFIX+"images/doctors/"+(String)value.get("doc_pic"),
"doc_post_pic_"+(String)value.get("doc_id")+".png", new Callback<Image>() {
#Override
public void onSucess(Image img) {
pic.setIcon(img.scaledWidth(mStateMachine.getProportionalWidth(.23)));
StateMachine.applyGreenBorder(pic);
pic.addActionListener(goToDoc);
pic.getParent().revalidate();
}
#Override
public void onError(Object sender, Throwable err, int errorCode, String errorMessage) {
System.out.println("Unable to download expert profile picture");
}
});
When I debug the code, the components do show that the ActionListener is attached, but the actionPerformed method is never reached, no matter how many times I click on the buttons. I experience this problem both on the simulator and on an Android device. I have yet to test on an iPhone.
Did you set a parent to be a lead component or focusable?
The reason the click event wasn't firing was because the Components weren't enabled, possibly a bug in the BUI Builder. After checking off the 'Enabled' and 'Focusable' checkboxes in the GUI Builder, and seeing they were unchecked every time I went back to that form, I just used component.setFocusable(true) and component.setEnabled(true) in the code, and it worked fine after that.

How can I override or act on KeyDown: Space or Enter for ListView in UWP?

I've attached the KeyDown event to a ListView in my Win 10 UWP app. I want to make VirtualKey.Enter have a special effect, but the event is not firing for this particular key. Neither does it for Space, Arrow up or down. This I guess because the listview already has defined a special behaviour for those keys.
I'd like to override some of those keys though, or at least trigger additional actions. Even attaching events to those key with modifiers (e.g. Shift+ArrowDown) would not work because the events still are not firing.
I read that for WPF that there is a PreviewKeyDown-event which one can attach to. I can't find that event for UWP though. Are there any other options?
Stephanie's answer is a good one and it works in the general case. However, as Nilzor observed it will not work in the case of a ListView for the Enter key. For some reason the ListView handles the KeyDown event in case Enter is pressed.
A better way to handle key events when dealing with a ListView, as the question asks, is this.
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
(sender as ListView).AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(ListView_KeyDown), true);
}
private void ListView_KeyDown(object sender, KeyRoutedEventArgs args)
{
if (args.Key == Windows.System.VirtualKey.Enter)
{
}
}
Notice the last argument in the AddHandler function. This specifies whether we want to handle events already handled by a previous element in the visual tree.
Of course don't forget to unsubscribe from the event when appropriate
Here is one way to do it : subscribe to the global Window.Current.CoreWindow.KeyDown event.
Then save the focus state of your listview and react accordingly.
Here is the code :
public sealed partial class MainPage : Page
{
bool hasFocus = false;
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if(hasFocus)
{
Debug.Write("Key down on list");
}
}
private void myList_GotFocus(object sender, RoutedEventArgs e)
{
hasFocus = true;
}
private void myList_LostFocus(object sender, RoutedEventArgs e)
{
hasFocus = false;
}
You will also need to subscribe to the focus events in xaml, for your ListView :
<ListView .... GotFocus="myList_GotFocus" LostFocus="myList_LostFocus"/>
Corcus's solution doesn't work for me. What is working is handling PreviewKeyDown directly from XAML. Works well for SPACE or ENTER key:
XAML:
<ListView PreviewKeyDown="BookmarksListView_PreviewKeyDown">
Code behind:
private void BookmarksListView_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
// DO YOUR STUFF...
e.Handled = true;
}
}
You can use AddHandler method.
private void KeyEnterEventHandler(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalKey == Windows.System.VirtualKey.Enter)
{
PlayFromListView();
}
}
private void LoadListView()
{
foreach (var music in playListStorageFile.PlayList)
{
ListViewItem item = new ListViewItem();
item.AddHandler(FrameworkElement.KeyDownEvent, new KeyEventHandler(KeyEnterEventHandler), true);
TextBlock mytext = new TextBlock();
mytext.Text = music.Nro.ToString() + " - " + music.Name;
mytext.Tag = music.Nro;
item.Content = mytext;
lvMusics.Items.Add(item);
}
}
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.addhandler?view=winrt-18362

ServiceStack Profile Steps not rendering

I have a ServiceStack Service with a service call like so:
public class MyService : Service
{
public object Get(MyServiceRequest request)
{
using (Profiler.Current.Step("Getting Data"))
{
// code that gets data
using (Profiler.Current.Step("Doing work with data"))
{
// code that does work
}
}
return response;
}
}
and a global.asax.cs like so:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
new AppHost().Init();
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (Request.IsLocal)
Profiler.Start();
}
protected void Application_EndRequest(object sender, EventArgs e)
{
Profiler.Stop();
}
}
My problem is that when I test the service call through the browser I only see profile information for the overall request. "show time with children" and "show trivial" don't provider any more granular information. I've also placed breakpoints within each using statement to get a look at Profiler.Current and noticed in each case its Children property is null. Am I doing it wrong? Are they any other things I can do to troubleshoot this?
For some reason setting the level argument to Profile.Info in the Step method resolves my issue. That's weird because Profile.Info is the default if no argument is provided. Oh well. Moving on.

Gecko WebBrowser.ReadyState

I'm creating an application that contains "geckoWebBrowser" in c #. But I have to wait the complete loading a web page, and then continue to execute other instructions. there is something similar to WebBrowser.ReadyState? thank you very much
Hi GeckoWebBrowser has a DocumentCompleted event you could use it.
Edit:
for example you have a button to show url
private void ShowBtnClick(object sender, EventArgs e)
{
geckoWebBrowser1.Size = new Size(int.Parse(ViewportWidth.Text), int.Parse(ViewportHeight.Text));
geckoWebBrowser1.DocumentCompleted += LoadingFinished;
geckoWebBrowser1.Navigate(PageUrl.Text);
}
private void LoadingFinished(object sender, EventArgs args)
{
GeckoElement body = geckoWebBrowser1.Document.GetElementsByTagName("body")[0];
body.SetAttribute("style", "margin-top:-700px");
}
Try this :
private void WaitBrowser(Gecko.GeckoWebBrowser wb)
{
while (wb.IsBusy)
{
Application.DoEvents();
}
}

Resources