Files
cefsharp/CefSharp.Test/OffScreen/OffScreenBrowserBasicFacts.cs
T
amaitland 2b3508d60a OffScreen/OffScreenBrowserBasicFacts- Add CrossSiteNavigationJavascriptBinding Test case
Very basic, little crude, passing though
Still having some problems with Xunit and shutting down on the correct thread.
2018-01-22 13:06:09 +10:00

103 lines
3.3 KiB
C#

// Copyright © 2010-2017 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Text;
using System.Threading.Tasks;
using CefSharp.OffScreen;
using Xunit;
using Xunit.Abstractions;
using Xunit.Extensions;
namespace CefSharp.Test.OffScreen
{
//NOTE: All Test classes must be part of this collection as it manages the Cef Initialize/Shutdown lifecycle
[Collection(CefSharpFixtureCollection.Key)]
public class OffScreenBrowserBasicFacts
{
//TODO: Move into own file/namespace
public class AsyncBoundObject
{
public bool MethodCalled { get; set; }
public string Echo(string arg)
{
MethodCalled = true;
return arg;
}
}
private readonly ITestOutputHelper output;
private readonly CefSharpFixture fixture;
public OffScreenBrowserBasicFacts(ITestOutputHelper output, CefSharpFixture fixture)
{
this.fixture = fixture;
this.output = output;
}
[Fact]
public async Task CanLoadGoogle()
{
using(var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.LoadPageAsync();
var mainFrame = browser.GetMainFrame();
Assert.True(mainFrame.IsValid);
Assert.True(mainFrame.Url.Contains("www.google"));
output.WriteLine("Url {0}", mainFrame.Url);
}
}
[Fact]
public async Task CanLoadGoogleAndEvaluateScript()
{
using (var browser = new ChromiumWebBrowser("www.google.com"))
{
await browser.LoadPageAsync();
var mainFrame = browser.GetMainFrame();
Assert.True(mainFrame.IsValid);
Assert.True(mainFrame.Url.Contains("www.google"));
var javascriptResponse = await browser.EvaluateScriptAsync("2 + 2");
Assert.True(javascriptResponse.Success);
Assert.Equal(4, (int)javascriptResponse.Result);
output.WriteLine("Result of 2 + 2: {0}", javascriptResponse.Result);
}
}
[Fact]
public async Task CrossSiteNavigationJavascriptBinding()
{
const string script = @"
(async function()
{
await CefSharp.BindObjectAsync('bound');
bound.echo('test');
})();";
var boundObj = new AsyncBoundObject();
var browser = new ChromiumWebBrowser("https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/url");
browser.JavascriptObjectRepository.Register("bound", boundObj, true);
await browser.LoadPageAsync();
browser.GetMainFrame().ExecuteJavaScriptAsync(script);
await Task.Delay(2000);
Assert.True(boundObj.MethodCalled);
boundObj.MethodCalled = false;
browser.Load("https://www.google.com");
await browser.LoadPageAsync();
browser.GetMainFrame().ExecuteJavaScriptAsync(script);
await Task.Delay(2000);
Assert.True(boundObj.MethodCalled);
}
}
}