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;
 }
 }
Niels van Reijmersdal
32.7k4 gold badges59 silver badges125 bronze badges
asked Feb 24, 2015 at 22:00
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. Commented 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); 
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Oct 10, 2017 at 14:34

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.