I have made a number of tests and I'm using selenium to run them. For a certain test I need to use a number of shortcuts. However for some reason my keyUp doesn't work which then knocks off my other shortcuts. Not sure if I'm doing something wrong here.
Has anyone got any ideas why?
My code is as follows
Actions action = new Actions(driver);
action.KeyDown(Keys.Control).KeyDown(Keys.Alt).SendKeys(Keys.Tab).KeyUp(Keys.Shift).KeyUp(Keys.Alt).Build().Perform();
Thread.Sleep(4000);
excelSession.FindElementByName("elementName").Click();
action.KeyDown(Keys.Alt).SendKeys("CFNC").KeyUp(Keys.Alt).Build().Perform();
Thread.Sleep(4000);
Thread.Sleep(4000);
TearDown();
Code wise it seems fine, does the form you are sending keys to have a 4 character limit? It could be that typing that fourth character makes the current form non-active and switches it another object.
For this scenario you could also replace that line with
WebElement typeInto = excelSession.FindElementByName("elementName");
typeInto.sendKeys("CFNC" + Keys.ALT);
Or something like that.
Related
I have an input box that pulls an autocomplete list. The list is a little slow to pull, so I need selenium to just wait before pressing the enter key, which will select the first item in the list. here is what I have so far
webDriver.FindElement(By.Id("seg-gl-1")).SendKeys("2");
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
webDriver.FindElement(By.Id("seg-gl-1")).SendKeys(Keys.Enter);
The issue is that selenium is hitting Enter too quickly. I believe I am using the implicitwait incorrectly. Can anyone shed some light on my issue?
When you use
webDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
You are only setting the default ImplicitWait time. If you want to actually perform a wait for, lets say 10sec, you could use
System.Threading.Thread.Sleep(5000);
Generally you should avoid this type of wait, but I'm guessing that there are javascript/ajax calls performed in the background of your application, and hence you should wait for these to execute before being able to assert the site behaviour (as these calls might update the DOM and such). For further help on that, please refer to my answer in this thread: https://stackoverflow.com/a/45033412/6220192
I have a number of tests which I run with Selenium. I often need to send short cut keys in my tests.
However, sometimes for a bizarre reason, keys don't get released. So at the end of each test I want to release Ctrl + Alt + Shift. I try and perform a keyUp once I send my keys. However, this doesn't always work. Is there a way I can release Ctrl,Alt and Shift keys at the end?
My code is as follows for they KeyDown/KeyUp
Actions action = new Actions(excelSession);
action.KeyDown(Keys.Control).KeyDown(Keys.Alt).SendKeys(Keys.Tab).KeyUp(Keys.Control).KeyUp(Keys.Alt).Perform();
I tried doing something like this to release keys at the end but it didn't work
action.KeyUp(Keys.Control).Perform();
action.KeyUp(Keys.Alt).Perform();
action.KeyUp(Keys.Shift).Perform();
However this didn't work correctly.
So, working in C#. I have a CSS-based drop-down menu. I open the menu, then grab a reference to it using FindElement by CSSSelector. I then grab the contents of the list using FindElements, again by CSSSelector.
Now here's where it get's interesting. I iterate the list, based on a file I have open in a streamreader.
Looks something like:
list = driver.FindElement(By.CSSSelector("dropdown-menu"));
list_items = list.FindElements(By.CSSSelector("LI > A"));
int row = 0;
while (data_file.read())
{
iWebElement item = list_items[row];
string label = item.text;
string url = item.getattribute("href");
assert.areequal("something", label);
assert.areequal("something else", url);
row++;
}
Now here's the thing: if the mouse pointer is placed over the drop-down, while this is executing, item.text returns value and the test succeeds. If the pointer is anywhere else, item.text will be blank and the test fails. Trying to understand what's going on, and taking a clue from the fact that though the test would fail when running, but would succeed while stepping, I modified the code with a loop:
while (data_file.read())
{
iWebElement item = list_items[row];
string label = item.text;
while (label == "")
{
label = item.text;
}
string url = item.getattribute("href");
assert.areequal("something", label);
assert.areequal("something else", url);
row++;
}
Now the test will always succeed, but if the pointer is not on the control it is SIGNIFICANTLY slower... we're talking a factor of 4 or 5... then when the pointer IS on the control. By wrapping a timer around this, I find that it typically takes between 2 and 4 seconds before .text returns anything but an empty string... sometimes longer.
Again, this delay only seems to apply when the mouse pointer is not over the drop-down. Otherwise, the value appears to be there instantaneously.
Can anyone suggest a possible explanation for why it's behaving this way, and a possible approach to solving it?
BTW, I'm not finding any difference between:
item = list_items[row];
label = item.text;
and
label = list_items[row].text;
Nor does .getattribute("value") produce any faster results than .text.
As for why the menus are acting this way, it's hard to tell with the code you've provided. If you could provide the code that displays your menu, that might help. As for solutions, there are a couple.
The label could be taking longer to display because of the while loop itself - it's just going crazy grabbing the text over and over very quickly. A better solution would be to wait for the element to be present. This may make your code run faster. See the Selenium Website for information on using WebDriverWait.
Alternatively, there is an "ugly" solution. You can just move the mouse to the menu with Selenium, to make sure the menu is always displayed when you need it. I've adapted some code from here as an example:
OpenQA.Selenium.Interactions.Actions builder = new
builder.MoveToElement(list).Build().Perform();
Hope this helps!
So I'm currently writing this script that will automate a simple, monotonous task using the Selenium Internet Explorer driver in C#.
Everything works great, but it is a tad bit slow at one point in the script and I'm wondering if there is a quicker way available to do what I want.
The point in question is when I have to fill out a textbox with a lot of information. This textbox will be filled sometimes up to 10,000 lines where each line never exceeds 20 characters.
However, the following approach is very slow...
// process sample file using LINQ query
var items = File.ReadAllLines(sampleFile).Select(a => a.Split(',').First());
// Process the items to be what we want to add to the textbox
var stringBuilder = new StringBuilder();
foreach (var item in items)
{
stringBuilder.Append(item + Environment.NewLine);
}
inputTextBox.SendKeys(stringBuilder.ToString());
Is there any to just set the value of the textbox to what I want? Or is this a bottleneck?
Thank you for your time and patience!
So as suggested by Richard - I ended up using IJavaScriptExecutor.
The exact solution was to replace the call to SendKeys in the following line of code:
inputTextBox.SendKeys(stringBuilder.ToString());
With this line of code:
((IJavaScriptExecutor)ieDriver).ExecuteScript("arguments[0].value = arguments[1]",
inputTextBox, stringBuilder.ToString());
Casting my InternetExplorerDriver object to the IJavaScriptExecutor interface in order to reach the explicitly implemented interface member ExecuteScript and then making the call to that member with the arguments above did the trick.
To avoid using SendKeys, you could use IJavaScriptExecutor to set the value directly. Something along these lines:
string javascript = "document.getElementById("bla").value = stringBuilder";
IJavaScriptExecutor js = (IJavaScriptExecutor)Driver;
js.ExecuteScript(javascript);
I'm currently working on a Program, that presses buttons for me. I'm working on WPF but I already finished my design in XAML, now I need some C# code.
I have a TextBox that should handle all the SendKeys input. I want to extend it's functionality by providing some CMD-like arguments. The problem is, I don't know how. ;A; For example:
W{hold:500}{wait:100}{ENTER}
This is a example line that I'd enter in the textbox. I need 2 new functions, hold and wait.
The hold function presses and holds the previous key for the specified time (500 ms) and then releases the button.
The wait function waits the specified time (100ms).
I know I could somehow manage to create this function but it would end up being not user editable. That's why I need these arguments.
You're trying to 'parse' the text in the text box. The simplest way is to read each character in the text, one by one, and look for '{'. Once found, everything after that up until the '}' is the command. You can then do the same for that extracted command, splitting it at the ':' to get the parameters to the command. Everything not within a '{}' is then a literal key you send.
There are far more sophisticated ways of writing parsers, but for what it sounds like you are doing, the above would be a good first step to get you familiar with processing text.