How can I share one instance of Selenium Web Selenium in NUnit and C #?

I need an easy way to run an instance of Selenium web selenium and run various tests on it. I am trying to do this in a Suite file, but this will not work. The instance is killed instantly. Are there any alternatives in how to do this?

Potentially, I want to add more drivers (IE, Chrome) to this set and, if possible, run separately. Any suggestions are welcome.

namespace NUnit.Tests { public class AllTests { private static IWebDriver _Driver; [TestFixtureSetUp] public void SuiteSetUp() { _Driver = new FirefoxDriver(); } [TestFixtureTearDown] public void SuiteTearDown() { try { _Driver.Quit(); } catch (Exception) { // Ignore errors if unable to close the browser } } [Suite] public static TestSuite Suite { get { LoginTest lt = new LoginTest { Driver=_Driver }; suite.Add(lt); AnotherTest at = new AnotherTest { Driver=_Driver }; suite.Add(at); return suite; } } } } 
+4
source share
3 answers

An attempt to run this using the base class / extended classes failed. Because the webdriver instance did not receive initialization properly and could not be killed properly. Instead, I created the SetupIE (), SetupChrome (), SetupFirefox () methods in Suite, and also created the teardown method, which will work as the last test for the package.

Here is the code:

 namespace TestNamespace { using System; using NUnit.Framework; using NUnit.Core; using SeleniumTests; using OpenQA.Selenium; using OpenQA.Selenium.IE; using OpenQA.Selenium.Firefox; using OpenQA.Selenium.Chrome; using OpenQA.Selenium.Support.UI; class AllTests { public static IWebDriver WebDriver { get; private set; } [Suite] public static TestSuite Suite { get { TestSuite suite = new TestSuite("All Tests"); //Setup a Web driver (see methods below for different browsers) - SetupIE(), SetupChrome(), SetupFirefox() SetupIE(); // Add tests to suite suite.Add(new FlashLoadedTest { Driver = WebDriver }); // Tear down a Web driver suite.Add(new TearDownTest { DriverToTearDown = WebDriver }); // return suite to NUnit return suite; } } // Method that initialises FireFox Driver private static void SetupFireFox() { WebDriver = new FirefoxDriver(); } // Method that initialises IE Driver private static void SetupIE() { WebDriver = new InternetExplorerDriver(); } // Can't get this working, but this is how its supposed to work private static void SetupChrome() { WebDriver = new ChromeDriver(@"C:\Users\<user>\AppData\Local\Google\Chrome\Application"); } // Class with a test that tears down browser instance [TestFixture] class TearDownTest { public IWebDriver DriverToTearDown; [Test] public void TearDownBrowser() { if (DriverToTearDown == null) Assert.Fail("No Browser to Tear Down"); try { DriverToTearDown.Close(); DriverToTearDown.Dispose(); } catch { Assert.Fail("Browser failed to tear down"); } } } } } 
+2
source

I did this in Java, I created a base class, declared webdriver as static, put my startup / configuration methods in this class, and then extended it for every class class that I made.

I am sure it is the same for C #.

+2
source

I understand that this is a little late, but may be useful for future readers.

I created a base class containing a firefox driver with the following and it works fine for me. Then you can just refer to the base class (Driver in this instance) from your derived test class. It is worth noting that I use C # and Nunit.

Code for base class:

 namespace yournamespace { public class Driver { public IWebDriver driver; public StringBuilder verificationErrors; public Driver() { driver = new FirefoxDriver(); //replace with required driver verificationErrors = new StringBuilder(); } } } 

Then the Driver class from my test class is simply called:

 [TestFixture] public class IMSLogin : Driver { //.. all the usual bits and bobs! 
+2
source

Source: https://habr.com/ru/post/1389083/


All Articles