BDD规范流对象引用未设置为ObjectContainer.RegisterInstanceAs中对象的实例
本文关键字:RegisterInstanceAs 对象 实例 ObjectContainer 范流 对象引用 设置 BDD | 更新日期: 2023-09-27 18:27:32
我已经重新排列了我的类文件和Webdriver变量,这样我就可以使用上下文注入在步骤之间共享Webdrive变量。在我使用SetupFixture、Setup和TearDown之前,这对BDD来说是不正确的。现在尝试使用BeforeAllTests、BeforeTestRun等进行上下文注入。我得到错误:
System.NullReferenceException: Object reference not set to an instance of an object
错误所在的行突出显示:
[BeforeScenario]
public void RunBeforeScenario()
{
objectContainer.RegisterInstanceAs<SeleniumContext>(seleniumContext);
}
我的setup.cs实现是:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using OpenQA.Selenium.Firefox;
using SearchTest.Setup;
using BoDi;
using SearchTest.WebDriver;
namespace SearchTest.Setup
{
[Binding]
public class BeforeAllTests
{
private readonly IObjectContainer objectContainer;
private static SeleniumContext seleniumContext;
public BeforeAllTests(IObjectContainer container)
{
this.objectContainer = objectContainer;
}
[BeforeTestRun]
public static void RunBeforeAllTests()
{
seleniumContext = new SeleniumContext();
}
[BeforeScenario]
public void RunBeforeScenario()
{
objectContainer.RegisterInstanceAs<SeleniumContext>(seleniumContext);
}
}
}
我的主页类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;
using SearchTest.WebDriver;
using TechTalk.SpecFlow;
using BoDi;
namespace SearchTest.PageObjects
{
[Binding]
public class HomePage : PageObjectBase
{
private SeleniumContext seleniumContext;
//private IWebDriver driver{ 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(IWebDriver driver)
public HomePage(SeleniumContext seleniumContext)
/*This is to reference the PageObjectBase Class. Passing in the Title of the
* page that is expected for the HomePage to ensure the correct Page is loaded
* before starting any tests.
*/
//: base("Amazon.co.uk: Low Prices in Electronics, Books, Sports Equipment & more")
: base(seleniumContext)
{
//driver = new FirefoxDriver();
//Console.Out.WriteLine("from Homepage Constructor Driver.title in SearchResultsPage class = " + driver.Title);
//driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(5)); // Set implicit wait timeouts to 5 secs
//PageFactory.InitElements(driver, this);
this.seleniumContext = seleniumContext;
PageFactory.InitElements(seleniumContext.driver, this);
}
public void goToURL() {
//driver.Navigate().GoToUrl("http://www.amazon.co.uk");
}
public void EnterSearchText(String text)
{
Searchfield_XPATH.SendKeys(text);
}
public SearchResultsPage click_search_button() {
searchButton_Xpath.Click();
return new SearchResultsPage(seleniumContext);
}
}
}
Steps类SearchSteps.cs的实现是:
using System;
using TechTalk.SpecFlow;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;
using SearchTest.PageObjects;
using SearchTest.WebDriver;
namespace SearchTest
{
[Binding]
public class SearchSteps
{
private SeleniumContext seleniumContext;
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)
home_page = new PageObjects.HomePage(seleniumContext);
//home_page.goToURL();
}
[Given(@"I see the page is loaded")]
public void GivenISeeThePageIsLoaded()
{
//Assert.AreEqual("http://localhost:8080: PS4 products", driver.Title);
}
[When(@"I enter Search Keyword in the Search Text box")]
public void WhenIEnterSearchKeywordInTheSearchTextBox(Table table)
{
//string search_text = table.Rows[0]["Keyword"].ToString();
//driver.FindElement(By.Id("twotabsearchtextbox")).SendKeys(search_text);
//SearchResultsPage SearchResultsPage = home_page.EnterSearchText("F1");
home_page.EnterSearchText("F1");
}
[When(@"I click on Search Button")]
public void WhenIClickOnSearchButton()
{
//driver.FindElement(By.Name("BtnG")).Click();
search_results_page = home_page.click_search_button();
}
[Then(@"Search items shows the items related to PS4")]
public void ThenSearchItemsShowsTheItemsRelatedToPS4()
{
//Assert.AreEqual("PS4", driver.FindElement(By.XPath(".//*[@id='desktop-auto-sparkle-multi']/div/a")).Text);
//Assert.AreEqual("PS4", driver.FindElement(By.XPath("//h2[contains(text(), "PS4")]")));
search_results_page.get_search_result_title();
}
}
}
我该如何解决这个问题?我认为这是在抱怨objectContainer.RegisterInstanceAs没有任何值,它是Null。它还没有实例化?
谢谢,Riaz
您的问题是构造函数中有一个拼写错误。此:
public BeforeAllTests(IObjectContainer container)
{
this.objectContainer = objectContainer;
}
应该是
public BeforeAllTests(IObjectContainer container)
{
this.objectContainer = container;
}
您没有使用构造函数中给定的container
实例,只是将objectContainer
设置为自身。