I am writing tests for a Java application which has been developed using the Vaadin framework. For the tests I am using Robot Framework. At some steps I have to use robot framework commands like execute javascript.
For example, for finding a component I have to execute the following command:
execute javascript document.getElementById('button-create').click()
Which works without any problem. Primitives like Click Element are not working because Vaadin doesn't wait until the whole page is loaded and therefore some ids are not yet assigned at run time.
Unfortunately this application has been developed in such a way that some components are not reacting to the event click but to the event mousedown. That is, at the Java console of chrome I can perform the following command:
document.getElementsByClassName('table-cell-wrapper')[1].mousedown();
and the action is performed without any problem. So far so good.
Unfortunately when trying to do the same at robot framework
execute javascript document.getElementsByClassName('table-cell-wrapper')[1].mousedown();
I am getting te following error:
Executing JavaScript:
document.getElementsByClassName('table-cell-wrapper')[1].mousedown();
20131029 12:22:12.445 : INFO : </td></tr><tr><td colspan="3"><img src="selenium-screenshot-1.png" width="800px">20131029 12:22:12.453 : FAIL :
WebDriverException: Message: u'document.getElementsByClassName(...)[1].mousedown is not a function' ;
Then the question is... how can I trigger the event mousedown on a given element using Javascript and Webdriver.
My environment is:
RIDE 1.2.1 running on Python 2.7.5.
Robot Framework 2.8.1
Library Selenium2Library
Library Screenshot
Library Dialogs
Library Collections
Thanks in advance.
From Selenium2Library Execute Javascript keyword:
Note that, by default, the code will be executed in the context of the
Selenium object itself, so this will refer to the Selenium object. Use
window to refer to the window of your application, e.g.
window.document.getElementById('foo')
So you might want to put window. in there.
mousedown() is not a pure Javascript function. JQuery does have one and there is a event called mousedown in plain JS also.
If some element does not exists after the page is loaded causing
Click Element foobar
to fail, you can use Wait Until Page Contains Element and then click that element. You could write your own Press Element keyword to make it more usefull:
Press Element ${locator}
Wait Until Page Contains Element ${locator}
Click Element ${locator}
Related
I am using selenium webdriver with Java. 7/10 times my script failed with error:
org.openqa.selenium.ScriptTimeoutException: script timeout
I tried so many option :
increase wait time
tried method jsWaitForPageToLoad and verified js.executeScript(""return document.readyState"").toString().equals("complete")
Wait for element to visible
wait for element to be Clickable
Wait last element to load
but nothing worked.
Workaround : If I manually move my mouse little bit on browser then script work without issue.
As per above workaround, I tried Action class to move mouse also , but its not worked.
Env : Chrome : Version 85.0.4183.83 (Official Build) (64-bit)
Selenium Webdriver : 3.141.59
Application : Developed in Angularjs
I also used ngwebdriver (1.1.5) for angularJs
Any help is highly appreciated
Issue got fixed. The issue was with waitForAngularRequestsToFinish() method of ngDriver.
Once I used as below :
ngDriver.withRootSelector(""root-app"").waitForAngularRequestsToFinish();
It worked well
Your problem is a flaky one because of the front end technology used (angular js).
The best tool to automate agular web app is Protractor.
Check here an explanation
For the project in my school I am creating a automation tool for the web with JAVA.
This tool should detect the user activity on web page, save it, and then run the result as Tests.
I found solution for the running part: I will use Selenium to run all the tests that I am automatically generating.
But I did not found how to detect user activity on the web, Selenium can do it?
The Idea is to check what element the user was clicked, hover, send keys...
There is a way do detect what element was clicked in the browser with pure JAVA? if not, there is some tool to check it?
Look at the Selenium IDE plugin for Firefox. It will help record your usage of a website. You can then reuse the scenario to perform your test.
Since the tests are actually made on the front-end, the tests are not tied to Java. Your back-end could be written in C# or PHP: the front-end user has no idea what's behind.
I have a code in selenium which dynamically clicks on the tabs in menu page. And sometimes happens that tab is not clickable(it is just a plain td with span inside) and when Web Driver tries to click on this tab, my program gets frozen (no exception thrown or anything).
I can't avoid clicking on tabs like that but I would like to somehow prevent that freeze after click. So can I set some timeout or tell to selenium what to do if element is not clickable?
driver.findElement(By.xpath(
format("//span[#class='rf-tab-lbl'][text()='{0}']",
navigation.getGroup()))).click();
//if the tab is currently selected (hence is not clickable) selenium won't click
// on it and program freezes
Please try another page from a different website. It might be a JS looping issue.
I encountered such an issue in HtmlUnit for some URLs.
I raised issue in HtmlUnit user group.
They told me that JS infinite loop was causing freez.
Time out did not work for me as well.
I tried to apply my own time out. That did not work too.
Refer following question for applying own timeout
HtmlUnit WebClient Timeout
If it works for another website, problem might be site specific.
You can attach source of Selenium in eclipse and check / debug where it is getting stuck.
I did same for HtmlUnit. I reached to parse method, which did not come out.
If still does not work, contact Selenium support.
I have a simple CSS-based dropdown menu, and I'm trying to click on one of the menu items in a Java Selenium (WebDriver) test.
Both the menu (<ul> element) and the items (<a>) have IDs and creating corresponding WebElement objects works fine. I'm trying to click on one of the items with code like:
hoverOver(transfersMenu);
transferLink.click();
In hoverOver(), I've tried all three answers from this question, but none of them work. I keep getting:
org.openqa.selenium.ElementNotVisibleException:
Element is not currently visible and so may not be interacted with
Command duration or timeout: 2.06 seconds
(I've tried calling transferLink.click() also before hoverOver(), in the hope that the implicit wait would make it work, but nope.)
Any idea how to make the hovering work so that the link can be clicked?
Selenium version 2.21.0. I'm running the tests on Linux (Ubuntu), using Firefox 13.0. A colleague just tried on Windows (using Firefox 12.0), and it didn't work for him either.
Update: As per Slanec's tip in comments, and these instructions, I tried setEnableNativeEvents(true) on the FirefoxProfile. At first this failed:
org.openqa.selenium.InvalidElementStateException:
Cannot perform native interaction: Could not load native events component.
...but after I upgraded to Selenium 2.23.1, I no longer get that complaint.
Still, the hovering doesn't work (with native events on or off). :-/
I use the following code to hover over our menus for 1 second, before clicking a link, just like the one you are using:
action = new SeleniumActionHelper(driver);
WebElement currentUser = findElementByLinkText("testing1");
action.mouseHover(currentUser);
Thread.sleep(1000);
Of note, the mouse cursor needs to remain in the browser window for the hover to keep. If the mouse cursor is outside of the browser window, I experience a quick flash of the menu, but it does not stay visible
Try this exampale:
WebElement menuHoverLink= driver.findElement(By.id("test"));
actions.moveToElement(menuHoverLink).perform();
driver.findElement(By.id("test")).click();
Thread.sleep(6000);
How do you run your test classes? I found out that running WebDriver through ANT makes hover actions impossible, whereas running the test classes from command line (TestNG JAR) or from Eclipse works just fine.
I'm trying to write a Selenium test for a web page that uses an onbeforeunload event to prompt the user before leaving. Selenium doesn't seem to recognize the confirmation dialog that comes up, or to provide a way to hit OK or Cancel. Is there any way to do this? I'm using the Java Selenium driver, if that's relevant.
You could write a user extension (or just some JavaScript in a storeEval etc) that tests that window.onbeforeunload is set, and then replaces it with null before continuing on from the page. Ugly, but ought to get you off the page.
I've just had to do this for an application of mine where the onbeforeunload handler brings up a prompt if a user leaves a page while a document is in an unsaved state. Python code:
driver.switch_to.alert.accept()
The Java equivalent would be:
driver.switchTo().alert().accept();
If the alert does not exist, the code above will fail with a NoAlertPresentException so there is no need for a separate test to check the existence before accepting the prompt.
I'm running Selenium 2.43.0 but I think this has been doable for a while now.
In cases where I don't want the prompt to come up at all because that's not what I'm testing, I run custom JavaScript in the browser to set window.onbeforeunload to null before leaving the page. I put this in the test teardown code.
faced same problem with "beforeunlaod" event listner, LUMINUS! a chrome addon that helps me just block the event listener in the plugin thats all..
When I was confronted with limited control which I had over browser using Selenium, I turned to MozLab plugin which solved my problem if only for one browser platform.