Java Selenium Firefox Driver - Textbox Onchange Issue - java

Running Java/Selenium 2.3 (and 4) using Firefox Driver on centos
Trying to test against a site that has form with a text type, with an onchange. Tried to insert the text using the sendkeys, and then changing focus by doing a select/click on another term in the form. Tried to do a fireevent as well. (Doesn't appear to be supported in the 2.3 sel)
Searched the net as well with no luck.
Basically, trying to get a solution to how to do an insert into a textbox for selenium/firefox driver, so the inserted text actually appears in the textarea, which means the onchange event gets fired.
The test html is::
<td rowspan='4' nowrap='nowrap' valign='top' align='left'>
<DIV id='win0divCLASS_SRCH_WRK2_SUBJECT$69$'><input type='text' name='CLASS_SRCH_WRK2_SUBJECT$69$' id='CLASS_SRCH_WRK2_SUBJECT$69$' tabindex='31' value="" class='PSEDITBOX' style='width:60px; ' maxlength='8' onchange="addchg_win0(this);oChange_win0=this;" />
</DIV></td>
The test code is::
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("ACG");
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("");
Select sCourse= new Select(driver.findElement(By.id("CLASS_SRCH_WRK2_ACAD_CAREER")));
sCourse.selectByValue("");
The test sets the textelement, and then sets the select/option of a select item, which should trigger the change in focus. I also tried to clear, and reset the text, thinking that might trigger the onchange..
A solution to this would help a lot of people who've been looking for the same thing!!
Thanks

Have you tried doing a tab after sendKeys?
You can do a
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$"))
.sendKeys("\t");

I had a similar problem (though my trigger was onBlur). I called the blur() method directly. In your case it would be
driver.findElement(By.name("CLASS_SRCH_WRK2_SUBJECT$69$")).sendKeys("ACG");
((JavascriptExecutor)driver).executeScript( "$('[name=\"CLASS_SRCH_WRK2_SUBJECT$69$\"]' ).blur() );
You might need a different function other than blur().

Related

Sendkeys not working on search inputfield

I am trying to access an input field with a dynamically changing ID, and using other ways to find the element doesn't seem to work either.
This is the input field:
<input data-v-44dd203d="" type="search" placeholder="Search users" class="form-control form-control-md" inputmode="search" id="__BVID__27">
This is my current code in Java:
driver.get("website");
driver.findElement(By.xpath("//input[#type='search']")).sendKeys("name");
I am not getting any error, but when i am running the code, the website opens but nothing happens to the field. Seems i cannot access it, if it makes sense.
I had similar issue once and adding a click to the element first and then send keys worked for me.

Focus and click on a GWT dropdown using Selenium

I am working with a HTML select (i.e. dropdown) in a web page that initially loads with no value. It loads the values only when an onfocus JS event is executed on it. Note that it is a GWT control.
Here is the DOM rendering of the dropdown. Note the onfocus event that actually loads the values in the dropdown:
<select id="Field1" class="select not_disabled" onchange="clearOperators(Operator1); addOperators(Field1,Operator1); adjustInput(Field1, 1);" onfocus="loadValues(Field1);" style="width:20em;" name="Field1">
</select>
The problem is that the click on the element is not getting registered unless the window is in focus. And hence the values are not loading.
I have this in my code, and this executes with no effect:
// bring the focus on the dropdown
((JavascriptExecutor) webDriver).executeScript("return document.getElementById('Field1').focus;", dropdownElement);
// click on it to load the values
dropdownElement.click();
Even tried the old way of firing an event on the element. Did not work:
JavascriptLibrary javascript = new JavascriptLibrary();
javascript.callEmbeddedSelenium(webDriver, "triggerEvent", dropdownElement, "blur");
I also tried out some of these suggestions, but none helped:
Correct way to focus an element in Selenium WebDriver using Java
https://groups.google.com/forum/#!msg/selenium-users/jk59XG2TQk8/xbaskp9uFnQJ
Could someone suggest what I can do to load the values in the dropdown? I am using Java with Selenium 2 on Firefox 47.
I found a way to get this working. Although it's not a very desirable solution for UI tests, since it does not mimic a "real user" behavior, it does work fine.
I am invoking the onfocus function from within my test class and that does the loading of values.
(String) ((JavascriptExecutor) webDriver).executeScript("return loadValues(Field1);", dropdownElement);
Further, I added this preference to the Firefox profile:
profile.setPreference("focusmanager.testmode",true);
Hope it helps someone.

How to click on an element using its Id and Value attributes together?

I am trying to click on some button (which becomes enabled after all of the fields are fill in):
<div class="savCancelContainer">
<input type="button"
value="Save"
translatekey="ACTVITY_DETAILS_SAVE_BUTTON"
class="translate" id="submitActivityDetails"
style="background-color: rgb(0, 125, 195);">
The programmers of the web-page have changed it for some reason, and now my code is no longer working correctly (the button doesn't get clicked on):
driver.findElement(By.id("submitActivityDetails")).click();
I also tried finding it by xpath, with no success.
Is there any way to click the button using the Id and Value attributes together?
Any other ideas?
Similar pages and dialogs are still working fine...
You need to create a xpath which will contain both the attribute:
//input[#id='submitActivityDetails'][#value='Save']
And Click event can be triggered in the following way:
driver.findElement(By.xpath("//input[#id='submitActivityDetails'][#value='Save']")).click();
Lemme know if it helps!
Additionally you can use css seelctor to perform that action too.
[id='submitActivityDetails'][value='Save']

Interrupting a file upload via Selenium using Java?

I'm trying to interrupt a file upload which doesn't seem possible. It seems that nothing gets executed beyond elem.sendKeys("filename.txt") until the entire file is uploaded. Further, none of the buttons are clickable although available when checked via Firebug or testing manually.
Automating the file upload and the interrupt by clicking a cancel file upload button (which is clickable when doing the test manually) leads to a test failure with the following exception: ElementNotVisibleException: Element is not currently visible and so may not be interacted with.
Is there any way I can enforce the interrupt? are there any other means that aids doing the same exact thing?. I'm using Firefox for this test.
HTML:
<div style="padding-left: 40px;">
<input id="upload" type="file" multiple="" label="File" name="upload[]" size="50">
</div>
Java:
WebElement elem = driver.findElement(By.id("upload"));
elem.sendKeys("filename.txt");
driver.navigate().refresh();
System.out.println("hi");
I have failed to see a point in doing that, but here we go:
I believe u can't stop the elem.sendKeys() method in Selenium(which does the actual upload).
Using it on a input text to set a string is the same as actually uploading a file. It's just Selenium trying to set some values on your input.
One way around would be clicking on a cancel upload button you have implemented.
Since you have tried that, this exception ElementNotVisibleException: Element is not currently visible and so may not be interacted with means exactly the that element exists (Selenium have found it on the DOM) but its not visible (the element has some properties like overflow: hidden display:none etc)
My best guess is that you should work that out and try clicking your button that implements an action of stopping the upload.

Webdriver Backed Selenium, missing confirmation dialog

I have a problem related to some strange behavior when trying to test confirmation popups using Webdriver Backed Selenium in JUnit.
I have a following chunk of code in my test case:
assertTrue(isElementPresent(By.xpath("//input[#title='Delete Site']")));
selenium().click("//input[#title='Delete Site']");
//assertTrue(selenium().isConfirmationPresent());
//assertEquals(selenium().getConfirmation(), "Are you sure want to delete this item?");
selenium().waitForPageToLoad("30000");
The above lines refer to the following element:
<input onclick="return confirm('Are you sure want to delete this item?');" value="Delete Site" type="image" title="Delete Site" src="/workthru-web/resources/images/delete.png" class="image" alt="Delete Site">
However, the test fails on the commented lines. When I do the same manually, a prompt appears as expected. Is it possible that popup windows are disabled when running WebDriver? I create my driver using new FirefoxDriver(), no extra params present. Do I need extra configuration? Or am I missing something else here?

Categories

Resources