Welcome Guest Search | Active Topics | Sign In | Register

Unit / Integration Testing WebView Options
Adam Skinner
Posted: Monday, August 31, 2015 11:28:41 AM
Rank: Newbie
Groups: Member

Joined: 8/31/2015
Posts: 2
I'm starting to do some very basic testing with the WebView class and am having some trouble setting the Url property and verifying that it's value is the value that I gave it.

In fact, when I set the Url value of the WebView, it's Url property is an empty string. I see the same behaviour with GetText and GetHtml (not quite sure what the difference in between those two).

An example of that simple test might be:
Code: C#
[Test]
public void HaveAUrl()
{
    var w = new WebView();
    w.Url = "about:blank";
    Assert.IsNotNullOrEmpty(w.Url);
}


Is also see this behaviour when debugging at runtime.

At the end of the day, my objective is to invoke some javascript on my rendered webpage and ensure that new elements have been added to the DOM. I'm intending to do this by reading the html from the webview after the script is invoked and verifying that the new elements are in place.
eo_support
Posted: Monday, August 31, 2015 11:36:23 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

You will need to wait for the UrlChanged event. Setting Url is asynchronous. It sends a "Load Url" request to the browser engine and returns immediately. Later if the initial loading phase has completely successfully, UrlChanged event will be fired, at which point of Url property will reflect the new value. If for some reason this stage fails (for example, the Url is not a valid syntax that the browser engine can recognize), then you will not reach this point.

As a result, you should follow these steps to perform the test:

1. Set the WebView's Url;
2. Start a timer for a given amount of time (for example, 3 seconds);
3. Check whether the Url matches what you set when your timer is triggered;

Hope this helps. Please feel free to let us know if you still have any questions.

Thanks!
Adam Skinner
Posted: Monday, August 31, 2015 11:59:20 AM
Rank: Newbie
Groups: Member

Joined: 8/31/2015
Posts: 2
Here's a fully encapsulated snippet that doesn't work. I create a new webview, set its url, wait 5 seconds for it to raise the UrlChanged for "about:blank", and it says that the url has not changed.

Code: Visual Basic.NET
<Test>
    Public Sub HaveAUrl
        Dim urlChanged As Boolean = False
        Sut = new WebView()

        Assert.IsNullOrEmpty(Sut.Url)
        AddHandler Sut.UrlChanged, Sub(s, e) urlChanged = True
        
        Sut.Url = "http://google.com"

        Thread.Sleep(5000)

        Assert.IsTrue(urlChanged)
        Assert.IsNotNullOrEmpty(Sut.Url)
    End Sub
eo_support
Posted: Monday, August 31, 2015 12:07:51 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,229
Hi,

There are two problems in your code:

1. You must create WebView.Create to create the WebView first;
2. You can not use Thread.Sleep. You must keep your message loop running. This usually means you can not do both set and check in the same method;

These generally are no issues for an UI application. In case your application is not a traditionally UI application, see here for more details:

http://www.essentialobjects.com/doc/webbrowser/start/webview_no_ui.aspx

Thanks


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.