How to test blinking element of a web page? - testing

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.

Related

How to get Position of button in selenium c#

One Video is there below that one button is there i need to automate that button wheather that button is present below that video or not any one please help me to resolve this
If the button is a part of the page DOM and it can be searched by Selenium you should be able to access it's Location property like:
System.Drawing.Point location = driver.FindElement(By.XPath("//your_element_locator")).Location;
Console.WriteLine("My element location: " + location);
If you want to conditionally execute this or that code block depending on presence/absence of the element you can go for FindElements() function like
if (driver.FindElements(By.XPath("//your_element_locator")).Count > 0)
{
// element is present
}
else
{
//element is absent
}
If the button is not present in the DOM and it's a part of video - you will have to go for image recognition frameworks like AForge.NET, Emgu CV or SeeTest

How to validate the user has been scroll to top when he click on "back to top" button in 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();
}

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.

Unable to get Xpath for an input tag on a form

I am not able to get the Xpath for an input file with Selenium.
Here is a screenshot (of the mark-up).
Since the element you want is inside an iframe, you will have to switch selenium's focus to the iframe and then use the xpath to fetch the element.
JAVA:
To switch to the iframe, use
driver.switchTo().frame("uploadFile"); // used iframe's id as locator inside frame()
then, to fetch the input element, use the xpath //input[#name='file']
Do not forget to navigate away from the iframe after you are done interacting with this iframe. To navigate to default frame, use driver.switchTo().defaultContent();
There is a frame in your html code, first you have to switch to that frame.
WebDriver's driver.switchTo().frame() method takes one of the three possible arguments:
A number.
Select a frame by its (zero-based) index. That is, if a page has three
frames, the first frame would be at index 0, the second at index 1 and
the third at index 2. Once the frame has been selected, all subsequent
calls on the WebDriver interface are made to that frame.
A name or ID.
Select a frame by its name or ID. Frames located by matching name
attributes are always given precedence over those matched by ID.
A previously found WebElement.
Select a frame using its previously located WebElement.
Get the frame by it's id/name or locate it by driver.findElement() and you'll be good.
Then you can perform any action on input tag using below cssSelector :
driver.findElement(By.cssSelector("input[name=file]"));
And after performing your action don't forget to swith it to default frame :
driver.switchTo().defaultContent();
Exception Handling
You can wait for the alert to come for 30 seconds using below code , once the alert is present you can easily accept it.
try {
WebDriverWait waitAlert = new WebDriverWait(wb, 30);
waitAlert.until(ExpectedConditions.alertIsPresent());
wb.switchTo().alert().accept();
} catch (Exception e) {
logger.info("Alert Didn't Come");
}
Let me know if it helps.
Try below code.
//Going inside of the iframe
driver.switchTo().frame("uploadFile");
//Getting file input webelement
WebElement input=driver.findElement(By.xpath("//form[#name='fileUploadFrm']//input[#name='file']"));
//Sending file path using sendkeys method
input.sendKeys("filepath");
//Switch back to default
driver.switchTo().defaultContent();
Exception handling: Modal dialog present exception will be thrown when an alert appeared and we are not handling it. If the alert doesn't appear every time place below after your operations.
try {
Alert alert = driver.switchTo().alert();
alert.accept();
//if alert present, accept and move on.
}
catch (NoAlertPresentException e) {
//do what you normally would if you didn't have the alert.
}
Let me know if it works for you

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.