Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

selenium - SpecFlow + Selenium-WebDriver:是否跨步骤共享WebDriver?(SpecFlow + Selenium-WebDriver: Sharing a WebDriver Across Steps?)

Say I have a scenario that uses steps that are contained in two different classes.

(假设我有一个使用两个不同类中包含的步骤的方案。)

Is there a way for both of them to have a handle to the same IWebDriver instance without going through ScenarioContext.Current["webdriverVariableName"] ?

(有没有办法让他们两个都拥有相同的IWebDriver实例的句柄而无需通过ScenarioContext.Current["webdriverVariableName"] ?)

That's so ugly.

(太丑了)

Is there a cleaner way?

(有没有更清洁的方法?)

I was going to make a class with a public static IWebDriver property that gets assigned at the start of every scenario, so that all my steps could refer to it, but I don't think that will work when I start to run them in parallel, as each scenario would overwrite the global driver.

(我打算创建一个带有public static IWebDriver属性的类, public static IWebDriver将在每种情况的开始时分配,以便我的所有步骤都可以引用它,但是当我开始并行运行它们时,我认为这种方法不起作用,因为每种情况都会覆盖全局驱动程序。)

  ask by user1738509 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Specflow offers a Dependency Injection mecanism, so you could get your web driver instance injected in your steps.

(Specflow提供了依赖注入机制,因此您可以在步骤中注入Web驱动程序实例。)

See https://github.com/techtalk/SpecFlow/wiki/Context-Injection

(参见https://github.com/techtalk/SpecFlow/wiki/Context-Injection)

See the "Avanced options" section.

(请参阅“高级选项”部分。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...