How can I wait for a network call in Selenium? - c#

If I'm reading this correctly, it should be possible to read the network tab of Chrome dev tools using Selenium.
I spent a few hours trying to set this up yesterday and got nowhere. This is for UI tests so what I'm trying to do is:
Set a value in a filter by clicking on it
Wait for GET that fires to return
Set a value in another filter by clicking
...
At the moment, I have a hard-coded 1 second wait that I am very unhappy with. If there's no wait in there, the test gets ahead of itself and fails.
Help please!

I had to switch part of my application to python to use selenium wire to get this done, I am not sure how to do it in c# but this was super simple in python. It offers a ton of control over Network requests and responses. Just wanted to put it here in case this is an option for you. I just made my c# execute a python script that runs its own instance of chrome.
Here are some docs:
https://pypi.org/project/selenium-wire/#example-mock-a-response
https://github.com/wkeeling/selenium-wire#all-options
Selenium Wire is super powerful and the easiest way I found to manipulate or execute actions based on network traffic. Below are two examples. One waiting for a network call. And the other intercepting a network request with a custom response.
from seleniumwire import webdriver
driver = webdriver.Chrome()
driver.wait_for_request('/signup/lifeline/nv/status', 60)
This will wait up to 60 seconds for that request before timing out.
from seleniumwire import webdriver
driver = webdriver.Chrome()
Endpoint = "/endpoint"
def interceptor(request):
if 'Endpoint' in request.url:
request.create_response(
status_code=200,
headers={'Content-Type': 'application/json; charset=utf-8', 'Access-Control-Allow-Origin': '*'}, # Optional headers dictionary
body=EndPointResponse)
self.driver.request_interceptor = interceptor
This will intercept the request when it is made and replace the response with whatever you would like.

Related

Selenium C# Click() cant get to another URL while using Jenkins

I have 30 tests working locally with Selenium C# in headless mode.
The problem occurs, when I try to use Jenkins. The message is "Unable to locate element".
It took lots of time to understand that the problem is not time wait and not frame size.
Chrome options (they say frame size will help, but its not):
ChromeOptions co = new ChromeOptions();
co.AddArgument("--window-size=2560,1440")
co.AddArgument("--ignore-certificate-errors")
co.AddArgument("headless")
Tested web application was made with vue.js (everything is dynamically generated).
To get the page or subpage displayed by URL Jenkins needs about 2-4 seconds:
driver.Url = "http://app.com/home" //not my URL
And every Click() works good on a certain URL: you can click anywhere, even on a pop-up form, if the URL is the same (Click() takes about 600 ms).
But if you Click() on element which directs to another URL, the page is not displayed (even if you wait 80 seconds).
This problem could be solved by using
driver.Url = "http://app.com/home/menu"
but thats not real testing.

ERR_CONNECTION_RESET error when calling Edge.func in C# async method through POST service

I have also raised an issue in the github pages for Edge, linked here
https://github.com/tjanczuk/edge/issues/579
I am trying to utilize Edge in my .NET application to run a quick node program to do some minor video processing.
I have verified that the javascript works - tested it independently running 'node test.js'. And I have verified that I am calling and using the Edge.func method properly by creating a small C# console app and testing to see if, when I run it in CMD (testApp.exe), the video processing works through node using Edge (it does).
The problem is that when I try to run the async method shown below through my POST web service using Edge I get the first log - 'In Edge Task' - but I never get the log at the bottom - 'in async call' - and client side I get a network error saying failed net::ERR_CONNECTION_RESET.
public static async Task Start()
{
LogManager.Instance.WriteLog("In Edge Task");
var func = Edge.Func(#"
var ffmpeg = require('fluent-ffmpeg');
return function (data, callback) {
var proc2 = ffmpeg('testFile-0.webm')
.input('testFile-1.webm')
.on('error', function(err) {
callback(null, 'ERRORRRR');
})
.on('end', function() {
callback(null, '');
})
.mergeToFile('merged-fromNET.mp4', 'tempDir');
}
");
LogManager.Instance.WriteLog("in async call");
await func(null);
}
This async task Start() method works properly if I call it from my test console app...
I have verified my post service is set up correctly because without executing this method using Start.Wait(), commenting it out, the service executes properly. I have installed the necessary node modules and the used video files and necessary dlls in the proper directories so I am sure that is not the issue.
Please help, thank you for your time.
So I figured out the answer to my question with the help of this question -
Starting a process with credentials from a Windows Service
It seems that a web service is executed with "Network Service" permissions and upon checking windows event viewer I was getting an access violation - meaning I needed to execute this chunk of code as a user with proper permissions. So I followed the logic provided in the question I linked to in order to create a process that would execute a simple exe I set up to execute the chunk of code I posted. I had to be careful to give my user proper permissions to access the folder with the exe and that was pretty much it.
With this technique, it seems easy to set up a web service that executes node.js logic with Edge https://github.com/tjanczuk/edge - Thanks tjanczuk

Calling website through Windows Service

Looking at creating a windows service that will run on an IIS Server. Said service will access an url (mysite.com/mailflow.ashx) every 5 minutes. This is done to get the mailflow going, which is done by accessing the mailflow.ashx file.
Is: https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.aspx
what I'm looking for in my scenario or is there another class that will do the job?
Coming from a non-coding background but I want to do this on my own as a learning experience.
Small hints are more than enough.
Cheers!
Unless you really want to learn coding, I'd recommend simply creating a scheduled task in windows. Make it call the following js script (called wget in my case)
var WinHttpReq = new ActiveXObject("WinHttp.WinHttpRequest.5.1");
WinHttpReq.Open("GET", WScript.Arguments(0), /*async=*/false);
WinHttpReq.Send();
WScript.Echo(WinHttpReq.ResponseText);
and set the task to start the following program cscript with the following arguments /nologo C:\Path\To\wget.js http://my.url.com
There are plenty of ways to ping an remote url, so you may decide to use another option (perhaps simply launching then killing a browser) but coding a service really is overkill if this is the only task you need
Yes, the HttpWebRequest class is what you need.

Selenium Grid2 Port not working only on port 4444

I have set up Selenium Grid2 and I have a dilemma that is driving me crazy.
If I start the Hub in the default port (4444), for some reason all my HTTP requests are redirected to a page that says
You are using grid .0.0Find help on
the official selenium wiki : more help
here
This happens in all browsers and in any application that connects to the internet.
If I change the port to anything else, the problem doesn't occur, but then tests in Internet Explorer (IE8 specifically) no longer work. They fail with an error about not being able to find an element.
This also happens with a non-grid setup, just using the server (selenium-server-standalone-2.0rc3.jar) except instead of the grid message all HTTPS connections are untrusted.
Any ideas?
That's the page you get when you fetch the "/" resource. But any page you care about almost certainly sits on another context. Here's the relevant portion from the Hub's code:
root.addServlet("/*", DisplayHelpServlet.class.getName());
root.addServlet("/grid/console/*", ConsoleServlet.class.getName());
root.addServlet("/grid/register/*", RegistrationServlet.class.getName());
// TODO remove at some point. Here for backward compatibility of
// tests etc.
root.addServlet("/grid/driver/*", DriverServlet.class.getName());
root.addServlet("/wd/hub/*", DriverServlet.class.getName());
root.addServlet("/selenium-server/driver/*", DriverServlet.class.getName());
root.addServlet("/grid/resources/*", ResourceServlet.class.getName());
root.addServlet("/grid/api/proxy/*", ProxyStatusServlet.class.getName());
root.addServlet("/grid/api/testsession/*", TestSessionStatusServlet.class.getName());
// Selenium Grid 1.0 compatibility routes for older nodes trying to
// work with the newer hub.
root.addServlet("/registration-manager/register/*", RegistrationServlet.class.getName());
root.addServlet("/heartbeat", Grid1HeartbeatServlet.class.getName());
Do you have your nodes connecting to the correct URL? They should be started with something like -hub http://localhost:4444/grid/register to register properly.

Proxying Videos locally with a TCP Socket

I've been really interested in adding support for video podcasts to Media Browser.
I would like users to be able to navigate through the available video podcasts and stream them from the internets. That's really easy cause media player etc.. will happily play a file that lives in the cloud.
The problem is that I want cache these files locally so subsequent viewings of the same episode will not involve streaming and instead will play the local file.
So... I was thinking, why not host an HttpListener and as media player asks it for bits of the file, have the HttpListener download and store it locally. Next time a user plays the file we will already have portions of the file locally.
Does anyone know of example code that uses HttpListener for proxying?
EDIT
The idea would be only to proxy simple streamable content like MP3 or Mov.
The bounty will go to an actual implementation.
Here is the API I would like:
// will proxy a uri on the local port, if cacheFile exists it will resume the
// download from cacheFile.
// while the file is downloading it will be name cacheFile.partial, after the
// download is complete the file will be renamed to cacheFile.
// Example usage: ProxyFile("http://media.railscasts.com/videos/176_searchlogic.mov", 8000, #"c:\downloads\railscasts\176_searchlogic.mov")
//
// Directly after this call http://localhost:8000 will be the proxy stream, it will be playable locally.
void ProxyUri(Uri uri, int port, string cacheFile)
Edit 2
HttpListener is looking pretty unpromising I will probably need to do the work at a TCP socket level as HttpListeners seem to require the program runs as admin which is going to be really tricky.
I hadn't done anything with HttpListener before, so I thought this would be a nice little exercise to bring myself up to speed with it - and so it proved. I implemented it as a single ProxyListener class whose constructor takes the parameters of the ProxyUri function you specified. Once you obtain an instance, you start it listening (and potentially downloading) by calling its Start method. When you're done with it, call Cleanup.
There are one or two rough edges but basically it works as per your question. To test it, I built it up as a console application with a Program class which accepts input lines consisting of (uri, port, filename), space-separated, creates the ProxyListener instances and starts them. You can run this console application, type in a suitable line, and the downloader will start (printing out progress to console). Simultaneously you can e.g. fire up IE and fetch the file from the specified port, and you will be able to download it while the downloader is still working. The "uploader" progress will be printed to console, too.
I'm having a bit of trouble pasting it in here, maybe due to size (it's not that big, but bigger than the snippets you normally see here - the ProxyListener class is a tad under 200 lines). Does it sound interesting? If so, I'll post it to a pastebin and update this answer with a link.
Update: Posted as a gist.
Note that you will need Administrator privileges to run the program, since HttpListener requires this.
Update 2: Under certain circumstances, it is not necessary to have admin privileges to run HttpListener. See this link and this one. The idea is, if you can reserve an URL namespace during installation time, then the user does not have to have admin privileges if listening against that namespace.
Streaming was not designed to be saved, and also these protocols are very custom and very complex to implement, streaming sessions do lots of validation and synchronization which will be extremely difficult to imitate. Of course it is not an impossible task, but its fairly big task to do. Only other way is to read and save it as local media file, and use that as a reference. Because you can use windows media encoder to read stream and write stream data as local file, but it still may not allow you to do copy protected data.
Did you consider using HTTP proxy with caching features?
Like:
Apache httpd with mod_proxy and mod_cache
Squid
See also Web Cache # wikipedia
If you want your application to have such web cache component, I suggest you look for Web Cache implementation in .Net, and not code it from scratch.

Categories

Resources