Timed out waiting for async script result Selenium C# Protractor - c#

I'm trying to create an automation test script using Protractor.net for an AngularJS platform, with Selenium in C#. I've created the driver using the below code.
driver = new FirefoxDriver();
Ngdriver = new NgWebDriver(driver);
And then attempted to locate and element as follows:
Ngdriver.FindElement(NgBy.Model("vm.reference")).SendKeys("Test");
However, I'm receiving an exception: Timed out waiting for async script result after 45ms.
Thanks in advance

I resolved this by using the SetScriptTimeout.
ngDriver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromSeconds(10));

Related

How to handle failed connect of C# Selenium chromedriver

I have the issue that sometimes our application don't start and therefore a connect of the Selenium will fail. It is OK for me that I get the error and the test fails.
My issue is in these case: That the following commands still starts the chromedriver.exe but I don't get a driver object back. So I can't stop the driver afterwards.
_driver = new Driver()
.SetChromeOptionsWeb()
.SetDriver()
.StartDriver();
Is there any possibility to not end up with a chromedriver.exe deamon process at the machine?
Thanks in advance
Nico
You need to surround it into try/catch/finally block or just make use of using.
For example you could use this piece of code:
using var driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://example.com");
//
// some driver actions
//
// Here the purpose of this task is to keep the driver up.
await CustomTaskAsync();
// And here after last action the driver will be disposed

Selenium C# integration With Neoload performance testing tool

Could anyone help me to elaborate procedure for integrating selenium C# script with neoload tool.i wanted to integrate selenium C# and Appium C# coding with neoload .So I went though some steps in documentation and unable to run the example code and didn't get the point how we can integrated with selenium/appium C# script with neoload for chrome or any browser and perform load testing for web or mobile application with same tool .Could you please have a look on shared screen shot and help me to share valuable document with any simple example for understanding the concept/getting some confidence.Thanks for giving valuable time
I modified the code found here for the chrome driver: https://www.neotys.com/documents/doc/neoload/latest/en/html/#24364.htm
DesiredCapabilities cap = new DesiredCapabilities();
NLWebDriverFactory.AddProxyCapabilitiesIfNecessary(cap);
object proxyCapability = cap.GetCapability(CapabilityType.Proxy);
ChromeProfile profile = new ChromeProfile();
if (proxyCapability != null) {
profile.SetProxyPreferences(proxyCapability as Proxy);
}
ChromeDriver webDriver = new ChromeDriver(profile);
Your problem is that you need to provide the ChromeDriver with a profile which has the DesiredCapabilities whereas you try to apply them directly to the driver.

C# selenium wont find webdriver from directory

I cannot get selenium to find my webdrivers, wether it be firefox or chrome. I have tried downloading the dirvers manually and with the NuGet package manager, neither of them are working for me in C# but with python they work fine.
C# code that does not work. Gives out error:
Exception thrown: 'OpenQA.Selenium.DriverServiceNotFoundException' in WebDriver.dll
An exception of type 'OpenQA.Selenium.DriverServiceNotFoundException' occurred in WebDriver.dll but was not handled in user code
The file C:/webdrivers/geckodriver.exe does not exist. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases
Commeted code, found on some old stackoverflow question did not work either
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
class Scraper
{
/*
private static IWebDriver Driver { get; set; }
public static IWebDriver Init()
{
//System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", #"C:\webdrivers\geckodriver.exe");
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\webdrivers");
service.FirefoxBinaryPath = #"C:\Program Files\Mozilla Firefox\firefox.exe"; // May not be necessary
FirefoxOptions options = new FirefoxOptions();
TimeSpan time = TimeSpan.FromSeconds(10);
Driver = new FirefoxDriver(service, options, time);
return Driver;
}
*/
IWebDriver driver;
public void StartScraping()
{
//driver = Init();
driver = new FirefoxDriver(#"C:/webdrivers/");
}
}
Calling the StartScraping() from UWP applications MainPage.xaml.cs constructor
Python code that works just fine
import selenium
from selenium.webdriver import Firefox, Chrome
def startf():
driverf = Firefox("C:/webdrivers/")
driverf.get("https://www.google.com")
startf()
Some have said that I should add the webdrivers directory to PATH, that I've done as well but nothing changed
EDIT:
After returning to this problem a couple of days later and testing selenium with just a console application and it working properly there.
This would seem to be a problem with me using a blank Universal Windows Platform application instead of a Console App. Is there a way to "attach" or open a console window inside a UWP application? Or could that even fix this issue? if not, can I somehow open similar XML form window from a console application?
The best way to get rid of this is to use Path.DirectorySeparatorChar in the path instead of using / or \\.
You can use it like:
separator = Path.DirectorySeparatorChar;
path = $"C:{separator}webdrivers{separator}";
And now you can use this path while running firefox.

Selenium C#, WebDriverWait timeout

I wrote a console application (visual studio 2013, C#) to test my web site with selenium.
This is my code
public static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("myUrl");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(40));
wait.Until(ExpectedConditions.ElementExists(By.Id("wsConnected")));
driver.Quit();
}
wsConnected is a div I put in page with jquery after page load (less than 10 seconds).
I am using Selenium 2.46.0 with Firefox v39.0
Of course it doesn't work because I get the driver timeout error after 60 seconds, any help will be very appreciated.
Thanks,
Alessandro
[UPDATE]
wait.Until fails with this exception:
An unhandled exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.Support.dll.
Additional information: The HTTP request to the remote WebDriver server for URL http://localhost:7056/hub/session/86847fde-462b-47be-85e1-31cd51791dc3/element timed out after 60 seconds.
[UPDATE 2]
I downgraded Selenium to 2.43 and firefox to v32, no timeout, endless wait
Try to avoid using of ExpectedConditions. Cause of error will be much more clear.
The next code was tested with the latest version of selenium (Selenium Support Classes 2.48.2 and WebDriver 2.48.2 from nuget packages). It works fine even if element become visible in 10 seconds.
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.ru/");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(40));
wait.Until(d => d.FindElement(By.Id("logocont")));
driver.Quit();
If it does not help, please provide an inner exception of the timeout exception.

c# headless browser with javascript support for crawler

Could anyone suggest headless browser for .NET that supports cookies and authomatically javascript execution?
Selenium+HtmlUnitDriver/GhostDriver is exactly what you are looking for. Oversimplified, Selenium is library for using variety of browsers for automation purposes - testing, scraping, task automation.
There are different WebDriver classes with which you can operate an actual browser. HtmlUnitDriver is a headless one. GhostDriver is a WebDriver for PhantomJS, so you can write C# while actually PhantomJS will do the heavy lifting.
Code snippet from Selenium docs for Firefox, but code with GhostDriver (PhantomJS) or HtmlUnitDriver is almost identical.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// driver initialization varies across different drivers
// but they all support parameter-less constructors
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Cheese");
query.Submit();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
System.Console.WriteLine("Page title is: " + driver.Title);
driver.Quit();
}
}
If you run this on Windows machine you can use actual Firefox/Chrome driver because it will open an actual browser window which will operate as programmed in your C#. HtmlUnitDriver is the most lightweight and fast.
I have successfully ran Selenium for C# (FirefoxDriver) on Linux using Mono. I suppose HtmlUnitDriver will also work as fine as the others, so if you require speed - I suggest you go for Mono (you can develop, test and compile with Visual Studio on Windows, no problem) + Selenium HtmlUnitDriver running on Linux host without desktop.
I am not aware of a .NET based headless browser but there is always PhantomJS which is C/C++ and it works fairly well for assisting in unit testing of JS with QUnit.
There is also another relevant question here which might help you - Headless browser for C# (.NET)?

Categories