Selenium doesn't recognize new opened window -Selenium C# - c#

Seems like I'm facing some sync issues in my code.
during my process, I'm clicking a button which opens a new window.
I'm swicthing to the new window by the following code.
_webdriver.SwitchTo().Window(_webdriver.WindowHandles.Last();
Then, I'm inserting data into fields within the next window.
problem is that sometimes the objects in the "next window" are not being found.
I'm getting : "can't find element" error.
for me it seems like a sync problem , meaning , DOM issues.
so I have tried using :
_webdriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
and I even tried :
Thread.Sleep(3000);
Unfortunately , seems like most of the times the problem is that selenium didn't switch to the new window(could see it when debugging).
I'll be happy to have your assistance.

You could wait for two windows and then set the context to the new one:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
// wait for 2 windows
ReadOnlyCollection<String> handles = null;
wait.Until((d) => (handles = driver.WindowHandles).Count > 1);
// set the context on the new window
driver.SwitchTo().Window(handles[handles.IndexOf(driver.CurrentWindowHandle) ^ 1]);

i am not sure how this is done in C# but i think selenium is same you just have to use C# syntax for loop.
//Switch to newly opened window (JAVA)
for(String winHandle : driver.getWindowHandles()){
driver.switchTo().window(winHandle);
}
this is how i do in Java. you will get driver.getWindowHandles() same method in C# as well, if i am not wrong as this is selenium method.
hope this is helpful to you.

Related

Unable to handle authentication pop up

I am using C# , Selenium , AutoIt and Google Chrome.
I can launch the browser, and can see the authentication pop up.
Pop up window disappears when below code is executed and after that the browser stays there forever.
autoItX3 autoIt = new AutoItX3();
Driver.Instance.Manage().Window.Maximize();
Driver.Instance.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(2);
try
{
Driver.Instance.Navigate().GoToUrl(Driver.BaseAddress);
}
catch
{
return;
}
autoIt.WinWait("Authentication Required");
autoIt.WinActivate("Authentication Required");
autoIt.Send("admin");
autoIt.Send("{TAB}");
autoIt.Send("pass");
autoIt.Send("{ENTER}");
Driver.Instance.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(-1);
You are trying to automate a child window.
Autoit doesn't see child windows untill told to.
Opt("WinSearchChildren", 1) ;0=no, 1=search children also
Allows the window search routines to search child windows as well as
top-level windows. 0 = (default) Only search top-level windows 1 =
Search top-level and child windows
hard to make comment without knowing the internals of the authentication implementation on the server. One thing is sure - it is a bad idea from security view because parameters appended to the URL are not secure.
like : http://myURL.com/index.jsp/j_security_check?j_username=username&j_password=password
or
"http://username:password#www.example.com/")
this is what worked for me according to our internal authentication :
https://myURL.com/login/Login.aspx?usestandardlogin=1
so its => "http:YouURL.com" + "?" + "usestandardlogin=1"
now I am not seeing any pop up , it just re-direct me on login.

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).

watin attach to manually started window keeps failing

IList<string> values = new List<string>();
var instance = Find.By("hwnd", "110CC");
...
if(instance != null)
{
var ie = Browser.AttachTo<IE>(instance);
The browser instance is manually started by the tester in case this makes any difference.
This just doesn't work for me I keep getting an exception from watin saying that it can't find a window with that handle.
I got the handle with Spy++.
I tried searching by window title or window url also but it also didn't work.
Is there any way to do this?
Thank you
The below works as expected / no errors. WatiN 2.1, IE9, Win7
Before running the code, open an IE browser and point it at cnn.com
IE browser = Browser.AttachTo<IE>(Find.ByUrl("www.cnn.com"));
browser.TextField("hdr-search-box").TypeText("searchy");

Selenium 2.0 WebDriver Advcanced Interactions DoubleClick Help (c#)

So within my selenium regression tests, I've been trying to double click on a calendar to make a new appt. I have attempted to use the doubleClick(); method within the advanceduserinteractions library, but there is an issue; the two clicks aren't fast enough/close enough together to fire an actual double-click! Has anybody found a way to deal with this in their testing?
This code works for me!
Actions action = new Actions(driver);
action.doubleClick(myElemment);
action.perform();
Here is the Java equivalent. This code will blindly open the first event. You could add some logic to open a specific event etc. This code works! (tested with 2.12)
List<WebElement> events = driver.findElements(By.cssSelector("div.dv-appointment"));
for(WebElement event:events){
WebElement body = event.findElement(By.cssSelector("div.body"));
if(!body.getText().isEmpty()) //or open a known event
{
System.out.println(body.getText()); //open the first event
Actions builder = new Actions(driver);
Action doubleClick = builder.doubleClick(event)
.build();
doubleClick.perform();
break;
}
}
Do not forget "using"
using OpenQA.Selenium;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Interactions.Internal;
using OpenQA.Selenium.Support.UI;
//create Actions object
Actions builder = new Actions(driver);
//create a chain of actions
builder.DoubleClick().Build().Perform();
http://selenium-interview-questions.blogspot.ru/2014/03/how-to-double-click-on-web-element.html
I too had the problem where Selenium's doubleclick event works in Firefox but has no effect in Chrome. Upgrading to Selenium didn't help; I already have the latest version. (My environment is Ubuntu 14.04, Python 2.7.6, Selenium 2.44.0, Firefox 35.0, Chrome 40.0.2214.91.)
I'm not sure why CBRRacer's answer was downvoted. I successfully worked around the problem by using two click events. This works in both Firefox and Chrome. There are two ways do to it, and both worked for me.
The first way:
elem = driver.find_element_by_css_selector('#myElement')
elem.click()
elem.click()
The second way:
elem = driver.find_element_by_css_selector('#myElement')
actions = webdriver.ActionChains(driver)
actions.click(elem).click(elem).perform()
I quite like the approach used here, particularly queuing the actions first, then performing, since that allows repeated application of the actionchain.
http://selenium-python.readthedocs.org/en/latest/api.html#selenium.webdriver.common.action_chains.ActionChains
From the documentation example linked:
menu = driver.find_element_by_css_selector(".nav")
hidden_submenu = driver.find_element_by_css_selector(".nav #submenu1")
actions = ActionChains(driver)
actions.move_to_element(menu)
actions.click(hidden_submenu)
actions.perform()
have you tried catching the IWebElement and then clicking it twice?
IWebElement element = driver.FindElement(By.Id("yourID"));
element.Click();
element.Click();
I don't know if this would give you the desird functionality or not but I know that when I execute a click event like the one above it runs as close as a double click from an actual user.
The other option is to reference the ThoughtWorks.Selenium.Core, however the only downside of this is that I'm not sure it plays well with the current IWebDriver I think it needs it's own instantiation of the IWebDriver.

Problem hiding Internet Explorer in WatiN, even when using Settings.Instance.MakeNewIeInstanceVisible = false

This question is more of a follow-up to this one:
Hiding Internet Explorer when WatiN is run
Like the person who asked that original question, I also want to stop IE from being shown when my WatiN tests are running, but even when using this setting in a seemingly correct manner (code snippet below), it still ends up showing an empty IE window initially (although it does not show the test behavior/web page interaction).
Is it possible to stop the window from showing at all, or is this as good as it gets?
My helper method to create a new IE instance:
public static IE CreateNewBrowserInstance(string url = DefaultAppUrl)
{
Settings.Instance.MakeNewIeInstanceVisible = false;
Settings.Instance.AutoMoveMousePointerToTopLeft = false;
Settings.Instance.AutoStartDialogWatcher = false;
return new IE(url, true);
}
You can Hide window after initializing new IE instance
browser.ShowWindow(NativeMethods.WindowShowStyle.Hide);

Categories