What is the difference between Selenium WebDriver "Click" and JavascriptExecutor Click - java

Execution process difference between below two statements.
driver.findElement(By.xpath("//input[#value='Save']")).click();
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);

WebDriver click() simulates real user interaction with the UI. I will be performed (in most browsers) by sending a native event to the browser, and it has to be visible in order to click on it. From the docs
...if click() is done by sending a native event (which is the
default on most browsers/platforms)
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.
JavaScript click() on the other hand
Executes JavaScript in the context of the currently selected frame or
window.
Regardless if the WebElement is visible or not. This approach misses the idea of user interaction Selenium tries to simulate.

in simple terms. Webdriver uses a native browser events to click on element, and javascript uses JavaScrip to click on the element.
If I remember correctly Selenium 1, was using JavaScript for all it's action but they changed this in webdriver (Selenium 2) and now they are using native browser events to interact with browser. And for this reason you required corresponding support from browser (geckodriver, IEDriver, Chromedriver, etc..). JavaScript engine on the other hand is inbuilt in all the major browsers and so you don't require this extra executables.

Related

Wait until elementToBeClickable is not the solution for other element would receive the click exception

Yesterday I ask question how to deal with: "Element is not clickable at point - other element would receive the click" exception in Selenium WebDriver.
I receive several answers with this solution:
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.id(...));
Yes, it has sense but in practice it doesn't work.
Below is piece of my code:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView();", categoryItem);
Thread.sleep(1000);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(categoryItem));
categoryItem.click();
List<WebElement> selects = driver.findElements(By.tagName("select"));
Select ticketsSelect = new Select(selects.get(3));
ticketsSelect.selectByValue("2");
By addToBasket = By.xpath("//div[contains(text(),'Add to Basket')]");
Thread.sleep(1000);
new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(addToBasket));
driver.findElement(addToBasket).click();
I had to put 1 second sleep before each wait until element to be clickable because without this it still throw exception that other element would receive the click.
My question is why WebDriver clicks this element if element is not clickable ?
ElementToBeClickable – An expectation for checking an element is visible and enabled such that you can click it.
But it seems that it doesn't imply that this element is not "hidden" under some other element, even if it's a transparent part of another tag. In my experience, this exception is mostly occur when you are trying to interact with a page, which currently loads, and you have typical loading div overlapping the whole page with loading icon, or in other cases when you have an open modal, which also often cover page with overlays transparent overlay, to prevent user from interacting with a page.
In this answer you can see that you basically have two options:
You can send a click event via JavaScript call. It will always work, but it's not something you want to do. If you want to test your page, you should always trigger real click events.
Wait. There is not much you can do. Something you just need to wait till your page fully loads. In a way maybe this will show you the slow parts of your application, which you can improve in a future

How can I force the driver to click on a pop up on a page which has not completed loading

I'm using java with selenium IEDriverServer. I would like to know if is possible to click on a pop up on a page which has not completed loading. When I get the pop up, my code is not running until I click on Ok on pop up.
Thanks.
It's not possible to click on element that is not visible.
But you can add the following config, it will tell the driver to implicitly wait for elements:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
As you mentioned if is possible to click on a pop up I presume it's a JavaScript Popup.
Essentially, until & unless the synchronous/asynchronous JavaScript/AJAX Calls gets executed completely the Page Loading won't be complete. Hence, invoking click() method while Page Loading may not fetch optimum results.

How to Simulate double mouse click in Selenium Webdriver

I want to be able to click a logout button twice very rapidly like a user would?
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));{
if(logout.isDisplayed()){
logout.click();
I want to be able to click on the logout button twice at less than a second like a user would? Is this possible in selenium webdriver?
EDIT:
Make sure the dom is loaded
Like atri said, you could use the double click function according to This thread.
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));{
if(logout.isDisplayed()){
logout.doubleClick();
if you don't want to use the doubleClick function, I would recommend the ExplicitWait from Selemium
Selenium: Implicit and explicit Wait
If you want to do this manually, could add a delay between your click using javascript Thread and selenium wait.
Based on This thread
WebElement logout = driver.findElement(By.id("dijit_form_Button_0_label"));{
if(logout.isDisplayed()){
logout.click();
Thread.sleep(100);
logout.click();
}
Better way is to use ExplicitWaits which you means you will wait
exactly as long as some action happens or some element gets rendered
on the page.
- petr-mensik
An explicit waits is code you define to wait for a certain condition
to occur before proceeding further in the code. The worst case of this
is Thread.sleep(), which sets the condition to an exact time period to
wait. - Selenium

Selenium webdriver - mouseover and dropdown menu - mouse inside/outside the browser issue

Specs:
Windows 7;
selenium 2.39.0;
Java - Eclipse
I'm currently using Selenium to test a webApp through three different browsers (Chrome, IE9 and Firefox).
The webApp has a menu bar, and there are located dropdown menus (classic).
I need to set the mouse over one item of that menu bar and wait for a dropdown menu to appear, then I need to click in one of the dropdown menu items.
My code:
WebElement div_menu = driver.findElement(By.xpath("//div[text() = 'Trigger of the dropdown menu']"));
WebDriverWait wait = new WebDriverWait(driver, 300);
Actions builder = new Actions(driver);
builder.moveToElement(div_menu).build().perform();
WebElement item_to_click = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("Link_inside_the_dropdown_menu")));
item_to_click.click();
The funny stuff occurs when I start the test. (FYI Browsers come up one by one, that is to say, all tests are performed for one browser before going on with other one, not threaded.)
If the mouse pointer is over the browser (because they are started not full maximized) at the time the tests are performed, then here are the results:
Chrome: div_menu seems to get the mouseover (because of a css style change), but for too short, so that dropdown menu never appears
IE9: throws this exception: org.openqa.selenium.ElementNotVisibleException: Cannot click on element, but mainly, div_menu gets the mouseover, so dropdown menu is displayed. In spite of this, item_to_click it's not clicked at all (btw, I don't think the exception is the issue cause). And besides, it's like an invisible mouse keep laying over the div_menu, because when I move the real mouse, dropdown menu blinks as if div_menu lose the hover and get it back every time I stop moving the real mouse.
Firefox: Works as it's supposed to.
Now, let's see what happens when I left the mouse outside the browser window.
Chrome: works
IE9: works
Firefox: div_menu seems to get the mouseover (because of a css style change), but for too short, so that dropdown menu never appears
As you can see, the code is the same for the three of the browsers. The funny variable here is the mouse_in_browser and mouse_out_browser.
I can't imagine what the problem could be.
If you need any extra info, please don't hesitate.
Thank you very much in advance!
I managed to get Firefox to work no matters where the physical mouse is.
I initialized it this way:
//Firefox initialization
FirefoxProfile profile = new FirefoxProfile();
//explicitly enable native events(this is mandatory on Linux system, since they
//are not enabled by default
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
This did the trick for me regarding Firefox. Not yet the others two.
Source: Selenium WebDriver mouse actions moveToElement doesn't raise mouseout event on Firefox Linux

Bring browser from back to front? (Selenium Web Driver, Java)

Is there any way I can bring browser from the back to the top(front)??
The situation is that there are two browsers, firefox and chrome. So, I instantiated two drivers, new FirefoxDriver() and new ChromeDriver(), let's call them fdriver and cdriver.
What I want is when the program is using Firefox, the firefox browser should be on the top. And so does Chrome. But, I am stuck how to bring the browser to the top when they are on the back.
I already tried,
Javascript: self.focus() and window.focus(). / WebDriverBackedSelenium to make driver back to selenium and use windowMaximize and windowFocus
Any idea is appreciated, thanks
You should be able to do this with
driver.SwitchTo().Window("//name of the window");
and that will bring whatever window you want into focus.
Now I don't use selenium, but I use Geb which is a wrapper around selenium so calling the javascript may be different for you, but this is how I did it (should be similar)
browser.js."alert()"
webdriver.switchTo().alert().accept()
I called the javascript alert function which brought the window to the foreground then I dismissed the alert with webdriver.switchTo().alert().accept().
Robot from java.awt; could help you:
Robot.mouseMove(webDriver.manage().window().getPosition().getX(),webDriver.manage().window().getPosition().getY());
Robot.mousePress(InputEvent.BUTTON1_MASK);
Robot.mouseRelease(InputEvent.BUTTON1_MASK);
Limitations:
if you have more than one monitor, you need to calculate absolute coordinates. As webDriver.manage().window().getPosition().getX() give you offset for the currently used display.
window should be visible

Categories