WebBrowser not executing Google Analytics Javascript - c#

I have a website running locally and I want to test the GA code I have by hitting the website repeatedly (with a delay) using C#'s WebBrowser class. The issue is that the GA code (the event) is sent with $('document').ready() and I cannot for the life of me get WebBrowser to actually load the javascript on the page.
I was able to get this to work by using Chrome or Firefox by using var proc = new Process("firefox.exe", "my-ga-args") and that works, but I'd like to run this in the background while I'm doing other work and even if I modify the process to start minimized it still takes focus away from my main window when it closes and re-opens Firefox (using different GA parameters that I have in a list that its looping through).
So, is there any way to get WebBrowser to actually execute javascript that exists on the webpage and have it hidden from view while running?

I think you're looking for something like Google's Puppeteer or given your C# tag, perhaps the .NET port Puppeteer Sharp.
... library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

Related

Is it possible to open Chrome process and execute javascript on a website in the Chrome window?

For a C# WPF application I'm creating, I would like to open a website in Google Chrome.
Chrome is standalone and totally independent from my application.
I start Chrome like the following:
Process.Start("chrome.exe", "http://www.example.com");
So an independent process is started, which is the Chrome instance that loads the desired web address.
What I would like to do, is running arbitrary javascript code on the loaded webpage. Like if the javascript code would be 'injected'.
I do not control the loaded website.
The purpose is to open a third-party webpage and pre-fill in some form fields so the user doesn't have to do this all time.
Is there any possibility to achieve this?
Yes, I've done this many time using Seleniums's JavaScriptExecutor.
You'll want to launch Chrome using the Selenium Driver instead of launching the Chrome process yourself.
https://www.guru99.com/execute-javascript-selenium-webdriver.html
From their example:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(Script,Arguments);

Can we capture GET responses via ChromeDriver Selenium?

When i navigate a page, the page makes several GET requests. I wonder that, can I capture individual GET requests by using ChromeDriver Selenium?
C# 4.6.2
Selenium itself can't capture network traffic, but you could use it with this embedded proxy and it should work fine.

Can I call Greenshot on a client's machine from a website?

I am looking to make Greenshot take an IE screen shot from the code behind of a C# Web Form. How can I call Greenshot from my server or code running in the browser and have it take a screenshot on the client's machine?
The code behind of a web form (or really, any code behind in ASP) is run on the server. The server cannot directly run code on a client (it can, of course, inject javascript into its responses).
So no, you won't be able to do this in the manner you describe. However, if a given page had JS that performed this task for you on page load (or on a button click, or whatever), you could likely get a similar result.
Looking at Greenshot it looks like that is a program that is run on the client's computer, so this is even more impossible as the browser sandbox isn't going to let JavaScript run that application. There are possibly other ways to get an image of the rendered content in JavaScript, but that approach seems very unlikely to work.

automated task with selenium on a website

I have an application that is accessed by many users. It's a .net 4.0 C# web forms application, and they requested me to automate a task that they have to do almost daily. The task I should automate is to access a link outside my domain and fill it up with some info, log-in, and other stuff like dates.
So, after many researching and reading, I've managed to do it locally with Selenium. I have the entire process automated and I call it from a link inside my website. Locally it works like a charm.
Here's the code:
public void AutomateTask()
{
var url = new Uri("http://127.0.0.1:4545/wd/hub");
var capability = DesiredCapabilities.Chrome();
var driver = new RemoteWebDriver(url, capability);
driver.Navigate().GoToUrl("url");
//do all the stuff I need to do
driver.Close();
}
I start my server using this command:
java -jar C:\...\selenium-server-standalone-2.43.1.jar -role hub -port 4545
The main problem is that I have to publish my website for the users and they should be able to use the automated process without having to start a selenium node on their machines or anything else. Locally I have a node running for each browser, is it possible for them to consume this webdrivers? I mean, users should not worry about installing or anything, they should just click the link and see their browsers acting till the task is over. When I click at the link on my application(from my local IIS) on another computer, a new window of the request browser is opened on server machine, not on the machine which clicked the link.
I've looking everywhere for a solution for this but none was found.
So:
I have a website that access a third website.
I need to automate this process.
Users must only click the link, wait the new browser window open, wait for some fields to be filled automatic and then fill the captcha and press "continue", then enjoy their browser working.
Is it possible with selenium? If not, which framework or how can I achieve this?
Let me know if any further details are required.
If you don't need to show the browser to the users you could just put a link on your webapp which runs selenium on the machine hosting your webapp. That way you only need to have selenium installed and running on the webserver hosting your webapp and not all the end user machines. If you do it this way you should probably use the HtmlUnitDriver because its headless and you really aren't going to be watching these selenium tests executing.

C# browser automation that will work on server

I have been using WatiN for some browser automation and website testing. Recently I received a request to automate some task that needs to check a partner website for existence of some SKU (since they don't have a proper API) and confirm transaction.
I tried using WatiN, but since this runs on a server and on-demand, the server desktop is naturally locked at most times and the IE window does not open and the process is never run.
I am looking for an alternative to WatiN, which is preferably a .net library (not a must, but just makes things easier), does not require a logged in user and being free and open source is always nice.
Anyone have experience with this type of automation?
You can use the Selenium WebDriver coupled with PhantomJS.
Selenium is similar in many ways to WaitN but it supports more browsers. PhantomJS is a headless browser and wrapped in a portable executable you can run from your web server.
Once you've added the NuGet packages, you will be able to instantiate a PhantomJS web driver and control a site without having to launch a full-fledged browser.
var driver = OpenQA.Selenium.PhantomJS.PhantomJSDriver();
We use this on build servers since the build agents are not logged in and won't be able to launch a normal browser process.
If you just need to check some HTML, you can use WebClient to make a request to the site, return the content response as a string and parse it.

Categories

Resources