How to open new TAB in IE using WatiN? - c#

How can I open a tab and go to "http://anotherwisite.com"? I tried this:
ie = new WatiN.Core.IE("https://somewebsite.com", true);
ie.TextField(WatiN.Core.Find.ByName("user")).TypeText(User.getName());
ie.TextField(WatiN.Core.Find.ByName("password")).TypeText(User.getPassword());
ie.Button(WatiN.Core.Find.Any).Click();
(the above code opens an IE and log the person in)

I can open a new Tab and open new page with Watin
Code:
var browser1 = new FireFox("http://www.omda.com");
SendKeys.SendWait("^{t}");
browser1.GoTo("http://www.omda2.com");
I use the keyboard, CTRL+T in Firefox.

There is no built-in method in WatiN to do that.

System.Windows.Forms.SendKeys.SendWait("^{t}");
Thread.Sleep(400);
//this is the new browser instance
var newTab = Browser.AttachTo<IE>(Find.ByTitle(string.Empty));

Related

WatiN doesn't find anything

I'm new to C# and I'm trying to do an application that automatize Internet Explorer.
When I click a button, the application does :
using ( var Browser = new IE())
{
Browser.GoTo("http://testweb.com");
Browser.TextField(Find.ByName("username")).TypeText("User");
Browser.TextField(Find.ByName("password")).TypeText("Pass");
}
But it doesn't write anything. It navigates to the web but...
Try this:
IE ie = null;
ie = new IE();
ie.GoTo("Link");
ie.WaitForComplete();
At least to get started.
For the other bit, you need to get an exact identification and then you can tell WaTiN to interact with it.
Textfield userTextBox = ie.Textfield(Find.ByName("name"));
userTextBox.TypeText("user");
This may seem banal but now you can add a peek definition in your code and see if "userTextBox" gets found by name. If it doesn't you need to find it through another method (ID or class).

Open a new tab in an existing browser session using Selenium

My current code below in C# opens a window then navigates to the specified URL after a button click.
protected void onboardButton_Click(object sender, EventArgs e)
{
IWebDriver driver = new ChromeDriver();
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.Navigate().GoToUrl("http://www.google.com")
}
But the site that I am planning to navigate to has single sign-on. How can I open a new tab in my existing browser session and navigate from there? The above code does not seem to work.
Sending Keys.Control + "t" didn't work for me. I had to do it with javascript and then switch to it.
((IJavaScriptExecutor)driver).ExecuteScript("window.open();");
driver.SwitchTo().Window(driver.WindowHandles.Last());
To handle new tab you should switch to it first. Try following:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.Navigate().GoToUrl("http://www.google.com")
Also you might need to switch back:
driver.SwitchTo().Window(driver.WindowHandles.First());
This may not works:
driver.FindElement(By.CssSelector("body")).SendKeys(Keys.Control + "t");
Alternative: Find clickable element with target blank (search for "blank" in page's surce code). This will open new tab.
Than switch between tabs (thanks #Andersson) with:
driver.SwitchTo().Window(driver.WindowHandles.Last());
driver.SwitchTo().Window(driver.WindowHandles.First());
We can simulate Ctrl + Element Click
Actions action = new Actions(_driver);
action.KeyDown(Keys.Control).MoveToElement(body).Click().Perform();
Reference:
https://www.codeproject.com/Answers/1081051/Open-link-in-New-tab-using-Selenium-Csharp-for-chr#answer3
The solution is really simple:
Driver.SwitchTo().NewWindow(WindowType.Tab);
Enjoy...
IWebDriver driver = new ChromeDriver();
Change this to:
var driver = new ChromeDriver();
I do not know why. May be the IWebDriver miss the method.

Open link in new tab selenium c#

I am writing a program to run videos listed on my site for testing purpose and here what I need is to run videos in different tabs of the same browser window.
I have hundred video urls in the List videoLinks = getVideoUrls();
and now what I need is to execute these videos 5 at a time.
ChromeDriver driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.withoutabox.com" + videoLink);
If I go the above way then for all videos I will have to create a new ChromeDriver object. I want to use single chrome browser object.
I have tried this
IWebElement body = driver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
it only adds a new tab but not open a link there.
Please let me know how should I go around it. I have googled but couldn't find my solution so thought to ask for help.
Try this:
public void SwitchToTab(object pageId)
{
webDriver.SwitchTo().Window(pageId.ToString());
}
You can use CurrentWindowHandle to find current tab.
webDriver.CurrentWindowHandle;
For your scenario I'm using that code:
public IPageAdapter OpenNewTab(string url)
{
var windowHandles = webDriver.WindowHandles;
scriptExecutor.ExecuteScript(string.Format("window.open('{0}', '_blank');", url));
var newWindowHandles = webDriver.WindowHandles;
var openedWindowHandle = newWindowHandles.Except(windowHandles).Single();
webDriver.SwitchTo().Window(openedWindowHandle);
return new SeleniumPage(webDriver);
}
Update
Window open create new popup. By default this option can be blocked by browser settings. Disable popup blocking in your browser manually.
To check this, open js console in your browser and try to execute command window.open('http://facebook.com', '_blank');
If new window open successfully than everythng is OK.
You can also create your chrome driver with specific setting. Here is my code:
var chromeDriverService = ChromeDriverService.CreateDefaultService();
var chromeOptions = new ChromeOptions();
chromeOptions.AddUserProfilePreference("profile.default_content_settings.popups", 0);
return new ChromeDriver(chromeDriverService, chromeOptions, TimeSpan.FromSeconds(150));
Here is a simple solution for open a new tab in seleneium c#:
driver.Url = "http://www.gmail.net";
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");

WatiN need to declare browser outside of STAThreaded thread

I'm currently trying to use WatiN to do some automatic data collection. I used to use a WebBrowser controll, and the way I did it was I declared a stathread and ran it from there, then using a Browser.DocumentCompleted I started a void LoginPageLoaded on which I would set the user and pass, and login. Thing is, with WatiN, I'm trying to do
var th = new Thread(() =>
{
Browser browser = new IE(url);
browser.WaitForComplete();
HolyThunder hl = new HolyThunder();
l.LoginPageLoaded();
});
th.SetApartmentState(ApartmentState.STA);
th.Start();
But obviously, when I try to use the browser instance on the LoginPageLoaded void it says it downs't know what it is, because it was declared inside the th thread. I didn't do it when I ran LoginPageLoaded through browser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(LoginPageLoaded);, and I obviously can't declare the browser outside of that STAThread. What do you think I can do to fix this?
EDIT - I failed so bad... if I do everything I need to do inside that STAthread, everything will work. My question now is, in watin, is there anyway to open a "webbrowser" without actually having the visual component of the webbrowser?
You can try using NHtmlUnit, it's a headless browser. I am not sure if you can do it with WatiN.
You can run WebKit headless (see, for example http://phantomjs.org/), but I don't know if you can drive it from WatiN.
I guess you want to hide web browser window.
In that case try this:
Settings.Instance.MakeNewIeInstanceVisible = false;
To open a "webbrowser" without actually having the visual component of the webbrowser, use
var browser = new IE();
browser.Visible = false;

How to handle multiple tabs in internet explorer with the IWebBrowserApp com interface?

I am using the following code (C#) based on the IWebBrowserApp com interface to find the Internet explorer window that matches the page I am trying to find, based on the title of the page.
I works fine if the page is on the first tab, but it does not work if its a later tab. So how do I get access to the tabs in internet explorer?
objSW = new ShellWindows();
IEnumerator ie = objSW.GetEnumerator();
while (ie.MoveNext())
{
obj = ie.Current;
app = (IWebBrowserApp)ie.Current;
System.Object docObj = app.Document;
HTMLDocumentClass hdoc = (HTMLDocumentClass)docObj;
if (hdoc.title.Contains(title)) matches.Add(app.HWND, app);
//do something
}
Sorry, but there's no supported API for tab enumeration/manipulation in IE9 or earlier.

Categories