Selenium Webdriver opens Firefox and then dies - c#

I have a super simple test script (below) to get started with WebDriver. When I run the test (C# - Visual Studio 2015), it opens up a Firefox browser and then does nothing.
There are several posts out there that talk about the following issue, which I'm also getting:
OpenQA.Selenium.WebDriverException: Failed to start up socket within 45000 milliseconds. Attempted to connect to the following addresses: 127.0.0.1:7055.
But those posts regarding this problem are quite old and also have one major difference- their FF browser didn't open; mine does.
The error:
The code:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace seleniumDemo
{
[TestClass]
public class UnitTest1
{
static IWebDriver driverFF;
[AssemblyInitialize]
public static void SetUp(TestContext context)
{
driverFF = new FirefoxDriver();
}
[TestMethod]
public void TestFirefoxDriver()
{
driverFF.Navigate().GoToUrl("http://www.google.com");
driverFF.FindElement(By.Id("lst-ib")).SendKeys("Selenium");
driverFF.FindElement(By.Id("lst-ib")).SendKeys(Keys.Enter);
}
}
}
This question is different from what's been suggested as a duplicate because the FireFox browser actually opens in this case. In the other questions, it wasn't responding at all.

it seems to be related to Selenium and Firefox version incompatibility. I also faced the same error when selenium on my machine was unable to communicate to firefox. I upgraded firefox to 46.x and it started working.
You can find version compatibility information over web or refer selenium changelog as well.

Use MarrioneteDriver to use latest version of Firefox.
Below is the Java code, you can write in C# accordigly (Make sure you have geckodriver.exe under BrowserDriver folder in your project folder)
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/BrowserDrivers/geckodriver.exe");
DesiredCapabilities cap = DesiredCapabilities.firefox();
cap.setCapability("marionette", true);
WebDriver driver = new MarionetteDriver(cap);

You can download latest version of MarrioneteDriver from below :
https://github.com/mozilla/geckodriver/releases
And you should Marionette executable to Windows system path :
To add the Marionette executable to Windows system path you need
update the Path system variable and add the full directory to the
executable.
To do this, right-click on the Start menu and select System. On the
left-side panel click Advanced system settings and then Environment
Variables button from System Properties window. Now the only step left
to do is to edit Path system variable and add the full directory to
your geckodriver (you may need to add a semi-colon before doing this,
if not already present) and you’re good to go.
Then simply create your driver instance :
var driver = new FirefoxDriver(new FirefoxOptions());

Related

How to Create a Test Project for Winappdriver with C#

I need to learn about how to set-up a winappdriver windows app test project in visual studio and inspecting the elements for a medium size windows application.
Navigate to the WinAppDriver GitHub web page and download the latest
version. I downloaded the 1.2 version
After installation, you will find the diver at C:\Program Files
(x86)\Windows Application Driver location.
First, we need to enable Developer Mode on our Windows 10, Developer
mode is not enabled. Enable it through Settings and restart Windows
Application Driver
If you have Microsoft Visual Studio installed, you will most likely
have Element Inspector already installed on your machine
In case you do not have it installed, navigate to the Windows 10 SDK
site, download and install it.Run the inspect.exe and make sure it is
working fine
**.
Create a new NUnit test project
2- download appium dependency from NuGet
3- Update an existing UnitTest1.cs class as follows:**
using NUnit.Framework;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Windows;
using System
[SetUp]
public void Setup()
{
AppiumOptions Options = new AppiumOptions();
Options.AddAdditionalCapability("app", "C:\\Windows\\System32\\notepad.exe");
Options.AddAdditionalCapability("deviceName", "WindowsPC");
DesktopSession = new WindowsDriver<WindowsElement>(new Uri(DriverUrl), Options);
Assert.IsNotNull(DesktopSession);
}
[TearDown]
public void Close()
{
DesktopSession.CloseApp();
}
}
4= Run the test
Note: If some references or packages are missing, right-click on it and include it in the solution.
5- Create Windows Elements and perform actions
Open up application under test (AUT), in our case it is Notepad.exe, and Inspect.exe tool to fetch locators.
Click on the Notepad text area and find the AutomationId attribute on the right-hand side of the Inspect.exe tool.
WindowsElement NotepadTextArea = DesktopSession.FindElementByAccessibilityId(“15”);
NotepadTextArea.SendKeys(“Hello World”);
Try! try! ,Run the test! Run the test! Run the test!
#pawansinghncr's answer is correct, but I would also suggest trying out the UI Recorder, it's in the Releases page of the WinAppDriver's Github.

C# Selenium Chromedriver only shows 'data'

I created a very simple C# project to test Selenium and Chrome Driver, but when I run it, it gets stuck at the very first line, ie. creating ChromeDriver object.
I have set up as following:
.NET Core 3.1 console app (also tried .NET 4.7)
Installed Selenium.WebDriver 3.141.0 and Selenium.WebDriver.ChromeDriver 87.0.4280.8800.
My Chrome version is "Version 87.0.4280.88 (Official Build) (64-bit)". I have double-checked compatibility with Chrome and ChromeDriver.
Code:
using OpenQA.Selenium.Chrome;
namespace MyFirstSelenium
{
class Program
{
static void Main(string[] args)
{
ChromeDriver chrome = new ChromeDriver();
chrome.Navigate().GoToUrl("https://www.google.co.jp/");
}
}
}
visual studio screen
chromedriver.exe is copied to the debug folder as expected, so I assume the problem is neither compatibility nor path.
chromedriver
When I run the code, the result is an empty window with 'data' in the address bar. No error is thrown. I have searched for several hours and found some articles, but most of them are old and none of these answers led to success.
Any help is appreciated. Thank you.
Added chromedriver.exe verbose console output.
#Piotr M.
chromedriver console
logfile
https://drive.google.com/file/d/1ECOS8E55aaTFV63e8P-7n6uRVcf49PRN/view?usp=sharing
New code
Works when I first execute "chromedriver.exe --verbose --log-path=chromedriver.log", but without it, it throws WebDriverException.
OpenQA.Selenium.WebDriverException: 'A exception with a null response was thrown sending an HTTP request to the remote WebDriver server for URL localhost:9515/session. The status of the exception was UnknownError, and the message was: No connection could be made because the target machine actively refused it. No connection could be made because the target machine actively refused it.'
var chromeOptions = new ChromeOptions();
chromeOptions.BrowserVersion = "87";
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:9515"), chromeOptions);
Possibly a classic case of cyclic redundancy.
chrome is a reserved word, you may have to change the variable name to something else e.g. chromex. So your effective line of code will be:
ChromeDriver chromex = new ChromeDriver();
Additional Consideration
Consider the following aspects aswell:
Instead of using the ChromeDriver class use the IWebDriver interface.
IWebDriver driver = new ChromeDriver();
Instead of the partial url google.co.jp/ you need to pass the complete url through GoToUrl() as follows:
driver.Navigate().GoToUrl(#"https://www.google.co.jp/");

C# Google Chrome Selenium URL Navigation with BinaryLocation

Greetings StackOverflow Community,
My issue is simple. I have the following five lines of code, that I can't figure out why Google Chrome doesn't launch google.com when using a custom binary location.
var Chrome = new ChromeOptions();
Chrome.AddArgument("no-sandbox");
Chrome.BinaryLocation = #"C:\GoogleChrome\chrome.exe";
ChromeDriver driver = new ChromeDriver(#"C:\ChromeDriver", Chrome);
driver.Navigate().GoToUrl("https://www.google.com");
Any ideas? All I get is the default chromedriver URL of "data:," when Google Chrome launches.
Why isn't the driver.navigate command working when using a Chrome.BinaryLocation? If I comment out that line, than it works fine.
I am using the following:
Windows 7
Visual Studio Community Edition 2017
Google Chrome version 67
chromedriver 2.41
.NET 4.5 Bindings
hey i dont think you need of binary location
And maybe Chrome is already a type,
try this:
ChromeOptions options = new ChromeOptions();
options.AddArgument("no-sandbox");
var driver = new ChromeDriver(#"C:\GoogleChrome", options);
driver.Navigate().GoToUrl("https://www.google.com");
Okay, I found the answer to this question. I don't know why the Portable version of either Firefox, Chrome or other Chromium based browsers do not function this way, but I resolved this issue by copying the enterprise installation files from program files for Chrome to another directory on the computer and then pointing the Selenium script to use that binary location. Then it worked just fine.
It was also useful to point Chrome to a custom chrome profile location to keep more of the Chrome application from using the local users's AppData folder.
If anyone is interested in accomplishing the same task, I can provide some example code that accomplishes this task. Just message me for more details.

Visual Studio Selenium Test Starting Local Project

I have a solution with 2 projects.
The static web page project
The selenium tests project
Here's my Test File:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace SeleniuumTestSuite
{
[TestClass]
public class HomePageTest
{
private string baseURL = "http://localhost:56403/";
private static IWebDriver driver;
[AssemblyInitialize]
public static void SetUp(TestContext context)
{
driver = new ChromeDriver();
}
[TestMethod]
public void RemoteSelenium()
{
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl(this.baseURL);
}
[TestCleanup]
public void Finally()
{
driver.Quit();
}
}
}
I need to start the localhost project before the test case runs so that navigating to the localhost doesn't result in a 404. I found this post that seems to answer that question but I have no idea what library the solution is using.
I tried using NuGet to download Microsoft.AspNet.WebApi.WebHost but if I try to do private Server _webServer = new Server(Port, VirtualPath, SourcePath.FullName);, VS doesn't recognize Server and has no idea what library to import. So I'm kind of stuck here.
Any idea how to get this to work?
In order to solve the problem, it is necessary to run the two projects at the same time, as mrfreester pointed out in comments:
The main project in order to be able to run the main application.
The test project which will access the running main application to test it.
As mrfreester suggested, you might use two visual studio instances and it will work. Yet, to enhance this solution and manage everything in just one visual studio instance, you can run the main project using Debug.StartWithoutDebugging (default keyboard shortcut is ctrl + f5). This will effectively run the server for the application without starting VS debug mode, allowing you (and your test project) to normally use the application. The application will run even if you close your browser.
Be noted: if you start your application debugging normally, when you stop the execution, the server will stop, and you will have to start again without debugging to be able to pass your tests again.

InternetExlorerDriver cannot be found even after referencing all selenium DLLs

I'm trying to get started using Selenium by running some of the samples. I've seen others having trouble at run time getting the InternetExplorerDrive working, see How to instantiate InternetExplorerDriver with Selenium WebDriver using C#, but my problem is different. I get a compile time error that InternetExlorerDriver could not be found. I've installed all four "Official NuGet Packages: RC WebDriver WebDriverBackedSelenium Support" in my project.
I have the IEDriverServer.exe added to the project too but I'm not getting that far yet.
What reference am I missing?
using System;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
namespace SeleniumTest
{
class Program
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new InternetExlorerDriver(); //missing reference error here
Compile time error:
Error 1 The type or namespace name 'InternetExlorerDriver' could not be found (are you missing a using directive or an assembly reference?) c:\users\chadd\documents\visual studio 2013\Projects\SeleniumWebDr\SeleniumTest\Program.cs 22 37 SeleniumTest
I notice that in your code, you don't have a using statement for the IE driver's namespace. Add the following line to your source code:
using OpenQA.Selenium.IE;
See if that doesn't resolve the issue.
Additionally, in your code, you're attempting to instantiate a class named InternetExlorerDriver. You're missing a p there. InternetExplorerDriver is what you want.
Incidentally, Visual Studio should provide you with tooltip support to correct this issue.
You have to download the IE Explorer driver from selenium hq site in order to invoke the Internet explorer browser as selenium only supports firefox as its default browser.
Please find below the link: http://www.seleniumhq.org/download/
download the 32 bit or 64 bit internet explorer driver server and just add the below setting in your script:
System.setProperty("webdriver.ie.driver",
System.getProperty("user.dir") + "\\IEDriverServer.exe");
Webdriver driver = new InternetExplorerDriver();
assuming you have kept the exe file in the same project folder. hope this helps !!

Categories

Resources