How to validate the user has been scroll to top when he click on "back to top" button in selenium - selenium

I came across one of the scenario where I need to validate the user is scroll to top of the page when clicked on the "back to top" button on the bottom of the screen.
I tried with the following way but that didn't work.
I tried to validate the element present on the top of the page using
isDisplayed method
I have attached the image for clear description.

Solved it using the javascript concept used pageYOffset method.
Complete code
JavascriptExecutor executor = (JavascriptExecutor) driver;
Long value = (Long) executor.executeScript("return window.pageYOffset;");
pageYOffset method will return the vertical pixels, so as soon I logged in got the vertical pixels and then scrolled to the back to top button and after performing the action on back to top button, again got the vertical pixels and validated it.

isDisplayed() checks if the element is actually present in the viewport so it should work. May be put some wait in between clicking and checking isDisplayed for debugging puropose .
if (element.isDisplayed()) {
doSomething();
}
else {
doSomethingElse();
}

Related

In the Navigator.click() method where exactly in the stack does the scrollIntoView() occur?

With Geb and WebElement, before a click is performed the webelement is first scrolled into view. I run into a problem here because there is a 'menu' bar at the top of screen that the webdriver doesn't see. this cause chrome to throw a webdriver exception stating that the element is not clickable at point x,y since the webDriver scrolls the element under the menu banner.
If I look at the implementation of the NonEmptyNavigator and subsequently remote webElement, I can't find where the scrollIntoView() occurs in the code. I want to put some code in between the scrollIntoView() and the actual click action so that I can offset the ScrollIntoView() slightly so the click can be performed. where exactly in stack for Navigator.click() does the scrolling into view happen?
Scrolling elements into view before clicking when calling WebElement.click() happens inside of the implementation of the method on the browser side so you won't find any reference to scrolling anything into view in Geb or RemoteWebElement. Here's a link to an example of the scrolling happening on the browser side from Marionette's (Firefox WebDriver implementation) code base: https://searchfox.org/mozilla-central/source/testing/marionette/interaction.js#148.
If you wish to manually scroll the element into view before clicking on it you might want to write a Geb module and overwrite click() in it:
class ViewPortOffsetModule extends Module {
Navigator click() {
//put your implementation of scrolling the element to view here, most probably using the js executor
super.click()
}
}
and then in your content definition:
static content = {
elementNeedingOffsetWhenScrollingIntoView { $("#my-element").module(ViewPortOffsetModule) }
}
Now, if you correctly implemented scrolling into view in ViewPortOffsetModule, calling elementNeedingOffsetWhenScrollingIntoView.click() will scroll the element into view with offset before clicking it.

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.

How to test blinking element of a web page?

Suppose that you have a web page with blinking elements (for instance the background of a cell blinks red when a condition happen on the database, the page queries the database with a javascript). How would you verify that the element is blinking with Selenium web driver?
You can check if element is visible or not.
IWebElement myBlink = chrome.FindElement(By.Id("blink"));
if (myBlink.Displayed)
{
//it's displayed
}
else
{
//it's not displayed
}
All you have to do next is create a timer to set up time when to check it again.

Is it possible to assert that the page has stopped scrolling in a waitFor

I have a page where I click a "change" link which displays another section. When this happens, the page scrolls vertically a bit so that the visible section is somewhat centered on the screen.
My next step in the test is to click something inside this newly shown container. But since the viewport scrolled, the coordinates that geb picks up for the clickable element are no longer accurate and geb tells me it can't click the link.
There's nothing I can assert in the waitFor in terms of visible content. But I'm wondering if there is a way for me to waitFor the content to stop scrolling?
waitFor { // page no longer scrolling }
If not, is there a way to just tell geb to wait a few seconds before moving on to the next event?
If you know what element you're scrolling to (the element that is at the top of browser viewport when you're done with scrolling) you can wait for the y property of a navigator representing that element to equal zero. Following is an example that you can paste into groovy console which goes to a page and then scrolls to an element by using it's id in the url (I know there is no waiting here nor the scrolling is animated but I just want to show how that property can be used to achieve want you want):
#Grapes([
#Grab('org.gebish:geb-core:0.9.0'),
#Grab('org.seleniumhq.selenium:selenium-firefox-driver:2.32.0')
])
import geb.Browser
Browser.drive {
//at the top of the page
go 'http://docs.codehaus.org/display/GROOVY/Creating+an+extension+module'
//an element we'll scroll to later
def elem = $('#Creatinganextensionmodule-Themoduledescriptor')
assert elem.y != 0
//scroll to the element
go 'http://docs.codehaus.org/display/GROOVY/Creating+an+extension+module#Creatinganextensionmodule-Themoduledescriptor'
assert elem.y == 0
}
So you should end up with something like:
waitFor { elementWeScrollTo.y == 0 }
or even:
waitFor { !elementWeScrollTo.y }
I don't know how to express it in geb, but I'd write a loop checking the document.body.pageYOffset (document.body.scrollTop for IE) repeatedly until it settles down.

How do I use chrome driver to click a button in a frame in a pop up window, then get back to my original window?

I would, naively it seems, expect this code to click a button that opens a popup, switch to the popup, find the results frame (thanks sales force!) click a button there then finaly switch focus back to the original page.
Instead I get a 500 server error on the final switch to 'home'.
What should I be doing? I am using ChromeDriver 19.0.1068.0
Thanks
PageHelper.CountryButton.Click();
var home = _driver.CurrentWindowHandle;
foreach (var window in _driver.WindowHandles)
{
if (_driver.SwitchTo().Window(window).Title.Contains("Search"))
{
_driver.SwitchTo().Frame("resultsFrame");
PageHelper.Country.Click();
break;
}
}
_driver.SwitchTo().Window(home);
I have no solution to this but I expect it is related to this issue
http://code.google.com/p/selenium/issues/detail?id=1167