Spinner clear value exception in Selenium c# Webdriver - c#

I want to test this popup window:
but for spinner I get exception. I tried these codes but non of them worked.
Actions componentProportion = new Actions(driver);
componentProportion.SendKesy(Kesy.Control+"a");
componentProportion.SendKeys(editNumber,"2356").Build().Perform();
When I run this code nothing happens and it also causes problem for new text box(Display Title) code and it doesn't work properly.
//Edit display title
IWebElement editDt = driver.FindElement(By.XPath("//*[#id='cmp_display_title']"));
editDt.Click();
Thread.Sleep(500);
editDt.SendKeys(Keys.Control + "a");
editDt.SendKeys(Keys.Delete);
Thread.Sleep(500);
editDt.SendKeys("hello");
And if I want to use this code:
IWebElement editNumber = driver.FindElement(By.CssSelector("something"));
editNumber.Click();
Thread.Sleep(1000);
editNumber.SendKeys(Keys.Control+"a");
I get this exception:
ElementVisibleException : element not visible
Is there anyway to solve this problem? Thank you for your help.

So, since your elements are in frame, in order to be able to work with them you need to switch to that frame first (usually by name)
//Switch to required frame
driver.SwitchTo().Frame("ContentContainer");
// find your elements and do the actions
// This is how you switch to the default page
driver.SwitchTo().DefaultContent();

As I noticed, you want to replace the display title input text with some new text
In order to clear the text, you dont need to simuale ctrl+a.
Simply, use this code to set new value to your text element :
IWebElement editDt = driver.FindElementById("cmp_display_title");
editDt.SendKeys(""); // clears the text
editDt.SendKeys("your new text");

Related

Need to enter Text

I tried finding the element using Get but it does not work, thats why i treid with GetElement method
I am trying to enter text in an textbox element found using GetElement in teststack white using C#
i want to know how to cast the automation element to UIitem so that i can do enter() or click operation on that element
var all = appWindow.GetElement(SearchCriteria.ByControlType(ControlType.ComboBox)
.AndByText("Model collapsed"));
var element = all.FindFirst(TreeScope.Children,
new PropertyCondition(AutomationElement.NameProperty, "Edit Box collapsed"));
element.enter("");
when i do element.enter or click it gives error, i think i need to cast it or is there any other way where i can achieve this. Thank you.
After using the below code i was able to enter text.
var all = appWindow.GetElement(SearchCriteria.ByControlType(ControlType.ComboBox)
.AndByText(parentValue));
var element = all.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, childValue));
TextBox textBox = new TextBox(all, appWindow.ActionListener);
TestStack.White.InputDevices.AttachedKeyboard keyboard = appWindow.Keyboard;
textBox .Click();
keyboard.Enter("test");

Selenium unhide elements C#

I have Menu Group:
<div class="menuGroup">
Some of this div contain class which hide or show menu contents
class="toggleMenuChildren">
When you click on it, it change on
class="toggleMenuChildren opened">
So I want show all content from menu (click on all classes toggleMenuChildren) to show it.
I try this
IWebElement zi = driver.FindElement(By.ClassName("toggleMenuChildren"));
zi.Click();
But this opened (unhide) just first element, and if you call it again then hide content. How I can show all content (click on all elements) ?
You can use xpath - //div[contains(#class,'toggleMenuChildren') and not(contains(#class,'opened'))]
(sorry it is in java)
List<WebElement> allElements = driver.findElements(By.xpath("//div[contains(#class,'toggleMenuChildren') and not(contains(#class,'opened'))]"));
for(WebElement ele: allElements){
ele.click;
}
You can try to use hover to show all the information under toggleMenuChildren class
Actions actions = new Actions(driver);
IWebElement menuHoverLink =
driver.FindElement(By.XPath("//div[#class='toggleMenuChildren']"));
actions.MoveToElement(menuHoverLink);
actions.Build().Perform();
//That is the elements under the toggleMenuChildren class that you can use
driver.FindElement(By.PartialLinkText("...")).Click();
after perform() line all the elements will be unhided .

PhantomJS is clearing out fields after sendKeys had previously populated them

I wait for an element to appear then I try to populate it with SendKeys. 40% of the time the element isn't populated even though the element is enabled and displayed. I put all kinds of Thread.Sleep with all kinds of values everywhere.
My issue is similar to this one but I'm using the PhantomJS driver instead of the Firefox one. Using the solutions from the link above didn't work, I just got undefined function exceptions.
public static IWebElement FindElement(this IWebDriver driver, By by, int timeoutInSeconds)
{
if (timeoutInSeconds > 0)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
return wait.Until(drv => drv.FindElement(by));
}
return driver.FindElement(by);
}
zipcode = _driver.FindElement(By.Name("iZipCode"), 50);
while(!zipcode.Displayed)
{
System.Threading.Thread.Sleep(3000);
}
zipcode.Click();
zipcode.Clear();
System.Threading.Thread.Sleep(3000);
zipcode.SendKeys(OpenQA.Selenium.Keys.Backspace);
zipcode.SendKeys(text);
The element that I'm trying to populate is a textbox that appears upon a combobox selection. I click element in combobox -> textbox is displayed (it is hidden prior to selection of item in combobox).
Using Enter fixed the issued:
zipcode.SendKeys(text);
zipcode.SendKeys(OpenQA.Selenium.Keys.Enter);

Selenium ChromeDriver switch tabs

When I click on a link in my test, it opens a new tab.
I want ChromeDriver to then focus on that tab. I have tried the following code to get ChromeDriver to change tabas using the ctrl+tab shortcut:
Actions builder = new Actions(driver);
builder.KeyDown(Keys.Control).KeyDown(Keys.Tab).KeyUp(Keys.Tab).KeyUp(Keys.Control);//switch tabs
IAction switchTabs = builder.Build();
switchTabs.Perform();
But this throws the following exception:
ekmLiveChat.tests.UITests.EndToEndTest.EndToEnd:
System.ArgumentException : key must be a modifier key (Keys.Shift, Keys.Control, or Keys.Alt)
Parameter name: key
Is there a way to switch tabs using ChromeDriver?
This is what worked for me:
var popup = driver.WindowHandles[1]; // handler for the new tab
Assert.IsTrue(!string.IsNullOrEmpty(popup)); // tab was opened
Assert.AreEqual(driver.SwitchTo().Window(popup).Url, "http://blah"); // url is OK
driver.SwitchTo().Window(driver.WindowHandles[1]).Close(); // close the tab
driver.SwitchTo().Window(driver.WindowHandles[0]); // get back to the main window
As mentioned in my comment on your post, I'm not sure if the Chrome driver handles tabs the same way as it handles windows.
This code works in Firefox when opening new windows, so hopefully it works in your case as well:
public void SwitchToWindow(Expression<Func<IWebDriver, bool>> predicateExp)
{
var predicate = predicateExp.Compile();
foreach (var handle in driver.WindowHandles)
{
driver.SwitchTo().Window(handle);
if (predicate(driver))
{
return;
}
}
throw new ArgumentException(string.Format("Unable to find window with condition: '{0}'", predicateExp.Body));
}
SwitchToWindow(driver => driver.Title == "Title of your new tab");
(I hope my edits to the code for this answer didn't introduce any errors...)
Just make sure you don't start looking for the new tab before Chrome has had the chance to open it :)
On my code I click a button and opens a tab (so it is already on the new tab, I don't need to do something to go to that new tab) and run this so it recognize the new tab and worked:
driver.SwitchTo().Window(driver.WindowHandles.Last());
After a long fight with this I was able to get this working with chrome driver. The alert message is not visible but brings tab to front and accept closes it immediately.
//Rotate Tabs
seleniumDriver.SwitchTo().Window(seleniumDriver.WindowHandles[currentUrlIndex]);
IJavaScriptExecutor jscript = seleniumDriver as IJavaScriptExecutor;
jscript.ExecuteScript("alert('Focus')");
seleniumDriver.SwitchTo().Alert().Accept();
In C# I used the below lines to switch between the two tab.
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("window.open();");
IList<string> tabs = new List<string>(driver.WindowHandles);
driver.SwitchTo().Window(tabs[1]);
driver.Navigate().GoToUrl("http://www.google.com");

Create a VS2010 Addin to collapse every methods of my active document

I'm looking for the source code to collapse every methods of my active document using the VS2010 Addin.
For the moment I parse the text content of the document trying to match if the line is a method signature. If it is the case, I collapse the method.
TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
var editPoint = selection.ActivePoint.CreateEditPoint();
editPoint.MoveToLineAndOffset(1, 1);
while (!editPoint.AtEndOfDocument)
{
editPoint.StartOfLine();
var line = editPoint.GetText(editPoint.LineLength).TrimStart();
if (line.StartsWith("public"))
{
selection.MoveToLineAndOffset(editPoint.Line, 1);
_applicationObject.ExecuteCommand("Edit.ToggleOutliningExpansion");
}
// go to the next line
}
Does anyone could tell me if I'm on the good way or if there is an easiest way ?
Maybe I asked not so well my question. My real goal was to collapse all the code : properties, methods, comments with ///, using; but not the regions.
Here is one solution :
// reduce everything like Ctrl+M+O
_applicationObject.ExecuteCommand("Edit.CollapsetoDefinitions");
// save the cursor position
TextSelection selection = (TextSelection)_applicationObject.ActiveDocument.Selection;
var selectedLine = selection.ActivePoint.Line;
var selectedColumn = selection.ActivePoint.DisplayColumn;
// open the regions
selection.StartOfDocument();
while (selection.FindText("#region", (int)vsFindOptions.vsFindOptionsMatchInHiddenText))
{
// do nothing since FindText automatically expands any found #region
}
// put back the cursor at its original position
selection.MoveToDisplayColumn(selectedLine, selectedColumn);
I hope this could help

Categories

Resources