how to click on an element which comes under a toaster message - selenium

hi im into selenium automation course and as part of our project im automationg a web application. when i add users toaster message appears as User added succesfully. after this step i have to signout, but the signout element gets overllaped by the toaster. and error Element not clickable exception comes. i tried to give explicit wait, javascriptexecutor click. but its not working.
attaching my code:
public static void waitForElementToBeClickable(WebDriver driver, WebElement target) {
WebDriverWait wait = new WebDriverWait(driver, TimeUnit.SECONDS.toSeconds(EXPLICIT_WAIT));
wait.until(ExpectedConditions.elementToBeClickable ((target)));
}
public void isUserMenuLoaded() {
WaitUtility.waitForElementToBeClickable(driver, userMenu);
}
public SignOutPage clickOnUserMenu() {
PageUtility.clickOnElement(userMenu);
return new SignOutPage(driver);
}

In case the toast has close button you can wait for the toast appearance, then click the close button and wait until toast is disappeared.
Otherwise you should wait until toast disappeared and only after that you will be able to click the logout button.
You can enforce the process by clicking the logout button with JavaScript click but this is not recommended since this is not the way real user can do via the site UI.

Related

Switch focus to reopened tab in Chrome - selenium

I am trying to achieve the below flow in selenium ,
click on a link to open new tab(child1) from parent window
close the driver (child1) after performing certain actions
Open the same link(child1) again to perform another set of actions
I am able to achieve the first two steps successfully by switching focus. But I am stuck at the third step where I am not able to focus on the same reopened tab. I get the below error,
org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document
public class abc {
String currentWindow = driver.getWindowHandle();
public void action1() {
//Click on main menu that open a new tab
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(currentWindow)) {
driver.switchTo().window(handle);
}
//Perform the actions
driver.close();
driver.switchTo().window(currentWindow);
}
public void action2(){
//Click on main menu which reopen the same tab
for (String handle : driver.getWindowHandles()) {
if (!handle.equalsIgnoreCase(currentWindow)) {
System.out.println("Port Response : switch focus");
driver.switchTo().window(handle);
break;
}
}
//perform a set of actions
driver.close();
driver.switchTo().window(currentWindow);
}
}//end of abc
I want to create the actions more like a reusable independent action so I would like to close all tabs and reopen them again for other set of action. Please let me know if you need any more details on this.

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 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
}
}

Handle javascript alert in iframe using Selenium 2

I trying to practice handling Javascript alert. I want to accept alert ,which comes out of iframe in w3school website.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click();
WebElement editable = driver.switchTo().activeElement();
editable.sendKeys("enter");
//handle pop alert
String mainPage = driver.getWindowHandle();
Alert alt = driver.switchTo().alert();
alt.accept();
driver.switchTo().window(mainPage);
I am just getting alert box and then below error. I am not able to accept it.
Exception in thread "main" org.openqa.selenium.UnhandledAlertException: unexpected alert open: {Alert text : I am an alert box!}
You have all the right code. The error comes when you try to do something other than handle the alert once the alert is up. You have some extraneous code that isn't needed to handle the alert. I've removed it in the code below. You can add things back but you will need to add them before or after launching the alert and handling it.
WebDriver driver = new ChromeDriver();
driver.get("http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert");
driver.switchTo().frame("iframeResult");
driver.findElement(By.xpath("/html/body/button")).click(); // this launches the alert
driver.switchTo().alert().accept(); // this one line accepts the alert
driver.switchTo().defaultContent(); // you will probably need to switch back out of the IFRAME at some point to access the main page again so I added this line
One thing that I would strongly suggest for you is to read some guides on debugging in the IDE of your choice. If you learned how to debug your code, you could walk through it and more likely see where the issue was and fix it.

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

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();