When testing smartmenus in Selenium, submenus do not get exposed on hover over - selenium

Standard Selenium mouseover / hover mechanism does not work
Actions builder = new Actions(driver);
builder.click(actionsButton).moveToElement(addNewLink).build().perform();
However, click action unexpectedly works:
Actions builder = new Actions(driver);
builder.click(actionsButton).click(addNewLink).build().perform();
Which is the exact opposite of what happens when a user navigates the menus - they get exposed on hover over and closed on click.

The culprit turns out to be Smartmenus. It has a non-standard code that uses absence of a mouse to detect mobile devices. When a mobile device is detected, Smartmenus stops recognizing hover over and instead begins responding to click/tap.
Whatever Selenium does to simulate the mouse movement does not convince Smartmenus that the real mouse is present.
Switching to click instead of hover over also does not work reliably in cases where a user moves the mouse on the computer running Selenium tests.
I've settled for a workaround that tries to click and, if unsuccessful, switches to hover over:
Actions builder = new Actions(driver);
builder.click(actionsButton).click(addNewLink).build().perform();
try {
wait.until(ExpectedConditions.visibilityOf(pcLink));
}
catch (TimeoutException e) {
builder.click(actionsButton).moveToElement(addNewLink).build().perform();
wait.until(ExpectedConditions.visibilityOf(pcLink));
}
pcLink.click();

Related

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.

Selenium Webdriver Actions are not working with context menu (right click)

I am trying to use Selenium Webdriver to right click on a page, and navigate the context menu. This script should open a right click menu, and navigate Up 2 options, then select with the Return key...
driver.Navigate().GoToUrl("http://www.google.com");
//Google search bar
IWebElement tb = driver.FindElement(By.Id("lst-ib"));
Actions action = new Actions(driver);
//Right Clicks outside of the search bar.
action.MoveToElement(tb, -5, -5).ContextClick().Perform();
action.SendKeys(Keys.Up).SendKeys(Keys.Up).SendKeys(Keys.Return).Perform();
The right click executes as it should (outside of the search bar), but after that, there is no evidence of the Up arrow being pressed, and nothing is selected with the Return key. The menu options should highlight as they scroll through.
I am using the latest version of ChromeDriver 2.30, and Chrome 59.0.3071.109
If your application only runs on Windows, you may use System.Windows.Forms.SendKey.
action.MoveToElement(tb, -5, -5).ContextClick().Perform();
System.Windows.Forms.SendKeys.SendWait("{UP}");
System.Windows.Forms.SendKeys.SendWait("{UP}");
System.Windows.Forms.SendKeys.SendWait("{ENTER}");
If it is user designed context menu . The context menu itself would be having the locator
`WebElement element =driver.findElement(By.xpath("your xpath"));
Actions action = new Actions(driver);
action.contextClick(selectedCell).build().perform();
WebElement copyContext = driver.findElement(By.xpath("xpath of the right context column"));
if (copyContext .isEnabled())
{
copyContext .click();
log.info("Right context menu COPY CONTENT clicked.");
}
`
This chromedriver bug has been unattended since 2015.
I resorted to PyAutoGui for controlling dialog boxes.
http://pyautogui.readthedocs.org/

How to handle a popup in Selenium

I'm automating an application where on clicking a button a popup opens up , I am a bit confused about handling this popup.I am attaching the screenshot below where i have to handle the " Daily Trip details" popup. Can it be handled by driver.getwindowhandles() code or autoit?Thanks
Image of the popup:
You can handle using getWindowHadles(). After switching to new window you can perform actions which are required for your test case.
Following code may help you out for switching to modal
String currentWindowHandle=driver.getWindowHandle();
for(String windowHandle : driver.getWindowHandles()){
if(!windowHandle.equals(currentWindowHandle)){
driver.switchTo().window(windowHandle);
//perform actions which are required
}
}

program stuck using drag and drop selenium

I am trying to move slider using drag and drop. It identifies the element and clicked on it and after that nothing happens and my code stuck there itself(like waiting for user input). As soon as i moved my mouse little bit manually it executes rest of the code and works as expected. please help me what is this weird behavior.?. below is the code i used to build drag and drop.
Actions builder = new Actions(driver);
Action secondSlide = builder.dragAndDropBy(secondSlider, 50, 0).click().build();
System.out.println("waiting");
secondSlide.perform();
System.out.println("not waiting");
"Waiting" message is printing nicely but it doesn't print "not waiting" as it stuck in "secondSlide.perform()" line. But as soon as i moves my mouse little bit manually it prints "not waiting" and program ends gracefully.
Try to do it differently. Here is a number of approaches:
WebElement element = driver.findElement(By.name("element dom name"));
WebElement target = driver.findElement(By.name("target dom name"));
(new Actions(driver)).dragAndDrop(element, target).perform();
Or:
Actions builder = new Actions(driver);
Action dragAndDrop = builder.clickAndHold(someElement)
.moveToElement(otherElement)
.release(otherElement)
.build();
dragAndDrop.perform();

Selenium IDE window focus in Internet Explorer

I'm using selenium IDE and webdriver to test a web page in Internet Explorer. I discovered a while back that IE will not fully accept commands from Selenium if it's not the window in focus. For example, if the Selenium IDE window is in focus, and the command is to click a button in IE, the button will push down, but it won't let go.
With that in mind, my test involves popping up a window, doing a few things in it, leaving it open and returning to the null window to do a few things, then returning to the popup for a few more commands.
Is there a way I can make the null window come forward (over the popup) when I need to execute the commands for the null window? And then vice versa, can I make the popup then come forward when I need to return to it? I tried using windowFocus, but that did not work.
Use the SwitchTo() method and the TargetLocator Interface in Selenium.
A really simple example would look like this:
// Switch to new window
public String SwitchToNewWindow()
{
// Get the original window handle
String winHandleBefore = driver.getWindowHandle();
foreach(String winHandle in driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
return Constants.KEYWORD_PASS;
}
// Switch back to original window
public String switchwindowback()
{
String winHandleBefore = driver.getWindowHandle();
driver.close();
//Switch back to original browser (first window)
driver.switchTo().window(winHandleBefore);
//continue with original browser (first window)
return Constants.KEYWORD_PASS;
}
I remembered that webdriver sometimes acts differently than running non-webdriver tests. It turns out that using windowSelect followed by windowFocus switches between windows when running webdriver tests.