cant reach to the text field with Watin - c#

im trying to make an automation with Watin and i'm having an issue reaching to a text fill in an HTML body..
when i log into the site i manage to reach the search box and to put input there and even press "enter" when it moves to the second page, i cant reach the input form there
This is my code - first step is working smooth but second isnt.
browser.GoTo("mywebsiteaddress");
browser.TextField(Find.ByName("sysparm_search")).TypeText(ticketNumber.Text);
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
//browser.TextField(Find.ByName("sys_display.sc_task.u_category")).TypeText(ticketNumber.Text);
browser.Element("sc_task.work_notes");
This is the Browser source code when i check it with google chrome
<textarea wrap="soft" onkeypress="" onkeyup="multiModified(this);fieldTyped(this);" onfocus="this.isFocused=true;" autocomplete="off" data-charlimit="false" rows="16" id="sc_task.work_notes" data-length="4000" style="; width:100%; overflow:auto; " name="sc_task.work_notes" onblur="this.isFocused=false;" onchange="this.isFocused=false;multiModified(this)" onkeydown="multiKeyDown(this);;"></textarea>
Thanks all!

It could be a number of things including what you're looking for is in a frame or less likely you're looking for it before it is loaded. If in a frame, you'll need to specify which frame. If it is not loaded yet the easy way to check is by putting in a generic sleep() call, though that is not the best long term.
When I deal with something I can't find, I make heavy use of the Flash() method. In your case, you'd probably want to start at the whole page level and work your way down to your object. Flash() will show you where you're looking at to make sure you're looking in the right spot on the page, ideally getting down to the parent element of what you're looking for and being able to correctly identify and flash that and then figure out what is amiss with trying to get the textarea you're really trying to get at.

Use Fire fox. Install an add on by the name Firebug.
Whichever element that you want to inspect, you right click on it and say inspect element with fire bug.
once you know the id or name of that element, you can easily access it using the Find class.
Sometimes it so happens that the element is in a different frame than the main frame. watin cannot directly access that element if it is not accessed via the frame.

Related

Differences between .SendKeys(Keys.Enter) and .Click()?

I am working with some automation scripts using C# and Selenium, running my tests on IE11. I was trying to click on a link using the .Click() function from WebElement and it wasn't working at all so I tried to use the .SendKeys(Keys.Enter) function and it worked.
I'm trying to understand this behavior but I can't figure out why is this happening.
Does anyone know the difference between using .Click() and .SendKeys(Keys.Enter)?
From the documentation
Click()
...If this causes a new page to load, this method will attempt to block until the page has loaded. At this point, you should discard all references to this element and any further operations performed on this element will throw a StaleElementReferenceException...
...There are some preconditions for an element to be clicked. The element must be visible and it must have a height and width greater then 0.
SendKeys()
Use this method to simulate typing into an element, which may set its value.
Both simulate user action on the web element. But while SendKeys() tries to perform the action without consideration to anything else Click() has some safe guards, like "stopping" the code from continue until page load event is received. Also, the method won't try to execute without meeting some preconditions to avoid unexpected behavior.
Calling .SendKeys(Keys.Enter) is not a realistic way to write your test cases, as a human user wouldn't be able to do so in most cases, and even if he is, he would usually prefer to click.
Thus, you should try to alter your locators for .Click() to work. MSIE is a little picky sometimes when it comes to clickable elements. For instance, I had an HTML snippet like this:
<button type="button" onclick="executeSomeJavaScript()">
<span>
<span>Abmelden</span>
</span>
</button>
When I tried to call
driver.FindElement(By.XPath("//span[.='Abmelden']").Click();
it didn't work in MSIE. After altering the locator to
driver.FindElement(By.XPath("//span[.='Abmelden']/ancestor::button").Click();
everything was fine, as the button is the clickable element, not the span.
In other browsers, this usually doesn't matter, as they simply generate the click into the X and Y coordinates where the element's bounds rect has its horizontal and vertical center.

Ranorex test automation issue: Unable to reliably click a button on silverlight web app

We have automated a few test cases using the Ranorex automation framework for a Silverlight web application. These test cases involve clicking buttons in order to invoke certain messages on the screen. In order to grab the button on the screen, we first create an Ranorex button object and then point it to the appropriate element using Ranorexpath. Then, we use the RanorexButton.Click() event to click the button. However, this event is unreliable. It works sometimes and at other times the button is not clicked. When the button is not clicked, we have to run the test case again from the start. What are we doing wrong? If this is a known problem of ranorex, please suggest workarounds.
I was facing the same problem but I am able to resolve the problem by introducing a Validate.Exists(infoObject) just before the click. Please make sure that you pass infoObject of your button or any element in Validate.Exists API.
Example:
Validate.Exists(repo.MyApp.LoginBtnInfo);
var button = repo.MyApp.LoginBtn;
button.Click();
With regards,
Avinash Nigam
I haven't heard about such a problem with Ranorex yet, maybe this is just a timing issue.
You could add a Validate.Exists(yourButton) right before the click, this ensures that the click is performed after the button was successfully loaded.
If it is a WebElement you could also use the PerformClick() method instead of the normal Click() method.
There are also different methods which will ensure that the button is in the visible area and has focus, like the EnsureVisible() or the Focus() method.
You will find the available methods of the used adapter in the online API of Ranorex.
If the Button is not within the area you can see without scrolling, you can use a
var button = repo.Buttons.button1;
button.EnsureVisible();
button.Click();
In this way the button is forced to be watched.
It might as well be an issue with the xpath and element Id-s.
If you have changing element Id-s even when the page is navigated away from and moved back (for example we have this issue with SAP related components) you might need to make a more robust xPath path variable using regular expressions.
Try to find object and parts of the path that do not change (eg. "iFrame id="MainContent"" or "btn id="ID_XXXX_Search_Button"") - ofcourse this will only help if the issue is within this.
Ranorex Regular Expression info can be found here: http://www.ranorex.com/support/user-guide-20/ranorexpath.html#c3294
A quick example of what I'm talking about:
Let's say we have an input field that has a changing ID in it's name:
US_EL.ID_F2B49DB60EE1B68131BD662A21B3432B:V_MAIN._046A-r
And I know that the part in the Id that doesn't change is:
:V_MAIN._046A-r
I can create a path for this searching the element by the ending of the elements' id that doesn't change using regular expression:
/dom[#domain='test.example.com']//iframe[#'identifier']//iframe[#'identifier2']//input[#id**~'^**:V_MAIN._046A-r']
The bold part will specify to search for an input element with an Id that ends with ":V_MAIN._046A-r".
An issue that might arrise from this is if you have elements using partially the same names you might get multiple elements returned for the same path. So it's wise to add a few more certain points to the path (eg. "iframe[#'identifier2']") when this issue arrises.

JS/C# how do I know the elment id or xpath I just clicked or manipulated?

As you know, Selenium IDE can show you a script and tell you what element you just manipulated.
I don't know the what happened behind this. Every automation framework can know what element you are clicked, inputed, or selected..
I want to have a console output and display every element I just manipulated in browser.
How can I implement this?
Using jQuery:
$("*").click(function(e) {
console.log(this);
}.blur(function(e) {
console.log(this);
};
Just pop this on your page somewhere and it'll log the element that was manipulated whenever it's clicked or selected. I'm sure there are other ways of manipulation, but the above is a start. It would be trivial to add more handlers--.change() and .hover() come to mind.

Popup on mouse hover the word

I am doing a website using asp.net C# and I would like to popup a small window with information as soon as mouse hover a particular word. I know that I have to use jquery but I don't know exactly how to do it.
Any suggestions please?
There are many plugins out there that will help you achieve what you are looking for. However it is also very possible to implement this functionality yourself. I wouldn't be surprised either if some of the plugins you come across also use similar code.
The following is my attempt to demystify tooltip/popup plugin behaviour.
You could wrap the desired word in a <span> element and give it a .hover class.
<div>
This is some text with a <span class="hover">special</span>
word that has hovercraft capabilities.
</div>
Your jQuery (ver 1.7+) would look something like this :
$(".hover").on('mouseenter',function(){
// The popup must be shown here (mouse is over element).
}).on('mouseleave',function(){
// The popup must be hidden here (mouse has left element).
});
I should add here that I am using a great and yet sometimes forgotten capability of jQuery called "chaining". The on() function actually returns the object that it was attached to. In this case $(".hover") - so if I want to call another function on that object I can just add it as another function at the end. Another example of this would be :
$("#myElement").text("An error has occured!").css("color","#FF0000");
That line of code would also at the text to #myElement and also turn the colour red.
With regard to your actual popup - I would suggest two things :
Have an element at the bottom of your markup (written last so highest index - or manually set the highest z-index)
You could also have the popup in a hidden element right next to the element that is supposed to trigger the popup.
What you're after sounds like a 'tool tip'.
The solutions using jQuery are somewhat involved - so I'll just direct you to external resources.
Possible solutions:
ToolTip Plugin for jQuery
Build a Better Tooltip with jQuery Awesomeness

Test if an element is focused using Selenium Webdriver

I'm really surprised I can't find references on the internet to testing for element focus using Selenium Webdriver.
I'm wanting to check when when a form submission is attempted with a mandatory field missed, focus is moved to the empty field. But I cannot see any way to do this using the WebDriver API.
I will be able to find the focused element using a JavascriptExecutor. But reading the FAQ makes me think there must be some way to perform the check using the driver itself.
Thanks for any help.
driver.switchTo().activeElement() will return the currently focused WebElement. Equality is well defined for WebElement, so you can call element.equals(driver.switchTo().activeElement()).
Calling the slightly misleading named driver.switchTo().activeElement() does not in fact switch focus, neither does driver.findElement(), so you do not need to switchTo().defaultContent() after; in fact, doing so would probably blur the current element.
driver.switchTo().activeElement();
returns the currently focused element.
Makes sure you switch back after using
driver.switchTo().defaultContent();
Also if nothing is focused the body of the document is returned.
Take a look at this question as well.
In Selenium how do I find the "Current" object
You can find the active element using selector 'dom=document.activeElement'. Then you can assert whether it's the element you want it to be focused or not.
The WebDriver is supposed to change focus when you use Driver.FindElement calls. So you're last element in the Driver context is active.
NOTE: This breaks for any elements injected dynamic (e.g. jQuery), so you'd need to go the script route then.
#danielwagner-hall The boolean bb = driver.switchTo().activeElement().equals(driver.findElement(By.id("widget_113_si‌​gnup_username"))); will always pass but it doesn't prove that the element is brought into focus if the element is out of view.
NB: Unable to comment as not enough reputation points.
One way of approaching this could be to use webElement.getLocation().getX(); getY() methods and reference the coordinates on the page and verify its focus.

Categories

Resources