automated task with selenium on a website - c#

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.

Related

Chrome WebDriver does not open when called from IIS

I have a really strange issue (And I have a really strong suspicion - that it will be on the IIS side).
I have .NETCORE6 application, which uses Selenium via IHostedService.
There are cases that work:
If I launch the application from Visual Studio's IIS locally the browser is opened
If I launch the application from CMD (as self-hosted) the browser is opened
If I launch the application from CMD (as self-hosted) on the server the browser is opened
BUT
If I launch the application from IIS on the server - the tests are running (looks like in headless mode) but the browser is not shown.
Does anybody have experience with IIS?
I have tried different IIS Pool Identity - and executed the app as a DefaulPoolIdentity, LocalService, LocalSystem, and even as my user which has admin rights - but the browser is still hidden.
Additional Notes:
I also checked - that the browser is launching in maximized mode and on the correct screen
So the question is how to launch the app from the server via IIS with the web browser opened? (There just must be the solution for that right?)
Lex Li's answer is the correct one - so if anyone else finds the answer please read the article https://blog.lextudio.com/web-application-differences-in-visual-studio-and-iis-60fec7e311b3
It seems that if you start chromedriver manually and then call it using Selenium Grid the browser will open.
Driver configuration would be something like
var uri = new Uri("http://localhost:9515/"); --my chromedriver uri
var driver = new RemoteWebDriver(uri, new ChromeOptions());
...

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);

Creating a web scraping program compatible with Windows task scheduler "run whether user is logged on or not"

I need to be able to web scrape information daily on a website that requires login credentials. I have wrote a program in C# that uses Selenium and Chrome, which works well when run normally, or through the Windows Task Scheduler when selecting "run only when user is logged on". However, Chromedriver.exe stalls when selecting "run whether user is logged on or not", presumably because this option forces chrome to run in the background and thus my program is not able to enter in any login credentials. I need to be able to run this program whether the user is logged on or not, because it is on a server where multiple users may be accessing it at a time.
I have tried wrapping the execution of the .exe within a batch file and using different web browsers (firefox/Chrome).
I am looking for
any solutions to make a Selenium app work as a background process with the Task Scheduler option "run whether user is logged on or not"
or
a framework other than Selenium with which I can automate entering text and clicking, and which can work in the non-interactive mode that is launched through the windows Task Scheduler.
Thanks for reading my question.
It seems that you are looking to use "headless chrome". According to Getting Started with Headless Chrome:
A headless browser is a great tool for automated testing and server
environments where you don't need a visible UI shell.
Look at the following chrome switches (options):
--headless (or "headless")
--disable-gpu
Here's are a couple of resources for chrome switches (options):
List of Chromium Command Line Switches
Chrome switches
According to the ChromeOptions Class documentation, you should be able to do the following:
ChromeOptions options = new ChromeOptions();
For use with ChromeDriver:
ChromeDriver driver = new ChromeDriver(options);
The following post may be helpful: How to start ChromeDriver in headless mode

Selenium webdriver test 100% pass locally, 100% fails on VSTS server

currently in my 2nd week of trying to work out why a certain test runs perfectly locally but fails when being kicked off by a VSTS agent.
Program flow:
Select an row from a table
Click Verify button
CSS overlay darkens the screen, spinner in the middle
Toaster pop up in the bottom left saying Verification starting
About 1.5 minutes of time passes, spinner spins
CSS overlay removed
Toaster pop up bottom left, successful verification
I have screenshots and debug output on everystep of the way and the failure is at the "Waiting for CSS overlay to be removed" stage. It times out waiting for it, now matter how long the timeout.
Screenshot shows the CSS overlay and spinner still spinning even though the verification process completed successfully. Its almost as if it's stuck in time.
Scenarios and results:
Run from Local Visual Studio - PASS
VSTS Agent - FAIL
Manually run on VSTS Agent by remoting in and passing the exact commandline as the build - PASS (This is what infuriates me!!)
Downgrading / upgrading Webdriver - no difference
Headless / full mode - no difference
Using different types of waits - no difference
Waiting for Toaster pop ups instead of CSS - no difference
One thing to note from the documentation:
Agents that are configured to run as service can run Selenium tests
only with headless browsers. If you are not using a headless browser,
or if you are running UI tests for desktop apps, Windows agents must
be configured to run as an interactive process with auto-logon
enabled.
The fact that you also tried to run Headless might mean that even if you are running as a service and still got errors, this might not be your issue. In some cases, I have had issues with remoting to the machine messing up future test runs or video recording by logging out the user or changing the screen resolution. The documentation continues about ways to prevent this:
If you use Remote Desktop to access the computer on which an agent is
running with auto-logon, simply disconnecting the Remote Desktop
causes the computer to be locked and any UI tests that run on this
agent may fail. To avoid this, use the tscon command on the remote
computer to disconnect from Remote Desktop. For example:
%windir%\System32\tscon.exe 1 /dest:console
In this example, the number '1' is the ID of the remote desktop
session. This number may change between remote sessions, but can be
viewed in Task Manager. Alternatively, to automate finding the current
session ID, create a batch file containing the following code:
for /f "skip=1 tokens=3" %%s in ('query user %USERNAME%') do (
%windir%\System32\tscon.exe %%s /dest:console
)
Save the batch file and create a desktop shortcut to it, then change the shortcut
properties to 'Run as administrator'. Running the batch file from this
shortcut disconnects from the remote desktop but preserves the UI
session and allows UI tests to run.
I'll answer my own question: We created a new agent which was an auto login. We watched the test run and pressed F12 and there, in the console, was an error.
It was trying to trim() on the username, the test account looked like it had a username but I guess it didn't really. So the system, when run by the agent, was throwing an error and not handling it.
Handled the error and everything went through swimmingly. Very long and frustrating road to get to here! Hope this helps someone in the future.
Please check Azure DevOps Hands-On-Labs to get started with Automating Selenium Tests in Azure Pipelines. Hope this helps...
VSTS will run Selenium and the browser as local admin. The browser will refuse that when started in sandbox mode.
Check for articles related to running Selenium as local admin

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