How to link ScreenCaptureJob to browser window? - c#

I'm trying record screen video using microsoft expression encoder SDK.
But it also records screen of locked Windows (after timeout or Win+L).
How to tether captureJob to chrome/firefox browser and capture only browser window?
using Microsoft.Expression.Encoder.ScreenCapture;
[Binding]
public sealed class CommonWebActions
{
[BeforeScenario]
public static void Initialize(TestContext testContext)
{
Driver = new ChromeDriver();
Driver.Manage().Window.Maximize();
if (VideoRecorder.Status != RecordStatus.Running)
{
TestRecordName = "Test.wmv";
VideoRecorder.OutputScreenCaptureFileName = TestRecordName;
VideoRecorder.Start();
}
}
[AfterScenario]
public static void CleanUp()
{
VideoRecorder.Stop();
if (_testContext.CurrentTestOutcome == UnitTestOutcome.Passed ||
_testContext.CurrentTestOutcome == UnitTestOutcome.Inconclusive)
{
File.Delete(TestRecordName);
}
Driver.Quit();
}
}

Related

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);
}

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.

Adding EventFiringWebDriver to Selenium Framework

I need help with expanding my already build Selenium framework with WebDriver. I want to add eventHandlers that use EventFiringWebDriver on top of my code to add event for each action triggered by WebDriver for logging purposes.
To begin with, I will try to clearly present code flow.
Test starts...
Step 1. CspTestBase
public class CspTestBase : CommonElements
{
[BeforeScenario]
public void Init()
{
Initialize(); <- Code to init driver.
// more code here.
LoginPage.Goto();
LoginPage.LoginAs(TestConfig.Username).WithPassword(TestConfig.Password).Login();
}
[AfterScenario]
public void CleanUp()
{
Close();
}
When it jumps to Init method. This class holds all methods that directly require WebDriver instance.
public static class Driver
{
public static IWebDriver Instance { get; private set; }
public static void Initialize()
{
Instance = new TestDriverFactory().CreateDriver(); <- Next step
Instance.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
Instance.Manage().Cookies.DeleteAllCookies();
//Instance.Manage().Window.Maximize();
}
Then when WebDriver Instance is created:
public class TestDriverFactory
{
public IWebDriver CreateDriver()
{
return new TestBase().Create(
new LocalDriverConfig(
TestConfig.Browser));
}
}
And when it finally comes to creating actual driver:
public class TestBase
{
private static IWebDriver _driver;
//string env;
private DesiredCapabilities _capabilities;
public IWebDriver Create(LocalDriverConfig config)
{
switch (config.Browser)
{
case "Chrome":
_driver = new ChromeDriver(#"C:\Utilities");
break;
case "Firefox":
SetFirefoxCapabilities();
//_driver = new FirefoxDriver(_capabilities);
break;
...
private void SetFirefoxCapabilities()
{
...
_capabilities = DesiredCapabilities.Firefox();
//FirefoxOptions options = new FirefoxOptions();
//options.SetLoggingPreference(LogType.Browser, LogLevel.Warning);
_capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, profile);
//capabilities.SetCapability(FirefoxDriver.ProfileCapabilityName, options);
EventFiringWebDriver firingDriver = new EventFiringWebDriver(new FirefoxDriver(binary, profile, TimeSpan.FromSeconds(60)));
// copy the profile for ommiting duplicate references
firingDriver.ExceptionThrown += firingDriver_TakeScreenshotOnException;
_driver = firingDriver;
}
private void firingDriver_TakeScreenshotOnException(object sender, WebDriverExceptionEventArgs e)
{
TakeScreenshot();
}
As you can see, I'm using EventFiringWebDriver already but I want to have it done properly to save me as much work as possible.
I did some reading and looked up solutions but these were simple or in Java. Any help will be much appreciated.

testfixturesetup failed in the program

I was writing automation using C# Selenium. I wrote this code and the test keeps on failing. what do you think the problem is? chrome opens however it does not google
namespace ConsoleApplication4Sel
{
class Program
{
IWebDriver driver = new ChromeDriver();
static void Main(string[] args)
{
}
[SetUp]
public void initialization()
{
driver.Navigate().GoToUrl("http://www.google.com");
}
[Test]
public void ExcuteTest()
{
IWebElement element = driver.FindElement(By.Name("q"));
//perform ops
element.SendKeys("Execute authomation");
}
[TearDown]
public void CleanUp()
{
driver.Close();
}
}
}
Please post the error you're getting.
But in any case you forgot to link the path to the driver, meaning:
IWebDriver driver = new ChromeDriver(#"YOUR_PATH");
Put there your path, for example "D:\Download\chromedriver".

Categories