Selenium C# 当前上下文中不存在名称驱动程序。我将变量移至 TestFixture class
Selenium C# The name driver does not exist in the current context. I moved my variable to TestFixture class
Selenium C# 我将我的驱动程序变量从 HomePage.cs class 移动到 TestFixture.cs class 因为我希望在 TestFixture 的设置中实例化浏览器变量。
例如。
[SetUp]
public void FixtureSetup() {
driver1 = new FirefoxDriver();
driver1.Navigate().GoToUrl("http://localhost:8080/searchtest");
}
我收到错误:
The name driver 1 does not exist in the current context File: HomePage.cs
我认为它找不到变量 "driver1"
如果我想在其他 classes 中使用来自 TestFixture class 的这个变量,HomePage.cs 我想如果我将 var 定义为 public 另一个 classes应该能够访问变量。
解决这个问题的最佳方法是什么?
我应该把 "driver1" 变量放在 globals.cs class 中吗?
我的代码片段如下:
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.");
}
}
}
在我的 Python 代码中,我有如下设置,如何在 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"
我正在尝试遵循 C# 中的页面对象模型,因为我想使用 BDD Specflow 而不是 Python 中的 Behave。
我的例子步骤文件是:
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...
谢谢,
里亚兹
如果您正在使用 specflow,那么您不应该使用 NUnit 属性来控制测试生命周期(即 [Setup]
、[TearDown]
等,您应该使用 specflow 选项来控制测试生命周期,(即 [BeforeScenario]
、[BeforeFeature]
等)。混合使用它们只会导致难以追踪的问题。
为了回答您的问题,specflow 提供了多种在绑定之间共享数据的方法。阅读this for more details。
我对 this question 的回答中使用了有关如何使用上下文注入执行此操作的一些详细信息,这应该为您提供足够的详细信息,以便能够了解如何共享网络驱动程序。
Selenium C# 我将我的驱动程序变量从 HomePage.cs class 移动到 TestFixture.cs class 因为我希望在 TestFixture 的设置中实例化浏览器变量。
例如。
[SetUp]
public void FixtureSetup() {
driver1 = new FirefoxDriver();
driver1.Navigate().GoToUrl("http://localhost:8080/searchtest");
}
我收到错误:
The name driver 1 does not exist in the current context File: HomePage.cs
我认为它找不到变量 "driver1" 如果我想在其他 classes 中使用来自 TestFixture class 的这个变量,HomePage.cs 我想如果我将 var 定义为 public 另一个 classes应该能够访问变量。
解决这个问题的最佳方法是什么? 我应该把 "driver1" 变量放在 globals.cs class 中吗?
我的代码片段如下:
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.");
}
}
}
在我的 Python 代码中,我有如下设置,如何在 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"
我正在尝试遵循 C# 中的页面对象模型,因为我想使用 BDD Specflow 而不是 Python 中的 Behave。
我的例子步骤文件是:
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...
谢谢, 里亚兹
如果您正在使用 specflow,那么您不应该使用 NUnit 属性来控制测试生命周期(即 [Setup]
、[TearDown]
等,您应该使用 specflow 选项来控制测试生命周期,(即 [BeforeScenario]
、[BeforeFeature]
等)。混合使用它们只会导致难以追踪的问题。
为了回答您的问题,specflow 提供了多种在绑定之间共享数据的方法。阅读this for more details。
我对 this question 的回答中使用了有关如何使用上下文注入执行此操作的一些详细信息,这应该为您提供足够的详细信息,以便能够了解如何共享网络驱动程序。