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;
}
}
-
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– Kate Paulk2015年02月25日 12:25:44 +00:00Commented Feb 25, 2015 at 12:25
1 Answer 1
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);
Explore related questions
See similar questions with these tags.