Firefoxdriver does not launch int test unit c# - 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

Related

Selenium static nunit hooks

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?

Invalid session id in specflow Selenium on closing [AfterScenarios]

I am opening a browser session at [BeginScenario] and Quit the session at the [AfterScenario].
At the beginning of second scenario, i am getting 'OpenQA.Selenium.WebDriverException: 'invalid session id'.
How do we start a new session at the beginning of each scenario.?
[Binding]
public class BasePage
{
public static IWebDriver driver;
public static void BrowserSetup()
{
driver = new ChromeDriver();
//driver.Manage().Cookies.DeleteAllCookies();
driver.Manage().Window.Maximize();
}
}
[Binding]
public class Hook : BasePage
{
[BeforeScenario]
public void Initialize(ScenarioContext scenarioContext)
{
BrowserSetup(); // Initialise the driver from BaseClass
scenario = featureName.CreateNode<Scenario>(scenarioContext.ScenarioInfo.Title);
//scenario = featureName.CreateNode<Scenario>(scenarioContext.ScenarioInfo.Title);
}
[AfterScenario]
public void CleanUp(ScenarioContext scenarioContext)
{
if (scenarioContext.TestError != null)
{
TakeScreenshot(driver);
}
//driver.Close();
driver.Quit();
}
}

Element was null when I user page object pattern

I'm new to mobile automation, and I'm facing a problem with page object pattern. When I try to find element with FindElementById everything works, here is my class with pop:
public class SamplePage
{
private AndroidDriver<AndroidElement> _driver;
[FindsByAndroidUIAutomator(ID = "com.miui.calculator:id/btn_1_s")]
private readonly AndroidElement _buttonOne;
[FindsByAndroidUIAutomator(ID = "android:id/button1")]
private readonly AndroidElement _confirmButton;
public SamplePage(AndroidDriver<AndroidElement> driver)
{
_driver = driver;
PageFactory.InitElements(_driver, this);
}
public void ClickOnConfirmButton()
{
//AndroidElement _confirmButton = _driver.FindElementById("android:id/button1");
_confirmButton.Click();
}
public void ClickOnButtonOne()
{
//AndroidElement _buttonOne = _driver.FindElementById("com.miui.calculator:id/btn_1_s");
_buttonOne.Click();
}
}
And here is main class
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using System;
using AppiumDotNetSamples.Helper;
namespace AppiumDotNetSamples
{
[TestFixture()]
public class AndroidBasicInteractionsTest
{
private AndroidDriver<AndroidElement> driver;
private SamplePage _samplePage;
[SetUp()]
public void BeforeAll()
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(MobileCapabilityType.PlatformName, "Android");
capabilities.SetCapability(MobileCapabilityType.PlatformVersion, "7.1.2");
capabilities.SetCapability(MobileCapabilityType.AutomationName, "UIAutomator2");
capabilities.SetCapability(MobileCapabilityType.DeviceName, "3e52f2ee7d34");
capabilities.SetCapability("appPackage", "com.miui.calculator");
capabilities.SetCapability("appActivity", "com.miui.calculator.cal.CalculatorActivity");
driver = new AndroidDriver<AndroidElement>(new Uri("http://localhost:4723/wd/hub"), capabilities, TimeSpan.FromSeconds(180));
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
_samplePage = new SamplePage(driver);
}
[Test()]
public void Click()
{
_samplePage.ClickOnConfirmButton();
_samplePage.ClickOnButtonOne();
}
[TearDown()]
public void AfterAll()
{
driver.Quit();
}
}
}
What am I doing wrong? I test on on Xiaomi Calculator app, but earlier I got the same issues on any other app like Google Calculator.
The ClickConfirmButton method is not returning the driver and hence it is null.
You may want to try something similar this and see if it is working
public AboutPage goToAboutPage()
{
about.Click();
return new AboutPage(driver);
}

Multiple public constructors with same maximum parameter count are not supported

I have these 2 classes
public class BrowserContext
{
private readonly ChromeDriver _driver;
public BrowserContext(ChromeDriver driver)
{
_driver = driver;
}
public void NavigateTo()
{
_driver.Navigate().GoToUrl("http://bbc.com");
}
}
public class Homepage
{
private readonly BrowserContext _browserContext;
public Homepage(BrowserContext browserContext)
{
_browserContext = browserContext;
}
[Given(#"I navigate to url")]
public void GivenINavigateToUrl()
{
_browserContext.NavigateTo();
}
When I try to run the test I get below error
Multiple public constructors with same maximum parameter count are not
supported! OpenQA.Selenium.Chrome.ChromeDriver (resolution path:
ClassLibrary3.Steps.Homepage->ClassLibrary3.Support.BrowserContext)
Please help!
Based on this answer https://stackoverflow.com/a/26402692/10148657 solution is to instantiate ChromeDriver in BrowserContext constructor rather than accept it in constructor:
public class BrowserContext
{
private readonly ChromeDriver _driver;
public BrowserContext()
{
_driver = new ChromeDriver();
}
public void NavigateTo()
{
_driver.Navigate().GoToUrl("http://bbc.com");
}
}

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.

Categories