WebElement.clear() fires javascript change event - Alternatives? - java

I use selenium IDE to initially record the tests and save them as Java WebDriver tests.
When I go into an input field, delete all the text and enter a new value, it records that as 2 commands:
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("johnnyleitrim");
One problem with this for me is that the clear() event fires a Javascript change event for the "username" field. This does not happen when I use the browser itself - it waits until the field loses focus before firing the change javascript event, and that's what I want to emulate in Selenium.
The reason I need this is that I do validation on the change() event, and when change is called with an empty value, it displays an alert telling the user the information is invalid - and this alert stops Selenium
So how do I clear the field without using WebElement.clear()?

You can avoid using the clear() method and use the Actions class to clear and set text in one go, therefore firing the onchange() event only once the text is set.
Call the below method like:
ClearAndSetText(By.id("username"),"johnnyleitrim");
The method clicks the element, selects the existing text using shift+home keys,clears using backspace, and then types in the new text - just like how a user would do.
public void ClearAndSetText(By by, string text)
{
WebElement element = driver.findElement(by);
Actions navigator = new Actions(driver);
navigator.click(element)
.sendKeys(Keys.END)
.keyDown(Keys.SHIFT)
.sendKeys(Keys.HOME)
.keyUp(Keys.SHIFT)
.sendKeys(Keys.BACK_SPACE)
.sendKeys(text)
.perform();
}

You can try it using JavaScriptExecutor (although I haven't tested it).
JavaScriptExecutor js = (JavaScriptExecutor) driver;
js.executeScript("document.querySelector(\"input[id='username']\").value = ''");

Seems like it's a known Selenium bug. There were a few options as workarounds mentioned on the bug page, but they all meant having to "heavily" modify the code returned from Selenium IDE. Instead, I decided to create a Proxy which would do the work for me without too much modification to the IDE generated code:
protected WebElement findElement(By criteria) {
try {
WebElementHandler webElementHander = new WebElementHandler(seleniumWebDriver.findElement(criteria));
return (WebElement) Proxy.newProxyInstance(getClass().getClassLoader(), new Class[]{WebElement.class}, webElementHander);
} catch (NoSuchElementException e) {
logger.error("Could not find " + criteria + " on page " + seleniumWebDriver.getCurrentUrl());
throw e;
}
}
private class WebElementHandler implements InvocationHandler {
private WebElement proxiedElement;
private WebElementHandler(WebElement proxiedElement) {
this.proxiedElement = proxiedElement;
}
#Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
if (method.getName().equals("clear")) {
Keys[] keys = new Keys[proxiedElement.getAttribute("value").length()];
for (int i = 0; i < keys.length; i++)
keys[i] = Keys.BACK_SPACE;
proxiedElement.sendKeys(Keys.chord(keys));
return null;
}
return method.invoke(proxiedElement, args);
}
}

Related

Selenium hover code works in Chrome not Edge

public void Hover()
{
Actions action = new Actions(BrowserWindow.Instance.Driver);
action.MoveToElement(WebElement).Perform();
}
This is working in Chrome. Not Edge. I have confirmed with the developer that I am "hovering" over the correct element.
WebElement elem = yourWebDriverInstance.findElement(By.xpath("//*[#class='goog-menu goog-menu-vertical uploadmenu density-tiny']/input"));
String js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible';";
((JavascriptExecutor) yourWebDriverInstance).executeScript(js, elem);
Which also failed to work. Anyone have any idea what I am doing wrong?
More info.
This is also failing on Firefox. I saw an article about out of date selenium drivers. I have JUST installed both geckodriver and set the Edge driver to auto update according to the documentation. I do not believe I have out of date drivers.
More info take 2
Calling code is
public static void DoCloseActiveTabEntire()
{
Element tab = new Element(byTab);
tab.Hover();
// CLose button is not clickable. Cannot use standard BUTTON for find
Button close = new Button(byClosePanelButton);
close.Click();
}
If I set a break point at button close... after the hover attempt, I notice that moving my mouse over the "tab" also does not cause the button to be visible.
This is weird. But replace
action.MoveToElement(WebElement).Perform();
with
action.MoveToElement(WebElement).Build().Perform();
And it works. I read that the Build is built into Perform. But I was kinda just smacking at it hoping something fell out. And it worked.
perform()
perform() is the convenience method for performing the actions without calling build() first.
build()
build() generates a composite action containing all actions so far, ready to be performed and additionally also resets the internal builder state, so subsequent calls to build() will contain fresh sequences.
This usecase
In your usecase, you have invovoked perform() just after moveToElement(WebElement) without generating the composite action to be performed using build().
Solution
A straight forward solution would be to invoke build() before perform() as follows:
public void Hover()
{
Actions action = new Actions(BrowserWindow.Instance.Driver);
action.moveToElement(WebElement).build().perform();
}
So, I don't know why I thought the build().perform() did the job. I know it worked ONCE. What I wound up doing is keeping the hover code the same.
public void Hover()
{
Actions action = new Actions(BrowserWindow.Instance.Driver);
action.MoveToElement(WebElement).Build().Perform();
Thread.Sleep(1000);
}
I changed the calling code which attempts to close the panel/page whatever I want to call it to this:
public static void DoCloseActiveTabEntire()
{
// So there is a defect in EDGE whereby the behavior of the code containted in the hover on the tab executes without
// error but the action underneath does not occur. So in Edge, callng the hover method of the TAB as seen in the else condition
// below does nto display the CLOSE button which needs to be clicked.
// So for Edge, javascript is used to display the button directly.
IWebElement close;
if (BrowserWindow.Instance.Browser == BrowserWindow.Browsers.Edge)
{
close = BrowserWindow.Instance.Driver.FindElement(byClosePanelButton);
string js = "arguments[0].style.height='auto'; arguments[0].style.visibility='visible'; arguments[0].style.display='inline';";
IWebDriver driver = BrowserWindow.Instance.Driver;
IWebElement element = close;
((IJavaScriptExecutor)driver).ExecuteScript(js, element);
}
else
{
Element tab = new Element(byTab);
tab.Hover();
close = new Button(byClosePanelButton).WebElement;
}
close.Click();
}
For me, I am happy that the thing can be closed. I don't care that much whether the hover achieves it.

Advanced Stale elements

I have been reading about stale elements and am still a bit confused. For instance, the following won't work, correct?
public void clickFoo(WebElement ele) {
try {
ele.click();
} catch (StaleElementReferenceException ex) {
ele.click();
}
}
because if ele is stale, it will remain stale. The best thing is to redo the driver.findElement(By), but as you can see in this example, there is no xpath. You can attempt to ele.getAttribute("id") and use that, but if the element has no id, this also will not work. All methods calling this would have to put the try/catch around it, which may not be feasible.
Is there some other way the element could be refound? Also, assuming there is an id, would the id remain the same after the element goes stale? What in the WebElement object ele is different once it goes stale?
(Java Eclipse)
I would recommend you NOT create a method like the above. There's no need to add another function layer on top of .click(). Just call .click() on the element itself.
driver.findElement(By.id("test-id")).click();
or
WebElement e = driver.findElement(By.id("test-id"));
e.click();
One way that I use regularly to avoid stale elements is find the element only when you need it and generally I do this by in a page object method. Here's a quick example.
The page object for a home page.
public class HomePage
{
private WebDriver driver;
public WebElement staleElement;
private By waitForLocator = By.id("sampleId");
// please put the variable declarations in alphabetical order
private By sampleElementLocator = By.id("sampleId");
public HomePage(WebDriver driver)
{
this.driver = driver;
// wait for page to finish loading
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(waitForLocator));
// see if we're on the right page
if (!driver.getCurrentUrl().contains("samplePage.jsp"))
{
throw new IllegalStateException("This is not the XXXX Sample page. Current URL: " + driver.getCurrentUrl());
}
}
public void clickSampleElement()
{
// sample method code goes here
driver.findElement(sampleElementLocator).click();
}
}
To use it
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://www.example.com");
HomePage homePage = new HomePage(driver);
homePage.clickSampleElement();
// do stuff that changes the page and makes the element stale
homePage.clickSampleElement();
Now I no longer have to rely on an old reference. I just call the method again and it does all the work for me.
There are many references on the page object model. Here's one from the Selenium wiki. http://www.seleniumhq.org/docs/06_test_design_considerations.jsp#page-object-design-pattern
If you want to read more info on what a stale element is, the docs have a good explanation. http://docs.seleniumhq.org/exceptions/stale_element_reference.jsp

How do I extract the recognition strategy of a Selenium Web Element during run time?

The problem I have is that my Page Object class finds all of the objects on the screen and then sends it to a more generic method to do the actual data entry. During this data entry process, some objects become stale and I get the "StaleElementException".
My plan is to catch that exception and attempt to re-find the element again.
Is there a way of extract the selection strategy from the runtime WebElement object other than doing a "object.toString()" and then parsing it?
I use page objects by defining the locators at the top of the class, then the constructor that verifies that we're on the right page (etc), and then methods for each action available on the page. Here's a simple example for the Google search page.
GoogleSearchPage.java
public class GoogleSearchPage
{
private WebDriver driver;
private By waitForLocator = By.id("lst-ib"); // optional
private By searchBoxLocator = By.id("lst-ib");
private By searchButtonLocator = By.cssSelector("button[name='btnG']");
private By feelingLuckyButtonLocator = By.id("gbqfbb");
public GoogleSearchPage(WebDriver webDriver)
{
driver = webDriver;
// wait for page to finish loading
new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(waitForLocator));
// see if we're on the right page
if (!driver.getCurrentUrl().contains("https://www.google.com"))
{
throw new IllegalStateException("This is not the Google search page. Current URL: " + driver.getCurrentUrl());
}
}
public void doSearch(String searchString)
{
driver.findElement(searchBoxLocator).sendKeys(searchString);
driver.findElement(searchButtonLocator).click();
}
}
GoogleSearchTest.java
public class GoogleSearchTest
{
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
GoogleSearchPage googleSearchPage = new GoogleSearchPage(driver);
googleSearchPage.doSearch("selenium");
System.out.println(driver.getCurrentUrl().contains("#q=selenium"));
}
}
This is obviously a super simple example but it shows one good way to create page objects that should significantly reduce the frequency of StaleElementExceptions and, in some cases, speed up your script execution because you only scrape what you need and move on.
Reduce the urge to provide a getX() and clickX() method for each and every element on the page. Instead favor task based methods. Ask yourself what tasks a user is going to want to accomplish on the page and provide methods to do those tasks. It will keep your page object API much cleaner and more clear in what it provides to the consumer (you and other script writers).

How to convert webelement to string in selenium using java? Please refers to detail section for more info

I created a POM for 'Create project' page
public static class addProjInfo_container
{
public static WebElement ProjName_txt(WebDriver driver)
{
element=driver.findElement(By.xpath("//label[text()='Project Name']/following-sibling::input"));
return element;
}
// and so on for every text field for adding project...
And I created a TestUtility class with method for waitForElement as show below
public final class TestUtility {
private static WebDriver driver;
public static void waitforElementXpath(final WebDriver driver,final int waitTime,final String xp)
{
WebDriverWait wait =new WebDriverWait(driver, waitTime);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(xp)));
}
}
Now, in Test script I want to avoid using Thread.sleep() to wait for webelement to be ready to start performing actions.
so, I use
TestUtility.waitforElementXpath(driver,10,CreateProject_Page.addProjInfo_container.projName_txt(driver));
But,it displays error as
The method waitforElementXpath(WebDriver, int, String) in the type TestUtility is not applicable for the arguments (WebDriver, int, WebElement)
Kindly, let me know how to handle the issue.
Basically your want to reverse the By to get its string and you are using xpath
so change to this which return the String instead of WebElement
public static class addProjInfo_container {
public static String projName_txt(WebDriver driver) {
By by = By.xpath("//label[text()='Project Name']/following-sibling::input");
driver.findElement(by);
return getSelectorAsString(by);
}
public static String getSelectorAsString(By by) {
String str = by.toString();
return str.substring(str.indexOf(" ") , str.length());
}
// and so on for every text field for adding project...
}
hope this could help
This is really a convoluted way of trying to accomplish this task. Your ProjName_txt() method already has found the element because that's what it returns so you don't need to wait for it to appear by using waitforElementXpath(). I would recommend that you read some articles on OOP and classes before you write too much more code.
The best way is:
String name = driver.findElementByClassName("classnamesample").getText() ;
Just add .getText() in the last of xpath and receive this as String.

Mockito stubbing void methods

I am currently writing unit tests for my selenium project and I am using Mockito to mock up my webelements and drivers.
The problem I am having is that I have a function that is used to change the radio option in a list of radio buttons but I am having a problem with this. the code looks like this:
#Test
public void testChangeRadioState(){
WebElement mockElement = mock(WebElement.class);
List<WebElement> mockElementList = new ArrayList<>();
WebElement selectedMockElement = mock(WebElement.class);
/*The when statements*/
when(selectedMockElement.isSelected()).thenReturn(true);
doReturn(when(mockElement.isSelected()).thenReturn(true)).when(mockElement).click();
doReturn(when(selectedMockElement.isSelected()).thenReturn(false)).when(mockElement).click();
/*Add a selected and a none selected element to the list*/
mockElementList.add(mockElement);
mockElementList.add(selectedMockElement);
/*The method that is beeing tested*/
elementSetter.changeRadioState(mockElementList);
Assert.assertTrue("The radio state was not selected",mockElement.isSelected());
}
What I am trying to do int he doReturn part is to tell the element "mockElement" that when it recieves a click it should allways return true on a isSelected() call. but since Click() is a void function it won't let me do that. Anybody know a way around this?
Ok, it is separate topic - what you are testing and would I mock things so deep.
I would just rewrite test like this:
#Test
public void testChangeRadioState() {
WebElement mockElement = mock(WebElement.class);
WebElement selectedMockElement = mock(WebElement.class);
List<WebElement> mockElementList = new ArrayList<>();
/*The when statements*/
when(selectedMockElement.isSelected()).thenReturn(true);
// By default mockito will return false but maybe I want to highlight
// that this is important
when(mockElement.isSelected()).thenReturn(false);
/*Add a selected and a none selected element to the list*/
mockElementList.add(mockElement);
mockElementList.add(selectedMockElement);
/*The method that is beeing tested*/
elementSetter.changeRadioState(mockElementList);
verify(selectedMockElement).click();
// according to test method name I would add
// one more verification that something was dis-selected
}
Another variant with state which I think has unnecessary mocks:
boolean selected;
#Test
public void testChangeRadioState() {
selected = false;
WebElement mockElement = mock(WebElement.class);
WebElement selectedMockElement = mock(WebElement.class);
List<WebElement> mockElementList = new ArrayList<>();
/*The when statements*/
when(selectedMockElement.isSelected()).thenReturn(true);
doAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
selected = true;
return null;
}
}).when(mockElement).click();
/*Add a selected and a none selected element to the list*/
mockElementList.add(mockElement);
mockElementList.add(selectedMockElement);
/*The method that is beeing tested*/
elementSetter.changeRadioState(mockElementList);
Assert.assertTrue("The radio state was not selected", selected);
// according to test method name I would add
// one more verification that something was dis-selected
}
But again there is misleading in names. For example I would expect that there are elements which don't become selected when they clicked. Question again about what you are testing

Categories