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);
}
}
}
Related
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/");
I have just started learning Appium testing using C#. I made simple program using Visual Studio to just open app from Emulator.
When I run my code I am getting this error
Message: System.UriFormatException : Invalid URI: The Authority/Host
could not be parsed. TearDown : System.NullReferenceException : Object
reference not set to an instance of an object.
This is my code
public class UnitTest1
{
private AndroidDriver<AndroidElement> driver;
private DesiredCapabilities capabilities;
[SetUp]
public void InItDriver()
{
capabilities = new DesiredCapabilities();
capabilities.SetCapability("PlatformName", "Android");
capabilities.SetCapability("deviceName", "Pixel_API_27:5554");
capabilities.SetCapability("appPackage", "com.sisapp.in.tulip");
capabilities.SetCapability("appActivity", "SplashActivity");
driver = new AndroidDriver<AndroidElement>(new Uri("https:127.0.01:4723/wd/hub"), capabilities);
}
[Test]
public void Test1()
{
Assert.IsNotNull(driver);
System.Threading.Thread.Sleep(2000);
}
[TearDown]
public void CloseTest()
{
driver.Quit();
}
}
Note: I did not installed anything like Appium Server in my machine.
These packages I have installed in my project. How can I solve this issue?
You must install and start Appium server in your pc. Then you will be able to run appium test.
Steps to install appium server
Download and install nodejs from here
install appium server from cmd using:
npm install -g appium
To start appium server:
appium -a 127.0.0.1 -p 4723 --session-override
In your code code instead of Uri try to use URL. Also change your url as follow:
driver = new AndroidDriver(new URL("https://127.0.0.1:4723/wd/hub"), capabilities);
Edit: Above issue is going to solve by this workaround regardless what other issues you are getting :).
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?!
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.
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());