Visual Studio 2015 and Selenium: Adding chromedriver - c#

VS never ceases to amaze me. Or, rather, it never ceases to amaze me how I either forget how to do things between each time I create a new solution, or how things seem to change in between solutions.
I'm trying to use ChromeDriver with Selenium (in C#). I've installed the Selenium.Webdriver and Selenium.Chrome NuGet packages in the project, and everything seems fine. No errors on the using directives, and no errors when trying to create a new ChromeDriver.
Here's the Selenium driver code:
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Threading;
...
public class GCDriver {
public static IWebDriver Instance { get; set; }
public static void Initialize () {
Instance = new ChromeDriver();
}
public static void Wait(TimeSpan timeSpan) {
Thread.Sleep((int)(timeSpan.TotalSeconds * 1000));
}
public static void Close() {
Instance.Quit();
}
}
When trying to start (initialize) the ChromeDriver as I allways do, I get this error:
Test Name: One
Test FullName: MyTests.TestUnitTest1.One
Test Source: C:\Users\149999frho\Source\Workspaces\QA\Automation\MyTests\TestUnitTest1.cs : line 17
Test Outcome: Failed
Test Duration: 0:00:00,0118783
Result StackTrace:
at OpenQA.Selenium.DriverService.FindDriverServiceExecutable(String executableName, Uri downloadUrl)
at OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService()
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor()
at Automation.GCDriver.Initialize() in C:\Users\149999frho\Source\Workspaces\QA\Automation\Automation\Selenium\GCDriver.cs:line 16
at MyTests.TestUnitTest1.Init() in C:\Users\149999frho\Source\Workspaces\QA\Automation\MyTests\TestUnitTest1.cs:line 12
Result Message: Initialization method MyTests.TestUnitTest1.Init threw exception. OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html..
The thing is, the chromedriver.exe IS located in the Solution\Project\Automation\bin\Debug folder. As it should be, since the NuGet package(s) is installed.
What am I missing here? Do I have to add any other references after installing the NuGet package? Adding the chromedriver.exe file as a reference is not possible, btw - VS complains about it not being an actual COM reference or something like that.
The references seems to be in order, though - and the references folder looks EXACTLY the same as in the other solution where things are working:
UPDATE:
I found a workaround: Adding the path to the chromedriver.exe file when creating a new instance of ChromeDriver. Instance = new ChromeDriver(). However, this is an idiotic and useless solution, as I have to use the absolute path - VS doesn't accept a relative one. When sharing a project it will be downright impossible to manage.
So the problem is that the project doesn't know where the file installed by it's own NuGet package manager resides. Argh! How is this possible with a piece of software in this price range?!

Related

Selenium C# Mstest - chromedriver.exe does not exist. Getdirectory() referring some-other directory while executing vis mstest commands

when i'm running the selenium test from VS2017 it is able to pick the drivers successfully BUT when i run the same test using mstest command - internally it is referring some other directory!
C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\mstest.exe/
testcontainer:..\Test\Sun.TestAutomation.dll /test:"myfristtest"
/resultsfile:..\Test\TestResultLog.trx //Mstest commands
OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The file
C:\Test\xsed_2018-12-07 10_55_51\Out\chromedriver.exe does not exist.
The driver can be downloaded at
http://chromedriver.storage.googleapis.com/index.html.
code:
this.DriversPath = Path.GetFullPath(Path.Combine(Directory.GetCurrentDirectory()));
additional information:
drivers are available in debug folder why my mstest is referring the drivers in "Out" folder ??
This post is a bit old but since it was brought back to the the front, this may help someone.
I would download the ChromeDriver Nuget package. This way you always get the latest version.
Right click on your project > properties. Click on Build tab.
set Conditional compilation symbols = _PUBLISH_CHROMEDRIVER
under output path set: bin\Debug\
Once installed, clean solution and rebuild and you should see the file in the bin dir.
for your chromedriver call it should look something like this:
Driver = new ChromeDriver(Path.Combine(GetBasePath, #"bin\debug"), options);
Then add the GetBasePath code:
public static string GetBasePath
{
get
{
var basePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
basePath = basePath?.Substring(0, basePath.Length - 10);
return basePath;
}
}
This PC -> Properties -> advanced system settings -> Environment variables -> system variables -> Varible PATH add folder, where you have chromedriver.exe

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.

Selenium Webdriver opens Firefox and then dies

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

web testing project in visual studio 2015 wont work, why?

I just started using visual studio 2015 & c# language so I'm a complete newbie. anyway I was trying to write a web testing project with selenium (references) in vs2015 and the first few tutorials were relatively easy to understand, that's what I got:
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support;
namespace testsite
{
[TestClass]
public class firsttest
{
IWebDriver driver;
String url = "http://www.somewabpage.com";
[TestInitialize]
public void setup()
{
//Starting the browser and visiting the site
driver = new ChromeDriver();
driver.Navigate().GoToUrl(url);
}
[TestCleanup]
public void Cleanup()
{
//after the test
driver.Quit();
}
[TestMethod]
public void testtingsite()
{
//Here we write the actual test
}
}
}
I know it's not much, but the problem is when ever I go to test explorer >> run all then I get this:
------ Run test started ------
Cannot discover test extensions installed by NuGet. The NuGet service is not available. Tests may not be found due to missing test adapter extensions.
NUnit VS Adapter 2.0.0.0 executing tests is started
Loading tests from C:\Users\kkkkkk\Documents\Visual Studio 2015\Projects\testsite\testsite\bin\Debug\testsite.dll
Run started: C:\Users\kkkkkk\Documents\Visual Studio 2015\Projects\testsite\testsite\bin\Debug\testsite.dll
NUnit VS Adapter 2.0.0.0 executing tests is finished
========== Run test finished: 1 run (0:00:06.5556584) ==========
White it should open the chrome browse, visit the url and then quits chrome, which doesn't happen at all. I re-tested it again with the same result only this time it says in the test explorer window that the test has failed and that's it, when I click on the testsite below the failed test (still in the test explorer window) i get the following message :
Test Name: testtingsite
Test FullName: testsite.firsttest.testtingsite
Test Source: c:\users\kkkkkk\documents\visual studio 2015\Projects\testsite\testsite\UnitTest1.cs : line 34
Test Outcome: Failed
Test Duration: 0:00:00.1350487
Result StackTrace:
à OpenQA.Selenium.DriverService.FindDriverServiceExecutable(String executableName, Uri downloadUrl) dans c:\Projects\webdriver\dotnet\src\webdriver\DriverService.cs:ligne 243
à OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService() dans c:\Projects\webdriver\dotnet\src\webdriver\Chrome\ChromeDriverService.cs:ligne 146
à OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeOptions options) dans c:\Projects\webdriver\dotnet\src\webdriver\Chrome\ChromeDriver.cs:ligne 80
à OpenQA.Selenium.Chrome.ChromeDriver..ctor() dans c:\Projects\webdriver\dotnet\src\webdriver\Chrome\ChromeDriver.cs:ligne 71
à testsite.firsttest.setup() dans c:\users\kkkkkk\documents\visual studio 2015\Projects\testsite\testsite\UnitTest1.cs:ligne 19
Result Message: Initialization method testsite.firsttest.setup threw exception. OpenQA.Selenium.DriverServiceNotFoundException: OpenQA.Selenium.DriverServiceNotFoundException: The chromedriver.exe file does not exist in the current directory or in a directory on the PATH environment variable. The driver can be downloaded at http://chromedriver.storage.googleapis.com/index.html..
okay that's the message, any ideas on how to solve the issue I really need help );
modification_1
so I've modified the code (thanks to #Leon) I added the following string DRIVER_PATH = #"C:\Projects\webdriver\dotnet\src\webdriver\chrome\chromedriver"; to [TestClass] right below the string url = "http://..."; and also add DRIVER_PATH like this driver = new ChromeDriver(DRIVER_PATH); in the [TestInitialize] but it's still not running the chrome browser!!?
You need download chrome driver.
chrome driver link
string DRIVER_PATH = #"C:\...\chromedriver";
IWebDriver driver = new ChromeDriver(DRIVER_PATH);
driver.Navigate().GoToUrl(url);
so the problem was like #leon mentioned it in the chromedriver, but although I used his path method (described in modification_1) it didn't work, what actually did work and solved my problem (without changing the first code, before mofication_1) was the installation of the selenium chromedriver from visual studio 2015, I went to references >> right click >> go to manage Nuget package >> and then typed selenium chromedriver in the search bar and boom all I had to do was click on install, internet connection was required. if you can't see the nuget manage packages then you need to download and install it and then restart visual studio to see it.
Just add the driver reference as per your browser specification like chromedriver, edgedriver. Please note "not mention any driver path".
namespace UnitTestProject3
{
public class UnitTest1
{
IWebDriver wb;
String url = "http://www.yahoo.com";
[Fact]
public void TestMethod1()
{
wb = new ChromeDriver();
wb.Navigate().GoToUrl(url);
}
}
}

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