Implement browser in windows form using awesomium - c#

I"m using Awesomium to try and implement webpages inside my windows form application.
I've used the the awesomium .NET samples but I just don't get the my tab with my homeurl.
When I run my project the status bar is floating inside my form and nothing else happens.
Anyone know where I can get a tut on how to do this or know what could be the problem?
public Laboratory() {
WebCoreConfig webConfig = new WebCoreConfig() {
SaveCacheAndCookies = true,
HomeURL = "http://www.google.com",
LogLevel = LogLevel.Verbose
};
// Using our executable as a child rendering process, is not
// available when debugging in VS.
if (!Process.GetCurrentProcess().ProcessName.EndsWith("vshost")) {
// We demonstrate using our own executable as child rendering process.
// Also see the entry point (Main function) in Program.cs.
webConfig.ChildProcessPath = WebCoreConfig.CHILD_PROCESS_SELF;
}
WebCore.Initialize(webConfig);
InitializeComponent();
}
#region methodes
#region OpenTab
internal WebDocument OpenTab(string url = null, string title = null) {
WebDocument doc = String.IsNullOrEmpty(url) ? new WebDocument() :
String.IsNullOrEmpty(title) ? new WebDocument(url) : new WebDocument(url, title);
doc.Show(dockPanel);
return doc;
}
#endregion
protected override void OnLoad(EventArgs e) {
base.OnLoad(e);
this.OpenTab();
}

I've redone the left panel completely and used the other example that was with the download, that works like a charm. It's very basic but that'll do for now.

I too had the same problem but i devised a work around for the problem . I implemented the main browser engine that needs to be rendered on every tab (WebDocument Page as per the example) as a user control . Then i used a DockPannel in the mainForm .
So i create an instance of the user control and then add this to an instance of the DockPannel and thus it creates a tab with the required structure .
In case you have still not found a solution or have problems , please leave down a comment and i'll put in some code to help you .

Related

Can't find WinWindow by property "Name"

I've got the following problem:
I try to create a Coded UI Test in Visual Studio 2015 Enterprise Edition without using the Generator. I want to achieve the very simple thing of pressing a button and looking at the results.
My Forms are not directly WinForms, but it's the basis of them.
Here's what I'm trying to do:
public void CodedUITestTestMethod()
{
//1. Step: Log into the program
LogIntoProgram();
}
private void LogIntoProgram()
{
// Find the Login-Window
WinWindow loginWindow = GetWindowByTitle("Program - Login");
[...]
}
private WinWindow GetWindowByName(string title, UITestControl parent = null)
{
// If the Window has a parent, assign it to the window
WinWindow result = parent == null ? new WinWindow() : new WinWindow(parent);
result.SearchProperties.Add(WinWindow.PropertyNames.Name, title);
result.Find();
return result;
}
The [...] section is where I want to press the button. The problem occurs before that though, as I can't even find the window I'm looking for. It repeatedly throws the UITestControlNotFound exception, no matter if I use the Title or the Classname of the Form.
I got the feeling that I am missing a very major point, but I can't figure out which one.
Thanks for the help in advance,
SchwarzSkills :)
Start your application and pass it into the WinWindow
var app = ApplicationUnderTest.Launch("C:\\Windows\\System32\\myProgram.exe"
, "%windir%\\System32\\myProgram.exe");
WinWindow loginWindow = GetWindowByTitle("Program - Login", app);

CefSharp WPF web browser is not displayed Or rendered

Am new to CefSharp
I have created a class library project and referenced the CefSharp library to render the Web browser, However I am facing some issues showing the web Browser. Please find the exact code
WebBrowser_test1:
public partial class ChildWidget : Window
{
public CefSharp.Wpf.ChromiumWebBrowser webView;
public Widget()
{
InitializeComponent();
CefSharp.CefSettings settings = new CefSharp.CefSettings();
settings.PackLoadingDisabled = true;
if (CefSharp.Cef.Initialize(settings))
{
webView = new CefSharp.Wpf.ChromiumWebBrowser();
main_grid.Children.Add(webView);
webView.Address = "http://www.google.co.uk";
}
}
}
and I am referencing this library (dll) in another project
public MainWindow()
{
InitializeComponent();
Button newbutton = new Button();
newbutton.Width = 50;
main_grid.Children.Add(newbutton);
newbutton.Click += ButtonClick;
}
private void ButtonClick(object sender, RoutedEventArgs e)
{
try
{
Webbrowser_test1.ChildWidget childWidget = new Widget();
childWidget.Show();
}
catch (Exception)
{
throw;
}
}
Now on the Button click I will open the (WebBrowser_test1) child widget in which I will show the web browser .. when the window opens it is showing blank.
Please let me know if I missing anything
Subscribe to IsBrowserInitializedChanged after creating a ChromiumWebBrowser. Then once the browser is initialized you can call Load and your control will be displayed.
...
_browser = new ChromiumWebBrowser();
mainGrid.Children.Add(_browser);
_browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
...
void OnIsBrowserInitializedChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (_browser.IsBrowserInitialized)
{
_browser.Load("https://www.google.com/");
}
}
I can think of the first three potential issues. But it's hard to tell what the real issue is from your code alone as it strays off a bit from the official examples
Move Cef.Initialize() to your MainWindow constructor. It should only be called once to launch the CefSharp.BrowserSubprocess.exe renderer process.
See my answer to CefSharp 3 always failing Cef.Initialize() for a few things to check regarding binaries and their placement. Really, the recommended approach is to start having the WPF example in the CefSharp.MinimalExample repo running first and then adjust to your use case from there.
I'm not sure a ChromiumWebBrowser() without explicitly setting a width and height works. A 0x0 window might not receive any rendered content. I haven't tried with recent code.
Have you tried replacing
webView.Address = "http://www.google.co.uk";
with
webView.Load("http://www.google.co.uk");
Like jornh mentions, you may have to explicitly set the height and width of the ChromiumWebBrowser. If you don't know the exact size, setting HorizontalAlignment and VerticalAlignment to Stretch (to fill the parent container) will probably also work.
Have you checked if the Cef.Initialize() actually returns true? You could be missing some files, and CefSharp doesn't always give clear error messages when this is the case.

how to login a javascript based site with a windows form?

I've been trying to login to a site using a WebBrowser control automatically by using the GetElementByTag / GetElemenByName methods but with not much success. (WFA - C#)
I believe the main reason is the fact that the site is in JavaScript.
I've done some research and found two methods:
by mimicking the site login form I could use something called POST. (or something of that sort)
inject a javascript input to all the input fields in the site
I have no idea how to approach this problem, and due to my total lack of experience with javaScript or any web based programing, I could realy use any advice or solution for the matter at hand.
/** EDIT 1
thank you for your quick response.
ive tried to use your code and still facing the same exception throw..
here is the code :
(I trimmed it a bit)
public WorkerClass(string url)
{
webBrowser1 = new WebBrowser();
webBrowser1.Navigate(url);
webBrowser1.Document.GetElementById("c_Username").InnerText = "?????";
webBrowser1.Document.GetElementById("c_Password").InnerText = "?????";
}
and I get a "System.NullReferenceExeption" on the username line above.
the site im trying to access is - "http://www.gool.co.il"...maybe my approach is wrong?!
You can use web browser control ,just find element id on your target page and fill them.
I write a simple one for you:
public Form1()
{
InitializeComponent();
//navigate to you destination
webBrowser1.Navigate("https://www.certiport.com/portal/SSL/Login.aspx");
}
bool is_sec_page = false;
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!is_sec_page)
{
//get page element with id
webBrowser1.Document.GetElementById("c_Username").InnerText = "username";
webBrowser1.Document.GetElementById("c_Password").InnerText = "pass";
//login in to account(fire a login button promagatelly)
webBrowser1.Document.GetElementById("c_LoginBtn_c_CommandBtn").InvokeMember("click");
is_sec_page = true;
}
//secound page(if correctly aotanticate
else
{
//intract with sec page elements with theire ids
}
}

WindowsFormsApplicationBase SplashScreen makes login form ignore keypresses until I click on it - how to debug?

My WinForms app has a simple modal login form, invoked at startup via ShowDialog(). When I run from inside Visual Studio, everything works fine. I can just type in my User ID, hit the Enter key, and get logged in.
But when I run a release build directly, everything looks normal (the login form is active, there's a blinking cursor in the User ID MaskedEditBox), but all keypresses are ignored until I click somewhere on the login form. Very annoying if you are used to doing everything from the keyboard.
I've tried to trace through the event handlers, and to set the focus directly with code, to no avail.
Any suggestions how to debug this (outside of Visual Studio), or failing that - a possible workaround?
Edit
Here's the calling code, in my Main Form:
private void OfeMainForm_Shown(object sender, EventArgs e)
{
OperatorLogon();
}
private void OperatorLogon()
{
// Modal dialogs should be in a "using" block for proper disposal
using (var logonForm = new C21CfrLogOnForm())
{
var dr = logonForm.ShowDialog(this);
if (dr == DialogResult.OK)
SaveOperatorId(logonForm.OperatorId);
else
Application.Exit();
}
}
Edit 2
Didn't think this was relevant, but I'm using Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase for it's splash screen and SingleInstanceController support.
I just commented out the splash screen code, and the problem has disappeared. So that's opened up a whole new line of inquiry...
Edit 3
Changed title to reflect better understanding of the problem
UI focus/redraw/etc. issues usually are rather straightforward to debug by using remote-debugging. I.e. use a second PC (virtual is just enough) where your application runs.
See this MSDN article for details.
Run this in your form code behind. It will tell you which control has focus by giving you the type and name of the control. Run it in form_shown because its the last event in the form load process.
private void Form1_Shown(object sender, EventArgs e)
{
Control control = FindFocusedControl(this);
MessageBox.Show("The focused control " + control.Name + " is of type " + control.GetType());
}
public static Control FindFocusedControl(Control control)
{
var container = control as ContainerControl;
while (container != null)
{
control = container.ActiveControl;
container = control as ContainerControl;
}
return control;
}
If the answer isn't obvious after that, tell us what you get.
I've found a hack...er...I mean...workaround, that fixes the problem. The solution was buried in one of the comments of this answer (thanks, P. Brian Mackey, for providing the link to the related question!)
The workaround is to minimize the main window while the splash screen is displayed, then set it's WindowState back to Normal before showing the login form.
In the code below, see the lines commented with "HACK".
public class SingleInstanceController : WindowsFormsApplicationBase
{
public SingleInstanceController()
{
this.IsSingleInstance = true;
}
/// <summary>
/// When overridden in a derived class, allows a designer to emit code that
/// initializes the splash screen.
/// </summary>
protected override void OnCreateSplashScreen()
{
this.SplashScreen = new SplashScreen();
}
/// <summary>
/// When overridden in a derived class, allows a designer to emit code that configures
/// the splash screen and main form.
/// </summary>
protected override void OnCreateMainForm()
{
// SplashScreen will close after MainForm_Load completed
this.MainForm = new OfeMainForm();
// HACK - gets around problem with logon form not having focus on startup
// See also OfeMainForm_Shown in OfeMainForm.cs
this.MainForm.WindowState = FormWindowState.Minimized;
}
}
public partial class OfeMainForm : Form
{
// ...
private void OfeMainForm_Shown(object sender, EventArgs e)
{
// HACK - gets around problem with logon form not having focus on startup
// See also OnCreateMainForm in Program.cs
this.WindowState = FormWindowState.Normal;
OperatorLogon();
}
// ...
}
This is working for now, but I'm wondering if I should explicitly open the Logon form from the SingleInstanceController, rather than from my main form.

Managed BHOs not instantiated using Protected Mode

I am writing a BHO for IE using C#. The code I'm concerned with is this:
public class BHO : IObjectWithSite, IOleCommandTarget
{
...
public BHO()
{
MessageBox.Show("Constructor called");
}
public int SetSite(object site)
{
MessageBox.Show("SetSite called!");
if( site != null )
{
_webBrowser = (WebBrowser) site;
_webBrowser.NavigateComplete2 += OnNavigateComplete2;
}
else
{
_webBrowser.NavigateComplete2 -= OnNavigateComplete2;
_webBrowser = null;
}
return 0;
}
private void OnNavigateComplete2(object pDisp, ref object URL)
{
MessageBox.Show("OnNavigateComplete2 called");
}
When IE is run with Protected Mode off, everything works fine. However, if Protected Mode is turned on, NavigateCompleted2() is called, but SetSite() and the constructor are never called (!?!). However, if I create a menu item which calls a method in the BHO class, or open a new tab, everything is correctly called.
Does anyone know why it doesn't work when I open a new IE window?
The full source listing can be found here.
Someone on MSDN answered my question: the constructor and method were still being called, but for some reason the MessageBoxes don't show when I open a new window in Protected Mode until the page is loaded. Variables weren't being set due to a different problem - the constructor was instantiating an object which was silently failing.
I now need help with a different (very much related) problem.

Categories