Selenium C# I moved my driver variable from HomePage.cs class to the TestFixture.cs class because I want the browser variable to be instantiated in the setup of TestFixture.
E.g.
[SetUp]
public void FixtureSetup() {
driver1 = new FirefoxDriver();
driver1.Navigate().GoToUrl("http://localhost:8080/searchtest");
}
I am getting the error:
The name driver 1 does not exist in the current context File: HomePage.cs
I think it cannot find the variable "driver1" If i want to use this variable from the TestFixture class in other classes, HomePage.cs I thought if i define the var as public the other classes should be able to access the variable.
What is the best way I can solve this? Should i put the "driver1" variable in a globals.cs class?
My code snippet is as follows:
class TestFixture.cs
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Firefox;
using SearchTest.Setup;
using earchTest.PageObjects;
namespace GoogleSearchTest.Setup
{
[SetUpFixture]
//public class TestConfiguration : SeleniumDriver
public class TestConfiguration
{
public IWebDriver driver1;
[SetUp]
public void FixtureSetup()
{
driver1 = new FirefoxDriver();
driver1.Navigate().GoToUrl("http://localhost:8080/searchtest");
}
[TearDown]
public void FixtureTearDown()
{
//if (WebDriver != null) WebDriver.Quit();
}
}
}
Class HomePage.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;
using SearchTest.Setup;
namespace SearchTest.PageObjects
{
class HomePage : PageObjectBase
{
//private IWebDriver driver{ get; set; }
//private IWebDriver driver1 { get; set; }
[FindsBy(How = How.XPath, Using = ".//TITLE")]
public IWebElement Title{ get; set; }
// search text field on the homepage
//[FindsBy(How= How.Id, Using="twotabsearchtextbox")]
//private IWebElement Searchfield_ID { get; set; }
[FindsBy(How = How.XPath, Using = ".//*[@id='twotabsearchtextbox']")]
private IWebElement Searchfield_XPATH { get; set; }
[FindsBy(How = How.Id, Using = "nav-search-submit-text")]
private IWebElement SearchButton { get; set; }
[FindsBy(How = How.XPath, Using = ".//*[@id='nav-search']/form/div[2]/div/input")]
private IWebElement searchButton_Xpath {get; set;}
public HomePage() : base("Title - Search Test")
{
//driver1 = new FirefoxDriver();
//Console.Out.WriteLine("from Homepage Constructor Driver.title in SearchResultsPage class = " + driver.Title);
//driver1.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Set implicit wait timeouts to 5 secs
PageFactory.InitElements(driver1, this);
}
public void goToURL() {
//driver = new FirefoxDriver();
//driver1.Navigate().GoToUrl("http://localhost:8080/searchtest");
}
public void EnterSearchText(String text) {
Searchfield_XPATH.SendKeys(text);
}
public SearchResultsPage click_search_button() {
searchButton_Xpath.Click();
return new SearchResultsPage(driver1);
}
}
}
Class PageObjectBase.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
/* The PageObjectBase class. This class is to represent anything that applies to all pages
* of the site to be tested. The example here shows that you can pass in by default a
* Title from the driver to ensure that the correct page is loaded, but the intention * of this is to provide a base class so that codereplication is kept to a minimum
*/
namespace SearchTest.PageObjects
{
class PageObjectBase
{
private IWebDriver Driver { get; set; }
//public PageObjectBase(IWebDriver driver,String titleOfPage)
public PageObjectBase(String titleOfPage)
{
//Driver = driver;
Driver = new FirefoxDriver();
Console.Out.WriteLine("From base class driver.title = " + Driver.Title);
//if (Driver.Title != titleOfPage)
// throw new NoSuchWindowException("PageObjectBase: The Page Title doesnt match.");
}
}
}
In my Python code i have the SetUp as follows, how to do it in C#?:
BaseTestCase.py
import unittest
from selenium import webdriver
from Locators import Globals
from Pages import login
import time
import os
class BaseTestCase(unittest.TestCase):
@classmethod
def setUpClass(cls):
webdriver.DesiredCapabilities.INTERNETEXPLORER["unexpectedAlertBehaviour"] = "accept"
cls.driver = webdriver.Ie(Globals.IEdriver_path)
cls.driver.get(Globals.URL_riaz_pc)
cls.login_page = login.LoginPage(cls.driver)
cls.driver.implicitly_wait(120)
cls.driver.maximize_window()
@classmethod
def tearDownClass(cls):
time.sleep(5)
cls.login_page.click_logout()
cls.driver.close()
# kill the IEDriverServer process because it stays left open when test finishes. Multiple instances will remain otherwise every time a test runs.
print "Kill process IEDriverServer.exe"
os.system('taskkill /f /im IEDriverServer.exe')
Globals.py
IEdriver_path = "C:\Webdriver\IEDriverServer\IEDriverServer.exe"
URL_riaz_pc = "http://riaz-pc.company.local:8080/clearcore"
URL_test1 = "http://test1:8080/clearcore"
I am trying to follow a page object model in C# as I want to use BDD Specflow instead of Behave in Python.
My E.g. Steps file is:
using System;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;
using SearchTest.PageObjects;
namespace SearchTest
{
[Binding]
public class SearchSteps
{
private IWebDriver driver { get; set; }
PageObjects.HomePage home_page { get; set; }
private SearchResultsPage search_results_page;
[Given(@"I navigate to the page ""(.*)""")]
public void GivenINavigateToThePage(string p0)
{
//driver = new FirefoxDriver();
//driver.Navigate().GoToUrl("http://localhost:8080/searchtest");
home_page = new PageObjects.HomePage();
//home_page.goToURL();
}
etc...
Thanks, Riaz
if you are using specflow then you shouldn't be using the NUnit attributes to control test ligfecycle ( ie [Setup]
, [TearDown]
etc, you should instead use the specflow options for controlling test lifecycle, (ie [BeforeScenario]
, [BeforeFeature]
etc). Mixing them can just cause issues which are awkward to track down.
To answer your question though specflow provides a number of ways to share data between bindings. Read this for more details.
Some details of how you can do this using context injection are used in my answer to this question and this should give you enough details to be able to see how to share the web driver.