Noksa / WebDriver.Screenshots.Extensions

C# WebDriver screenshots extensions, like a screenshot entire page
MIT License
12 stars 8 forks source link

javascript error: $ is not defined - Error returned when taking screenshot #24

Open domwills opened 4 years ago

domwills commented 4 years ago

When attempting to take a screenshot using the snippet below I get an error back.

Error:

OpenQA.Selenium.WebDriverException
  HResult=0x80131500
  Message=javascript error: $ is not defined
  (Session info: chrome=79.0.3945.117)
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScriptCommand(String script, String commandName, Object[] args)
   at OpenQA.Selenium.Remote.RemoteWebDriver.ExecuteScript(String script, Object[] args)
   at OpenQA.Selenium.Support.Extensions.WebDriverExtensions.ExecuteJavaScriptInternal(IWebDriver driver, String script, Object[] args)
   at OpenQA.Selenium.Support.Extensions.WebDriverExtensions.ExecuteJavaScript[T](IWebDriver driver, String script, Object[] args)
   at WDSE.Helpers.SizesHelper.GetHeight(IWebDriver driver, Entity entity)
   at WDSE.Decorators.VerticalCombineDecorator.CombineScreenshots(IWebDriver driver)
   at WDSE.Decorators.VerticalCombineDecorator.MakeScreenshot(IWebDriver driver)
   at WDSE.ExtensionMethods.TakeScreenshot(IWebDriver driver, IScreenshotStrategy strategy)

Snippet:

var driver = new ChromeDriver();

driver.Navigate().GoToUrl("https://github.com/Noksa/WebDriver.Screenshots.Extensions/wiki/How-to-use");

var vcd = new VerticalCombineDecorator(new ScreenshotMaker());
var screenshot = driver.TakeScreenshot(vcd);
Noksa commented 4 years ago

Hello.

The reason is github's security policy:

Refused to load the script 'https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.3.1.min.js' because it violates the following Content Security Policy directive: "script-src github.githubassets.com". Note that 'script-src-elem' was not explicitly set, so 'script-src' is used as a fallback.
domwills commented 4 years ago

I presume this is the same issue I'm having with our website.

Would there be any workaround for this?

Noksa commented 4 years ago

You can add this to config file of the site in the header Content-Security-Policy. Just search for these words throughout the directory of the site. This setting can be written anywhere. Then add https://*.aspnetcdn.com to the security config.

Or try this: https://stackoverflow.com/a/35273628