I have a question about referencing exe files for multiple browser testing in selenium NUnit with C#. I have added the additional code to get my tests to run in each browser but each time I run the tests, I get the error : OpenQA.Selenium.DriverServiceNotFoundException. My question is, is there anyway to add the reference without specifically laying out a path? I don't think that I would be able to add a path to the current code I have, without refactoring what I have. Thanks for your help in advance.
Test Fixture
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(ChromeDriver))]
public class CustomerLogin<TWebDriver> where TWebDriver : IWebDriver, new()
{
private IWebDriver driver;
private string url;
[TestFixtureSetUp]
public void FixtureSetUp()
{
url = System.Configuration.ConfigurationManager.AppSettings["homeUrl"];
this.driver = new TWebDriver();
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
}
Using statements
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
I believe you can set the path with something like this;
System.setProperty("webdriver.ie.driver", "C:\my\path\to\IEDriverServer.exe");
System.setProperty("webdriver.firefox.driber", "C:\my\path\to\FFDriver.exe");
Or, the better solution in my opinion is to add the drivers to your project and modify the build so that they end up in your drop folder. The code above is great for your local system but isn't portable. Most automation solutions are intended to 'work out of the box' which requires you to package your dependencies in the bin.
To package the drivers with your build; add a 'Drivers' folder to the project (right click the solution -> add -> new folder) then add the executables under there (right click the folder, add existing item). Now that you can see the driver exe in Solution Explorer right click and select properties, under Copy to Output Directory select 'Copy if Newer'.
After making some changed to the code I am able to run all tests on the big 3 browsers. I had to add the Chrome.exe and the IE.exe to the bin\Debug folder (The one with the WebDriver.dll's) Here are my changes to my code.
Test Fixtures
[TestFixture(typeof(FirefoxDriver))]
[TestFixture(typeof(InternetExplorerDriver))]
[TestFixture(typeof(ChromeDriver))]
public class CustomerLogin<TWebDriver> where TWebDriver : IWebDriver, new()
{
private IWebDriver driver;
private string url;
[SetUp]
public void SetUp()
{
this.driver = new TWebDriver();
url = System.Configuration.ConfigurationManager.AppSettings["homeUrl"];
driver.Navigate().GoToUrl(url);
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5));
//Finding the customer login link and clicking it
driver.FindElement(By.Id("Homepage_r2_c14")).Click();
}
[TearDown]
public void TearDown()
{
driver.Quit();
}
}
Related
I am using vs 2019 Community and have created a Class Library (.NET Framework) project named TestDLL.
The project contains a single file "TestDLL.cs" which has TestDLL as a namespace, a public class named MyDll and a single method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDLL
{
public class MyDLL
{
static bool Opposite(bool input)
{
return !input;
}
}
}
To the solution I added Unit Test Project (.NET Framework) named UnitTest, added TinyLib_dll.dll as a reference, and created the very simple test.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
bool start = true;
bool result = MyDLL.Opposite(start);
Assert.AreNotEqual(start, result, "MyDll.Opposite failed");
}
}
}
When I try to run the test, the dll file compiles successfully, but the UnitTest fails with "Error CS0103 The name 'MyDLL' does not exist in the current context UnitTest".
It appears that although I have referenced the dll as a reference it is not being loaded into the test project.
Can someone help me out?
You appear to be missing a TestDLL namespace in the test in order for the test to recognize the subject class
either add a using namespace
using TestDLL;
//...
or use the full name of the type
//...
bool result = TestDLL.MyDLL.Opposite(start);
//...
In my haste to resolve my problem i submitted code samples with simple compilation errors. Correcting these errors resolved my stated problem. However I now have a new issue with the same project, but for clarity I will submit a new question. I offer thanks to Nkosi for his assistance.
I have a Selenium framework that has been smooth sailing ever since it's creation. I implemented the Visual Studio Team Explorer today and after pulling from my remote branch, Intellisense started yelling at me, saying that one of my namespaces does not exist. The lines that it doesn't like are
using PageObjectModel.PageObjects.Maintenance;
and
var SettlementAccountReconciliations = new SettlementAccountReconciliation(_driver);
The SettlementAccountReconciliation is found within the Maintenance directory. Here is my full code for the test class where it does not like the directory:
using NLog.Internal;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using PageObjectModel.ControlObjects;
using PageObjectModel.PageObjects;
using PageObjectModel.PageObjects.Process;
using PageObjectModel.PageObjects.Status;
using System;
using PageObjectModel.PageObjects.Maintenance; // **LINE WITH MAINTENANCE UNDERLINED**
namespace PageObjectModel.Tests.TaskTesting
{
[TestFixture]
class JiraTaskTesting
{
private IWebDriver _driver;
[OneTimeSetUp]
public void SetUp()
{
_driver = new ChromeDriver();
DriverContext.Driver = _driver;
_driver.Manage().Window.Maximize();
var ConfigManager = new ConfigurationManager();
var Login = new Login(_driver);
Login.SignIn(ConfigManager.AppSettings["Username"], ConfigManager.AppSettings["Password"]);
}
[Test]
public void ReportNameStandardizationSettlementAccountReconciliations()
{
// **LINE WITH UNDERLINE**
var SettlementAccountReconciliations = new SettlementAccountReconciliation(_driver);
var Utilities = new Utilities(_driver);
var ReportPopup = SettlementAccountReconciliations.ExportReconciliationReport();
ReportPopup.StartDate.SetValue(DateTime.Now.ToString("MM/dd/yyyy"));
ReportPopup.EndDate.SetValue(DateTime.Now.ToString("MM/dd/yyyy"));
ReportPopup.ExportButton.Click();
Assert.IsTrue(Utilities.ValidateFilePresent("Settlement Account Reconciliation"));
Utilities.DeleteFile("Settlement Account Reconciliation");
}
Also, here is the SettlementAccountReconciliation found within the Maintenance namespace:
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Support.UI;
using System;
namespace PageObjectModel.PageObjects.Maintenance
{
public class SettlementAccountReconciliation
{
private readonly IWebDriver _driver;
public SettlementAccountReconciliation(IWebDriver driver)
{
_driver = driver;
PageFactory.InitElements(driver, this);
var Utilities = new Utilities(driver);
string TradingUrl = Utilities.RefactorUrl("/SettlementAccountReconciliation");
driver.Navigate().GoToUrl(TradingUrl);
WebDriverWait Wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
try
{
Wait.Until(ExpectedConditions.ElementExists(By.XPath("//a[text()='Settlement Account Reconciliations']")));
Console.WriteLine("SettlementAccountReconciliation Page Label Found");
}
catch
{
Console.WriteLine("SettlementAccountReconciliation Page Label Not Found, timing out");
}
}
This is an image of what is shown in my test class for the Directives:
And for the SettlementAccountReconciliation constructor:
I have all of my code in the Page Object Model format, and this is how my file structure is lined out:
I know this question is long winded but to continue, the tests run just fine, and the solution builds like a charm. I just need to figure out how to get the text editor to not think there are issues with my code.
Visual Studio tells me that "The type or namespace name 'Maintenance' does not exist in the namespace PageObjectModel.PageObjects'", but it does.
Any help on this would be greatly appreciated.
EDIT:
I fixed it.
I don't why it worked, but it did.
All I did was renamed the Maintenance folder to Maintenance1, recreated the Maintenance folder, and dragged and dropped the SettlementAccountReconciliation class into the Maintenance folder.
I am guessing there was some random property or setting stored in a temp folder somewhere in Timbuktu that was stored for the existing folder that was reset or deleted when the new folder was created.
Thanks to everyone who put time into helping me out!
I've had this issue before myself. Check the file and folder names in your project directory, the namespaces and assembly name within the properties of your project, and check that, if you've added the project as a reference, that the path to the reference is consistent with the actual path. I have a strong feeling that there is some inconsistency somewhere if you've refactored this at any point and the code you think you are referencing isn't actually what is being referenced.
I renamed the Maintenance folder to Maintenance1, recreated the Maintenance folder, and dragged and dropped the SettlementAccountReconciliation class into the Maintenance folder, and it seems to have fixed the issue.
I am guessing there was some random property or setting stored in a temp folder somewhere in Timbuktu that was stored for the existing folder that was reset or deleted when the new folder was created.
Looking for a solution to invoke Navigation in Chrome from outside of chrome by external process.
We have legacy WinForm Software that needs to browse an Angular HTML5 app that requires Chrome to run. (I have no control of this.)
Something along lines of:
Process process = new Process();
process.StartInfo.FileName =
#"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
process.StartInfo.Arguments = "google.com" + " --new-window";
process.Start();
And then some way to hook into that process and have the SAME TAB perform navigation.
magic.navigateTab1(www.anothersite.com);
I quite assume its not easily possible, solution may not use Process etc.. Just, anyway to accomplish it from outside chrome?
Whatever solution can't require installing > ~5MB to C# code base.. Ideally, no installation is preferred.
http://cefsharp.github.io/ <- looked into this but this is huge.
Edit 1: Perhaps using something like this..
https://sites.google.com/a/chromium.org/chromedriver/
I used Selenium to achieve that.
First you must add via Nugget the following packages to your solution
Selenium.WebDriver
Selenium.Chrome.WebDriver;
Then reference the following namespaces
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
Create the following property on your class root
IWebDriver driver;
On your class constructor add the following code to create a new Chrome Window handled by Selenium
driver = new ChromeDriver();
Then on your buttons add the following
// Switch the action target to the first tab opened on chrome instace handled by Selenium
driver.SwitchTo().Window(driver.WindowHandles.First());
// Go to a given URL
driver.Navigate().GoToUrl("http://www.yourURL.com.br");
Your code should be like that
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
namespace configure
{
public partial class Form1 : Form
{
IWebDriver driver;
public Form1()
{
InitializeComponent();
driver = new ChromeDriver();
}
private void button1_Click(object sender, EventArgs e)
{
driver.SwitchTo().Window(driver.WindowHandles.First());
driver.Navigate().GoToUrl("http://www.yourURL.com.br");
}
}
}
Hope this solve your problem
I'm trying to run some unit tests using Selenium Webdriver and C#.Net for Firefox browser but I was unable to do it (Chrome and IE11 browsers are working smoothly).
Here is the info I gathered:
OS: Windows 10 Enterprise
Gecko driver version: geckodriver-v0.17.0-win32
Mozilla Firefox version: 54.0.1 (32-bit)
Environment Path variable already set under "C:\LEO\SELENIUM C#\Firefox"
Piece of Code:
using System;
using System.Text;
using System.Threading;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Firefox;
[TestFixture]
public class UnitTest3
{
private IWebDriver driver;
private StringBuilder verificationErrors;
private string baseURL;
[SetUp]
public void SetupTest()
{
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\LEO\SELENIUM C#\Firefox");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
driver = new FirefoxDriver(service);
baseURL = "http://www.dow.com";
verificationErrors = new StringBuilder();
}
}
Error:
Message: OpenQA.Selenium.WebDriverException : Unable to find a matching set of capabilities.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.
StackTrace:
I will appreciate all your help, thanks.
First of all, you need Firefox: Developer Edition installed and updated.
That`s should be enough in your case.
Source with full guide.
I'm working on Automation framework using WebDriver with C#. Its working fine with Firefox but not with IE.
I am getting the following error:
IEDriverServer.exe does not exist-The file c:\users\administrator\documents\visual studio 2010\projects\TestProject1\TestProject1\bin\Debug\IEDriverServer.exe does not exist. The driver can be downloaded at http://code.google.com/p/selenium/downloads/list
I am using IE 9 and Windows 7.
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.co.uk");
IWebElement queryBox = driver.FindElement(By.Name("q"));
queryBox.SendKeys("The Automated Tester");
queryBox.SendKeys(Keys.ArrowDown);
queryBox.Submit();
See also .
The IEDriverServer.exe (as well as ChromeDriver.exe) can be downloaded from:
http://selenium-release.storage.googleapis.com/index.html.
To get these to work with your Selenium tests, include the .exe in your test project, and set its properties to 'Copy Always'.
NOTE: You'll have to adjust the Add File dialog to display .exe files.
Doing this will resolve the error.
Here's a simple C# example of how to call the InternetExplorerDriver using the IEDriverServer.exe.
Refactor according to your needs.
Note: the use of driver.Quit() which ensures that the IEDriverServer.exe process is closed, after the test has finished.
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium.IE;
namespace SeleniumTest
{
[TestClass]
public class IEDriverTest
{
private const string URL = "http://url";
private const string IE_DRIVER_PATH = #"C:\PathTo\IEDriverServer.exe";
[TestMethod]
public void Test()
{
var options = new InternetExplorerOptions()
{
InitialBrowserUrl = URL,
IntroduceInstabilityByIgnoringProtectedModeSettings = true
};
var driver = new InternetExplorerDriver(IE_DRIVER_PATH, options);
driver.Navigate();
driver.Close(); // closes browser
driver.Quit(); // closes IEDriverServer process
}
}
}
Per Jim Evans (who works on IEDriverServer)
The .NET bindings don't scan the %PATH% environment variable for the
executable. That means for the .NET bindings only, the
IEDriverServer.exe is expected to either be found in the same
directory as the .NET bindings assembly, or you must specify the
directory where it can be found in the constructor to the
InternetExplorerDriver class.
Failure to do one of these things (or to
set the UseInternalServer property in the InternetExplorerOptions
class) will cause the .NET IE driver implementation to throw an
exception. This is strictly by design, as we want people to begin
using the standalone IEDriverServer.exe, and the ability to use an
"internal" or "legacy" version of the server will be removed in a
future release.
https://groups.google.com/forum/?fromgroups#!topic/webdriver/EvTyEPYchxE
If you're working with Visual Studio and C# I've updated my NareshScaler nuget package to install IEDriverServer, ChromeDriver etc automatically, meaning you can get up and running quicker.
http://nuget.org/packages/NareshScaler
Code for WebDriver using java to run with IE. I believe this concept might be helpful for you using C#:
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
File file = new File("C:\\Program Files\\Internet Explorer\\iexplore.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
driver = new InternetExplorerDriver(capabilities);
If above code doesn't work use the following instead of "File file = new File("C:\Program Files\Internet Explorer\iexplore.exe");":
File file = new File("F:\\Ripon\\IEDriverServer_Win32_2.25.2\\IEDriverServer.exe");
[Note: The version of IEDriverServer and Windows (32 or 64 bit) may vary individual to individual]
Give path only till folder where Internetexplorer.exe is located.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System.IO;
namespace Automation
{
class To_Run_IE
{
static void Main(string[] args)
{
//Keep Internetexplorer.exe in "D:\Automation\32\Internetexplorer.exe"
IWebDriver driver = new InternetExplorerDriver(#"D:\Automation\32\"); \\Give path till the exe folder
//IWebDriver driver = new Firefoxdriver()
driver.Navigate().GoToUrl("http://www.google.com/");
driver.Manage().Window.Maximize();
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Cheese");
query.Submit();
System.Console.WriteLine("Page title is: " + driver.Title);
driver.Quit();
}
} }
public IWebDriver IEWebDriver()
{
var options = new InternetExplorerOptions();
options.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
webDriver = new InternetExplorerDriver(ConfigurationSettings.AppSettings["IDEServerPath"].ToString(), options);//Path of ur IE WebDriver,Here I stored it in a AppConfig File
return webDriver;
}