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.
Related
I am using MSTest project to automate my system. The test project work fine. But I need to run the test project in my production with out installing visual studio.
So that I am using VSTest.Console.exe command-line.
My Login method as below
[TestMethod]
[TestCategory("SmokeTests")]
public void Can_login_with_Empty_UserName()
{
Login(TestContext.Properties["UserName"].ToString(), TestContext.Properties["Password"].ToString());
}
and my vstest.console command as below
vstest.console MyPath\SmokeTests.dll /Tests:Login /Settings: PathToSettingFile\tests.runsettings
when I run this command it will give me
"System.NullReferenceException: Object reference not set to an instance of an object." Error
But if I change my method like below
[TestMethod]
[TestCategory("SmokeTests")]
public void Can_login_with_Empty_UserName()
{
Login("Admin", "1234");
}
Above code will work properly.
So the problem is
TestContext.Properties["UserName"].ToString()
When I run the test case through Visual Studio no issues.
Any idea why /Settings: PathToSettingFile\tests.runsettings cannot load runsettings file running through the vstest.console command.
Thank you.
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 am using the following code to try to programmatically create Visual Studio 2015 projects on the fly from within an existing Console Application. Why you ask? We are putting together a library of all our code snippets and we want a test/proof test solution for each one, and since we have hundreds of these, we aren't going to do it manually.
Ultimately this will be in an MVC5 app as its own project (class library?) but for now we are just trying to get it functional within this console application.
I am trying to create a new solution with 2 projects (a console application and a unit test project).I am also not even sure I need the unit test project, or whether one even works with a console app since there is no option to add a unit test to a console app in VS2015 solution generation.
Here is the code;
public class CreateConsoleProjectsProgrammatically
{
private const string Vs2015Type = "VisualStudio.DTE.14.0";
private const string Vs2015ConsoleProject = #"X:\Code Library\Helpers\ConsoleApplication\csConsoleApplication.vstemplate";
private const string Vs2015UnitTestProject = #"X:\Code Library\Helpers\UnitTestProject\UnitTestProject.vstemplate";
private const string Vs2015CodeLibraryBasepath = #"X:\Code Library";
public static void CreateVsConsoleProjectProgrammatically(string filename)
{
// Create a solution with two projects in it, based on project
// templates, a console project and a unit test project.
var vsType = Type.GetTypeFromProgID(Vs2015Type);
//error line is below
var vsInstance= Activator.CreateInstance(vsType, true);
EnvDTE.DTE dte = (EnvDTE.DTE)vsInstance;
dte.MainWindow.Visible = false; // optional: set to true if you want to see VS doing its thing
// create a new solution
dte.Solution.Create(#"X:\Code Library\", filename);
var solution = dte.Solution;
// create a C# console application
solution.AddFromTemplate(Vs2015ConsoleProject,Path.Combine(Vs2015CodeLibraryBasepath,filename), filename);
// create a unit test project
solution.AddFromTemplate(Vs2015UnitTestProject, Path.Combine(Vs2015CodeLibraryBasepath, filename + "_Test"), filename + "_Test");
// save and quit
dte.ExecuteCommand("File.SaveAll");
dte.Quit();
}
}
Here is the error which is from the Activator.CreateInstance
Creating an instance of the COM component with CLSID {A2FA2136-EB44-4D10-A1D3-6FE1D63A7C05} from the IClassFactory failed due to the following error: 8001010a The message filter indicated that the application is busy. (Exception from HRESULT: 0x8001010A (RPC_E_SERVERCALL_RETRYLATER)).
Not sure why I am getting a server busy error. It almost seems like its because VS is already open? But how else would the new solution be generated? I tried closing the solution and reopening it but that did nothing to solve the issue.
I also thought it may have been a connectivity issue, so I moved all the files and directories to C:\ but it produced the same error, so it wasn't an issue of using a networked drive location.
I also tried using
EnvDTE80.DTE2 dte2 = (EnvDTE100.DTE2)vsInstance;
but DTE2 is not an available property/method on EnvDTE100 according to the editor, even though I found some examples using it on the net.
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());
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);
}
}
}