Selenium static nunit hooks - c#

I'm having trivial question but what is the main different between static and non static driver/ NUnit hooks?
I'm having a code:
[TestClass]
public class SectionsTests
{
private static Driver _driver;
private static MainPage _mainPage;
private static CartPage _cartPage;
[ClassInitialize]
public static void ClassInitialize(TestContext testContext)
{
_driver = new LoggingDriver(new WebDriver());
_driver.Start(Browser.Chrome);
_mainPage = new MainPage(_driver);
_cartPage = new CartPage(_driver);
}
[ClassCleanup]
public static void ClassCleanup()
{
_driver.Quit();
}
[TestInitialize]
public void TestInitialize()
{
_mainPage.Open();
}
[TestMethod]
public void OpenBlogPage()
{
_mainPage.MainMenuSection.OpenBlogPage();
}
I'm facing different approaches, sometimes I see that QA's are using static hooks/ driver and some of those are using non static. What is the best approach to be taken?

Related

How can I run my browser only one time

I want to start my browser only one time and then get unstatic information from there. But my browser starts many times. How can I start it only one time and close it, when my bool = false;
class Program
{
public static bool GameIsRun = true;
static void Main(string[] args)
{
CheckGame();
}
public static ChromeDriver GetDriver()
{
return new ChromeDriver();
}
public static void CheckGame()
{
while (GameIsRun)
{
GetInformation(GetDriver());
}
}
static void GetInformation(ChromeDriver driver)
{
driver.Navigate().GoToUrl("myURL");
do
{
//loop for doing something on this page, I don't want to start my browser many times.
}
while ();
}
}
May this will work for you.
class Program
{
public static bool GameIsRun = true;
public static IWebDriver driver = null;
static void Main(string[] args)
{
CheckGame();
}
public static ChromeDriver GetDriver()
{
if(driver == null){
driver = new ChromeDriver();
}
return driver;
}
public static void CheckGame()
{
while (GameIsRun)
{
GetInformation(GetDriver());
}
}
static void GetInformation(ChromeDriver driver)
{
driver.Navigate().GoToUrl("myURL");
do
{
//loop for doing something on this page, I don't want to start my browser many times.
}
while ();
}
}
For that you can use singleton concept.
public sealed class Singleton
{
private static Singleton instance=null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance==null)
{
instance = new Singleton();
}
return instance;
}
}
}
Hope this will help you.
The reason behind this is while (GameIsRun) code . GameIsRun is always true that is why it goes to infinite loop.
How you can overcome this issue : you have to make the value of GameIsRun false after launching the browser just like this :
public static void CheckGame()
{
while (GameIsRun)
{
GetInformation(GetDriver());
GameIsRun = false;
}
}
Use this code , once the browser has launched , it would make GameIsRun as false.
Hope it'll help you to resolve your issue.

Using delegates with non static methods in C#

Asking the same question as
Using delegates with non static methods [no picked answer]
so as to bring a closure to it.
So I use #Adam Marshall's solution, it works, but as soon as I start using it, i.e., Testit():
using System;
public class TestClass
{
private delegate void TestDelegate();
TestDelegate testDelegate;
public TestClass()
{
testDelegate = new TestDelegate(MyMethod);
}
public static void Testit()
{
testDelegate();
}
private virtual void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TestClass.Testit();
}
}
It started to give the followig Error:
A object reference is required for the non-static field, method, or property
You can test it out here .
How to fix it? (Please fix it if possible instead of directing me to other posts, I've read them but am not able to understand them) Thx.
Either everything has to be static or everything has to be instance. You're getting in trouble because you are mixing and matching.
Everything static:
using System;
public class TestClass
{
private delegate void TestDelegate();
static TestDelegate testDelegate; //<-- static
static TestClass() //<-- static
{
testDelegate = new TestDelegate(MyMethod);
}
public static void Testit()
{
testDelegate();
}
private static void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TestClass.Testit();
}
}
Everything instanced:
using System;
public class TestClass
{
private delegate void TestDelegate();
TestDelegate testDelegate;
public TestClass()
{
testDelegate = new TestDelegate(MyMethod);
}
public void Testit()
{
testDelegate();
}
private void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
var t = new TestClass();
t.Testit(); //<-- non-static
}
}
Output (same in both examples):
Hello World
Foobar
You could use action C# internal delegate. This way you do not have specify the delegate. Then in your static method you could new up your object.
using System;
public class TestClass
{
Action testDelegate;
public TestClass()
{
testDelegate = new Action(MyMethod);
}
public static void Testit()
{
TestClass ts = new TestClass();
ts.testDelegate();
}
private void MyMethod()
{
Console.WriteLine("Foobar");
}
}
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TestClass.Testit();
}
}
Output:
Hello World
Foobar

C# code extract FROM methods

Does someone know a c# tool that could extract all code in given class from methods?
Example starting file:
public class HelloWorld
{
public static void Main()
{
Print("Hello, ");
Print("World");
PrintExclamationMark();
}
private static void Print(string text)
{
System.Console.WriteLine(text);
}
private static void PrintExclamationMark();
{
System.Console.WriteLine("!");
}
}
Result I would like to get after using the tool on given class/file:
public class HelloWorld
{
public static void Main()
{
System.Console.WriteLine("Hello, ");
System.Console.WriteLine("World");
System.Console.WriteLine("!");
}
}
Such tool could be very helpful when I extracting a lot of new methods and double checking if no code was omitted.

System.TypeInitializationException on setting a static public variable

In the same namespace I have these two functions
public static class DAL
{
public static bool SHOWMESSAGES = false;
private static StreamWriter logger = new StreamWriter(logLocation); // line 4
}
class testmain
{
static public void Main(string[] args)
{
DAL.SHOWMESSAGES = true; //line 5
}
}
If I run this code I will get a "An unhandled exception of type 'System.TypeInitializationException' occurred". What could be causing this?
In .NET, types are not initialized at application startup but when you access the type for the first time. In your case it is probably at line 5: DAL.SHOWMESSAGES = true;
before this statement is executed, DAL class must be initialized. Static constructor is executed and all the static field are set to their default value. If this fails, you get TypeInitializationException.
I would recommend you to avoid static constructors and static field with default value, e.g. private static _someField = new SomeClass();
I your case it could look like this:
public static class DAL
{
public static bool SHOWMESSAGES = false;
private static StreamWriter logger;
public static void Initialize()
{
logger = new StreamWriter(....);
}
}
or even better, avoid static class entirely:
public class DAL
{
public bool ShowMessages { get; set; }
private StreamWriter logger;
public DAL()
{
logger = new StreamWriter(....);
}
}
class testmain
{
static DAL dal;
static public void Main(string[] args)
{
dal = new DAL();
dal.ShowMessages = true;
}
}

Firefoxdriver does not launch int test unit c#

I have te code below:
[TestClass]
public class UnitTest3
{
private static FirefoxDriver _webDriver;
private TestContext testContextInstance;
private static string _baseUrl;
public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}
public static string BaseUrl
{
get
{
return _baseUrl;
}
set
{
_baseUrl = value;
}
}
[ClassInitialize()]
public static void Initialize(TestContext testContext)
{
_webDriver = new FirefoxDriver();
}
[ClassCleanup()]
public static void Cleanup()
{
_webDriver.Quit();
}
[TestMethod]
public void OpenGoogle_PageOpenSuccessfully()
{
BaseUrl = "http://www.google.es";
_webDriver.Navigate().GoToUrl(BaseUrl);
}
}
When debugging the test _webDriver = new FirefoxDriver is left wondering and does not launch.
I'm use Selenium-Webdriver and c# unit test (mstest)
Are there any issue with firefoxdriver?
I update to Selenium Web Driver 2.33 an working successfully.
Thanks

Categories