public void ShouldBeAbleToOverrideScriptInPageFromPhantomJSContext() { if (!(driver is PhantomJSDriver)) { // Skip this test if not using PhantomJS. // The command under test is only available when using PhantomJS return; } PhantomJSDriver phantom = (PhantomJSDriver)driver; // Can we override some browser JavaScript functions in the page context? object result = phantom.ExecutePhantomJS("var page = this;" + "page.onInitialized = function () { " + "page.evaluate(function () { " + "Math.random = function() { return 42 / 100 } " + "})" + "}"); phantom.Url = EnvironmentManager.Instance.UrlBuilder.WhereIs("injectableContent.html"); IWebElement numbers = phantom.FindElement(By.Id("numbers")); bool foundAtLeastOne = false; foreach (string number in numbers.Text.Split(' ')) { foundAtLeastOne = true; Assert.AreEqual("42", number); } Assert.IsTrue(foundAtLeastOne); }
private string ExecuteCommand(string url) { try { using (var phantom = new PhantomJSDriver()) { var indexFile = Server.MapPath("~/Scripts/PhantomJS/index.js"); var scriptSource = System.IO.File.ReadAllText(indexFile); var script = phantom.ExecutePhantomJS(scriptSource); phantom.Navigate().GoToUrl("https://www.bing.com"); phantom.FindElement(By.Id("sb_form_q")).SendKeys("learn2automate"); //Click on Search phantom.FindElement(By.Id("sb_form_go")).Click(); Screenshot sh = phantom.GetScreenshot(); sh.SaveAsFile(@"C:\Temp.jpg", ImageFormat.Png); phantom.Quit(); } } catch (Exception ex) { } return string.Empty; }
public void ShouldBeAbleToReadArgumentsInScriptExecutedInPhantomJSContext() { if (!(driver is PhantomJSDriver)) { // Skip this test if not using PhantomJS. // The command under test is only available when using PhantomJS return; } PhantomJSDriver phantom = (PhantomJSDriver)driver; // Can we read arguments? object result = phantom.ExecutePhantomJS("return arguments[0] + arguments[0]", 1L); Assert.AreEqual(2L, (long)result); }
public void ShouldBeAbleToReturnResultsFromScriptExecutedInPhantomJSContext() { if (!(driver is PhantomJSDriver)) { // Skip this test if not using PhantomJS. // The command under test is only available when using PhantomJS return; } PhantomJSDriver phantom = (PhantomJSDriver)driver; // Do we get results back? object result = phantom.ExecutePhantomJS("return 1 + 1"); Assert.AreEqual(2L, (long)result); }