empty field validation still fires if the fields are not empty in Selenium + Specflow - selenium

When I fill the fields with values and submit, empty field validation still fires randomly and not everytime. As you can see in the image, the fields are non empty , still validations are fired.
I have noticed, usually selenium takes time to enter details, but in my case, as soon as the page loads, it quickly starts filling the fields speedily. Its unusually fast.
I am using Selenium , specflow framework and page object pattern and page factory.
My code for entering the text field is below :
public static void FillTextBox(IWebElement webElement,string value)
{
webElement.Click();
webElement.Clear();
webElement.SendKeys(value);
}
Please help me with this.

If you look up the definition of the onBlur event, you will find:
The onblur event occurs when an object loses focus.
So you have to shift focus to another element after you fill in each text box.
public static void FillTextBox(IWebElement textField, string value, IWebElement otherElement) {
textField.SendKeys(value);
otherElement.Click(); // this will have to be some other element on your page
}

Related

Visibility of element- selenium webdriver

My script creates a new article: fills few fields and click "Submit button" at end of page.
I have written Click() function in util class like :
public void click(String xpathKey)
{
WebElement myDynamicElement = (new WebDriverWait(driver, 60))
.until(ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(prop.getProperty(xpathKey))));
try
{
myDynamicElement.click();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
}
Waiting for visibility of element means it will wait until element will be visible on the page or on screen? My script clicks on submit button while it's not exactly visible on the page.
I am running this script since months and it's running perfectly fine. Suddenly it started giving error element is not clickable at point(213, 415). It never appeared before. Anyone has an idea, why it could have happened?
I have done many cases, where the element is not exactly visible, generally button at end of page. selenium does not scroll itself, it finds the element and performs operation.
Try this.
public void click(String xpathKey)
{
WebElement myDynamicElement = (new WebDriverWait(driver, 60))
.until(ExpectedConditions.visibilityOfElementLocated(
By.cssSelector(prop.getProperty(xpathKey))));
try
{
Actions act = new Actions(driver);
act.moveToElement(myDynamicElement);
act.click();
act.build().perform();
}
catch (Exception e)
{
e.printStackTrace();
throw e;
}
}
The error message you are getting indicates that there is an element covering the element you are trying to click. If you read the error message carefully, it will tell you the HTML of the blocking element and that will help you find it on the page. Without more info, it's hard to tell exactly what the situation is but there are a few common causes.
You may have just closed a dialog/popup and it's not quite completely gone before you try to click. In this case, wait for some element that's part of the dialog to be invisible generally solves this problem.
There may be some wait/loading/spinner control that appears and disappears but you are clicking before it's completely gone. The solution is the same as #1, wait for some element that's part of the spinner to be invisible.
There may be some UI element like a header, footer, sidebar that is floating that covers the element you are trying to click if it's at the very bottom/top/etc of the page. These can be a real pain because you never know when the elements are going to align and be covered. One solution is to use JS to scroll the page a little more. For example, if you script is at the top of the page and you want to click something at the bottom. Doing a click will scroll the page to show that element but that puts the element at the very bottom of the page and under a floating footer. You try to click but catch the exception. Since you know you're at the bottom of the page, you scroll the page downwards by X pixels. This brings your desired element out from behind the floating footer and now you can click it.
NOTE: If you are going to click an element right after waiting for it, you should use .elementToBeClickable(locator) instead of .visibilityOfElementLocated(). It won't solve your current problem but it's a more complete and proper wait for what you are wanting to do.

WebDriver – Inconsistent Action of Method to Check an Element is Visible

I have a web page that uses a DatePicker control to enter a DoB as one of the criteria for a search. The issue I have had to resolve is that the datepicker hides the button to execute the Search. To get rid of the datepicker I click on another field, and then use a method that should wait until the Search button is visible and click it. My issue is that sometime this works, but more often it does not and I get the Element is not clickable .. Other element would receive the click exception. The calling code is below.
WebDriverSupport.ClearAndEnterDataIntoFieldById(driver, "txtDateOfBirthNew", "01/01/1980"); // causes the datepicker to appear
WebDriverSupport.ClickElementById(driver, "txtSurnameNew"); // to close date picker click on another field
WebDriverSupport.ClickElementById(driver, "btnSearch"); // wait for the search button to appear and click
The ClickElementById method is defined as,
/// <summary>
/// WebDriver: Clicks on an element
/// </summary>
/// <param name="driver">WebDriver object</param>
/// <param name="id">Name of element (by ID) to click</param>
/// <returns>None</returns>
public static void ClickElementById(IWebDriver driver, string id)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
element.Click();
}
If I run in debug mode and put a breakpoint on the statement that clicks on the Search button, and then continue from there it works every time. But I guess this is because there is time for the datepicker to disappear and the button to become visible.
What I cannot figure out is the ClickElementById method is waiting for the element to be visible before it moves onto doing the click, so why the exception? And why it works some times and not others.
Any advice on resolving this issue? Is there a more robust method for checking an element is visible and can be clicked on?
ElementIsVisible checks if at least part of the element is displayed. However, it's not always good enough for the click() method. Try using MoveToElement from Actions class
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id(id)));
Actions actions = new Actions(driver);
actions.MoveToElement(element).Perform();
element.Click();

showandwait changing variable value

I'm having a problem with a variable I'm using to track the status of a user activity. In a GUI I have a button that, on clicking the button launches a second GUI. In that GUI, the user can either complete the activity started in the first GUI or not.
If the user cancels the second GUI, then the idea is to go back to the first GUI, leaving all variables and lists with their current values. If the second GUI completes the activity of the first GUI, then all variables and lists should be reset.
To track this, I have a variable (Boolean complete) initially set to FALSE. In the second GUI, when the "OK" button is clicked (rather than the "Cancel" button), the second GUI calls a method in the first GUI, changing the value of "complete" to TRUE.
To see what the heck is going on, I have System.out.println at several points allowing me to see the value of "complete" along the way. What I see is this:
Launching first GUI - complete = FALSE
Launching second GUI - complete = FALSE
Clicking "OK" in second GUI - complete = TRUE
Second GUI closes itself, returning to complete first GUI activity
First GUI finishes activity with complete = FALSE
I'm assuming it is because I am launching the second GUI with a showandwait, and when the method containing the showandwait begins, the value of "complete" = FALSE. The value changes in the WAIT part of show and wait, then the method continues and that is where I get the value still being FALSE, though it was changed to TRUE.
Here is a summary of the code in question (if you need exact code, it's longer, but I can post on request):
completeButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
try {
System.out.println("b4 calc = " + complete); // complete = FALSE
// all the code to create the calcStage
calcStage.showAndWait(); // second GUI, which calls a method in THIS
// class that changes complete to TRUE. That method
// (in THIS file) also has a println that shows the change.
getComplete(); // tried adding this method to check the value of
// "complete" after the change made by the calcStage
// (which calls a method in this same file)
System.out.println("Complete? " + complete);
// this shows complete = FALSE,
// though in the calcStage it was changed to TRUE
if (salecomplete) {
// code that should reset all variables and lists if the activity was completed
}
}
}
}
The question here is why does the second GUI successfully change the value of "complete", but when I return to the first GUI it still sees complete as FALSE? And how can I get around this?
Try having the controller of the second GUI calling a method in the first GUI's controller to modify that complete variable
For example:
// Code to handle the OK button being pressed
#Override
public void handle(ActionEvent t) {
// Do validation and work
//reference to the first controller object
firstController.setComplete(true);
}

retrieving user input from a text field

What is the best way to retrieve user-input text from a text field on Submit button press? I am having issues with the listener, because it doesn't return anything (located in my gui class). I need to somehow retrieve what the user inputs, then return to the main class, and decide from there what to do with the user input. an example code would be nice!
You can attach a listener with your button and override its actionPerformed(). Inside actionPerformed(), you can retrieve the value entered in the text field.
Something like this :
button.addActionListener(new ActionListener (){
public void actionPerformed(ActionEvent evt) {
String text = textField.getText();
}
};)

Dojo OnKeyPress Handler: TextBox value is blank

I have a Dojo form that does not contain a submit button. Instead, I added an onkeypress handler to calls a method when Enter is pressed. The problem I am having is that when I hit enter before blurring off the current field, the _process method thinks that field is empty.
Or in other words: type in field1. hit tab. type in field2. hit enter. field2 is blank unless i click off the field or shift-tab back.
Any ideas?
dojo.connect(dijit.byId("fkrform"),"onKeyPress",function(e) {
if (e.keyCode == dojo.keys.ENTER) {
_process();
}
and the method it calls:
function _process()
{
var field1 = dijit.byId("field1").value;
var field2 = dijit.byId("field2").value;
alert(username);
alert(password);
...do stuff...
}
The fields are of dojoType: dijit.form.TextBox, and the form is: dijit.form.Form
Use dijit.byId('field1').get('value') instead of directly try to access the property "value". In your example you saved the value in the variable field1 and field2 and in the alert you use the variable username and password could be the answer why you don't get anything. But you still should use the get method to get a property instead of directly access the property.
When you press "Enter" your form will submit. So you need to connect to the "onSubmit" event on the form, instead of onkeyPress or onKeyUp.
The first example i created prints the value of the input box on every key someone pressed in the console.
http://jsfiddle.net/a8FHg/
But what you really wanted was hooking into the submit. I modified the example. The new example connects to "onSubmit" and creates an alert box with the text of the user input.
http://jsfiddle.net/a8FHg/1/
For completness if jsfiddle doesn't work some day. You JavaScript should looks like this.
dojo.ready(function(){
var form = dijit.byId('form');
var box = dijit.byId('box');
var submit = function(event) {
dojo.stopEvent(event);
alert("User input was " + box.get('value'));
};
dojo.connect(form, 'onSubmit', submit);
});
Assuming your form in your HTML has the id form and your box have the id box.