Cefsharp - display page loading indication - chromium-embedded

I'm implemeting a win form which contains a cefsharp chromium-embedded browser.
I'm facing the following problem - sometimes it takes time until a page is loaded.
The problem is that user has no idea that something happens until the page is actually loaded.
I have no control on the pages that the browser displays.
I need to display some kind of loading indication. I searched the web and the only thing that I found was to show an animated loading image while the loading takes place and hide it when the page is loaded (using the load state changed event).
It seems to make things even slower.
Is there anything in Cefsharp infrastructure that I can use? or any other idea of solving it?
Thanks!

ChromeView = new CefSharp.Wpf.ChromiumWebBrowser();
//Adding event listener
ChromeView.NavStateChanged += ChromeView_NavStateChanged;
//Event listener
private void ChromeView_NavStateChanged(object sender, CefSharp.NavStateChangedEventArgs e)
{
if(!e.IsLoading)
{
this.Dispatcher.Invoke(()=> { //Invoke UI Thread
controller.setLoaderinBack(); //UI Update
});
}
else
{
this.Dispatcher.Invoke(() => { //Invoke UI Thread
controller.setLoaderinFront(); //UI Update
});
}
}

For Higher Versions of CefSharp (mine version 81):
ChromeView = new CefSharp.Wpf.ChromiumWebBrowser();
//Adding event listener
ChromeView.LoadingStateChanged += ChromeView_NavStateChanged;
//Event listener
private void ChromeView_NavStateChanged(object sender, LoadingStateChangedEventArgs e)
{
if(!e.IsLoading)
{
//Stuff...
}
else
{
//Stuff...
}
}

Related

Accessing document elements when using Windows.Forms.WebBrowser

I'm new to automating webpage access, so forgive what is probably a remedial question. I'm using C#/Windows.Forms in a console app. I need to programmatically enter the value of an input on a webpage that I cannot modify and that is running javascript. I have successfully opened the page (triggering WebBrowser.DocumentCompleted). I set browser emulation mode to IE11 (in registry), so scripts run without errors. When DocumentCompleted() triggers, I am unable to access the document elements without first viewing the document content via MessageBox.Show(), which is clearly not acceptable for my unattended app.
What do I need to do so that my document elements are accessbile in an unattended session (so I can remove MessageBox.Show() from the code below)? Details below. Thank you.
The input HTML is:
<input class="input-class" on-keyup="handleKeyPress($key)" type="password">
My DocumentCompleted event handler is:
private static void LoginPageCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = ((WebBrowser)sender);
var document = wb.Document;
// I'm trying to eliminate these 3 lines
var documentAsIHtmlDocument = (mshtml.IHTMLDocument)document.DomDocument;
var content = documentAsIHtmlDocument.documentElement.innerHTML;
MessageBox.Show(content);
String classname = null;
foreach (HtmlElement input in document.GetElementsByTagName("input"))
{
classname = input.GetAttribute("className");
if (classname == "input-class")
{
input.SetAttribute("value", password);
break;
}
}
}
The problem for me was that the page I'm accessing is being created by javascript. Even though documentComplete event was firing, the page was still not completely rendered. I have successfully processed the first page by waiting for the document elements to be available and if not available, doing Application.DoEvents(); in a loop until they are, so I know now that I'm on the right track.
This SO Question helped me: c# WebBrowser- How can I wait for javascript to finish running that runs when the document has finished loading?
Note that checking for DocumentComplete does not accurately indicate the availability of the document elements on a page generated by javascript. I needed to keep checking for the elements and running Application.DoEvents() until they became available (after the javascript generated them).
If the problem comes from the creation of a STAThread, necessary to instantiate the underlying Activex component of WebBrowser control, this is
a modified version of Hans Passant's code as shown in the SO Question you linked.
Tested in a Console project.
class Program
{
static void Main(string[] args)
{
NavigateURI(new Uri("[SomeUri]", UriKind.Absolute), "SomePassword");
Console.ReadLine();
}
private static string SomePassword = "SomePassword";
private static void NavigateURI(Uri url)
{
Thread thread = new Thread(() => {
WebBrowser browser = new WebBrowser();
browser.DocumentCompleted += browser_DocumentCompleted;
browser.Navigate(url);
Application.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
}
protected static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = ((WebBrowser)sender);
if (browser.Url == e.Url)
{
while (browser.ReadyState != WebBrowserReadyState.Complete)
{ Application.DoEvents(); }
HtmlDocument Doc = browser.Document;
if (Doc != null)
{
foreach (HtmlElement input in Doc.GetElementsByTagName("input"))
{
if (input.GetAttribute("type") == "password")
{
input.InnerText = SomePassword;
//Or
//input.SetAttribute("value", SomePassword);
break;
}
}
}
Application.ExitThread();
}
}
}

WebBrowser issue (add infinite progress in web browser)

It takes a couple of seconds to load an url in webbrowser. Sometimes it takes more time to load. How to add infinite progress like that of connectionRequest in webbrowser?
#Override
protected void postWebView(Form f) {
WebBrowser wb = new WebBrowser();
if (businessWebsiteUrl != null && !businessWebsiteUrl.equals("")) {
wb.setURL("http://" + businessWebsiteUrl);
f.add(BorderLayout.CENTER, wb);
} else {
}
f.revalidate();
}
What I did but doesnot work
protected void beforeWebView(Form f) {
ip = new InfiniteProgress();
f.add(BorderLayout.CENTER, FlowLayout.encloseCenterMiddle(ip));
}
That's inherently problematic since a browser is effectively a peer component and while we can now paint on top of peers in Android this is still not portable.
Even if it would I'm not sure if it would be a good idea.
Overall you have the following options:
Place a progress indicator above/below the browser component
Fetch the data separately as HTML with a progress and set the HTML which should be instant
Use JavaScript and potentially an iframe to indicate progress from within the browser component itself

Awesomium web browser doesn't show the page

I have made a form which takes a few URLs in the constructor and loads them into some awesomuim web controls on the form.
It worked fine in the first computer but when I move the program to another computer (with the code), it doesn't show the page in the control. when I move the mouse over the control, it shows the busy icon.
But, if i put a breakpoint somewhere in the constructor and then click continue in visual studio, it works fine! I tried to put a sleep in the constructor but it didn't do the job.
here's the code :
public BrowsersWindow(List<string> links)
{
InitializeComponent();
// Thread.Sleep(5000);
Links = links;
Browsers = groupBox1.Controls;
}
private void timer1_Tick(object sender, EventArgs e)
{
if (index < Links.Count && index < Browsers.Count)
{
Awesomium.Windows.Forms.WebControl current =
(Awesomium.Windows.Forms.WebControl)Browsers[index];
try
{
current.Source = new Uri(Links[index]);
}
catch { }
index++;
}
}

Integration Testing of WPF GUI: How do I identify that current work was finished

I want to run integration UI tests on my WPF application, and I'm not sure how to detect when the current test has finished so that I can proceed to the next one.
Simplifying, suppose I have a button on my window. When the button is clicked I disable it, I modify the model, and I re-enable the button. Once it detects that the model has changed, WPF changes the view on the screen.
Now I want to run a test that simulates clicking the button again and again. To click the button I’ll use automation, as described in this SO question. But how do I know when the work is finished and the display updated, so as to "click" the button again? Do I hook the botton’s IsEnabledChanged, or is there some global indication that the current cycle of processing has finished?
Edit: What was missing in my description is that I want the user to see the interim results on the screen. For example, if the test has 10 phases I want the user to see something like a Step Counter label with values 1 .. 10 appearing on the screen, and not just the number changing immediately from 1 to 10. See my answer below.
how do I know when the work is finished and the display updated, so as to "click" the button again?
According to your description, you said When the button is clicked I disable it, I modify the model, and I re-enable the button.
Therefore, I can only assume that when the model has changed, the Button will be re-enabled. So you could either attach a handler to the model's NotifyPropertyChanged event, or as you suggested, add a handler for the IsEnabledChanged event.
Here is how I managed to get it working. This might be trivial - I'm a novice with GUI. I'm just posting it here in the hope it'll help other novices like me :)
Anyhow, what I used is a two button solutions: Test and Step. Test starts the testing sequence, Step runs each step of the tests. The Step buttons interact with an Integration Tester By Steps helper.
The helper receives an Init with the Number Of Commands as parameter, (currently the helper generates random commands by itself, so it just needs to know how many commands to generate). The helpe provides a Step method to execute the next command, and a Needs More Steps property to indicate whether testing should continue.
The helper derives form INotifyPropertyChanged and has a Counter dependency property that is displayed on the main window.
The states of the Test and Step buttons are controlled by three helper methods: SetButtonsFor_OutsideTesting, SetButtonsFor_InsideTestingOutsideAnyStep and SetButtonsFor_InsideTestingInsideAStep.
First, I verified that everything is working manually, and then I added a timer and automated the process using the Stack Overflow suggestions on how to programmatically click a button in WPF and how to make a WPF Timer Like C# Timer.
Now, here's the Main Window's code:
private void Test_Click(object sender, RoutedEventArgs e)
{
SetButtonsFor_InsideTestingOutsideAnyStep();
RunTheTestBySteps();
}
public readonly IntegrationTesterBySteps _integrationTesterBySteps =
new IntegrationTesterBySteps();
void RunTheTestBySteps()
{
SetButtonsFor_InsideTestingOutsideAnyStep();
IntegrationTesterBySteps.Init(10);
StartTheTimer();
}
private void StartTheTimer()
{
DispatcherTimer = new DispatcherTimer();
DispatcherTimer.Tick += DispatcherTimer_Tick;
DispatcherTimer.Interval = new TimeSpan(0, 0, 1);
DispatcherTimer.Start();
}
private void StopTheTimer()
{
DispatcherTimer.Stop();
DispatcherTimer.Tick -= DispatcherTimer_Tick;
}
private DispatcherTimer DispatcherTimer { get; set; }
private void DispatcherTimer_Tick(object sender, EventArgs e)
{
if (!BtnStep.IsEnabled) return;
ClickTheStepButton();
CommandManager.InvalidateRequerySuggested();
}
private void BtnStep_Click(object sender, RoutedEventArgs e)
{
SetButtonsFor_InsideTestingInsideAStep();
IntegrationTesterBySteps.Step();
if (this.IntegrationTesterBySteps.NeedsMoreSteps)
SetButtonsFor_InsideTestingOutsideAnyStep();
else
{
SetButtonsFor_OutsideTesting();
StopTheTimer();
}
}
private void ClickTheStepButton()
{
var peer = new ButtonAutomationPeer(BtnStep);
var invokeProv = peer.GetPattern(PatternInterface.Invoke)
as IInvokeProvider;
if (invokeProv != null)
invokeProv.Invoke();
}
void SetButtonsFor_InsideTestingInsideAStep()
{
BtnTest.IsEnabled = false;
BtnStep.IsEnabled = false;
}
void SetButtonsFor_InsideTestingOutsideAnyStep()
{
BtnTest.IsEnabled = false;
BtnStep.IsEnabled = true;
}
void SetButtonsFor_OutsideTesting()
{
BtnTest.IsEnabled = true;
BtnStep.IsEnabled = false;
}

WP8 Webbrowser, stop navigation without killing page

I have an app that uses a web browser control to navigate a website. When the user clicks on a hyperlink that will take them out of the site I want to be able to stop the navigation. I tried.
void wgBrowser_Navigating(object sender, NavigatingEventArgs e)
{
if (!e.Uri.AbsoluteUri.Contains(BASEURL))
{
e.cancel = true;
return;
}
progressPB.Visibility = Visibility.Visible;
}
This does in fact stop the navigation, but it kills the page causing a navigation error message to appear in the webbrowser.
I have tried a few things including a wgBrowser.Goback() after navigation, but that is not a real "back", and causes the page to reload complete losing data the user previously entered.
Any help will be greatly appreciated.
Thanks you.
Are u tried IsolatedStorageSettings? Because using IsolatedStorageSettings store the data will be better when the page is navigate.
Example:
private void tbUserName_SelectionChanged(object sender, RoutedEventArgs e)
{
if (appSetting2.Contains("MainPage2"))
{
appSetting2["MainPage2"] = this.tbUserName.Text;
}
else
{
appSetting2.Add("MainPage2", this.tbUserName.Text);
}
}
Use can use the InvokeScreipt method over WebBrowser control to stop page navigation.
yourWebBrowser.InvokeScript("eval", "document.execCommand('Stop');");

Resources