3

I am stuck badly with this piece of code. I am trying to write a code to use existing Browser if it exists otherwise launch my website in new browser window. Currently, if browser is not open with title "mysite" it throw an exception and doesn't go into else {} block where I'm expecting it to launch mysite instead of getting failed.

    [TestInitialize]
    public void Initialize()
    {
        if (BrowserWindow.Locate("mysite").Exists)
        {
            BrowserWindow.CurrentBrowser = "ie";
            mParentWindow = BrowserWindow.Locate("odrive");
            mParentWindow.Maximized = !mParentWindow.Maximized;
        }
        else
        {
            BrowserWindow.CurrentBrowser = "ie";
            mParentWindow = BrowserWindow.Launch("www.mysite.com");
            mParentWindow.CloseOnPlaybackCleanup = false;
            mParentWindow.Maximized = !mParentWindow.Maximized;
        }
    }
1
  • Have you tried debugging your test initialize method? Without running the code, I'd guess that you're getting a null reference exception when the browser isn't open because there is no browser object that can run the Locate method.
    – Kate Paulk
    Feb 25, 2015 at 12:25

1 Answer 1

2

First get the browser window process.

public static Process proc = null;

[ClassInitialize]
public void ClassInitialize(TestContext context)
{
    Playback.Initialize();
    BrowserWindow _bw = BrowserWindow.Launch(new Uri("about:blank"));
    proc = _bw.Process;

    _bw.CloseOnPlaybackCleanup = false;
 }

Then in your test method create a new browser window instance using the FromProcess method passing in the process of the original browser window instance.

BrowserWindow _bw = BrowserWindow.FromProcess(proc);   

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.