Selenium Webdriver : Dismiss Chrome Alert - java

I want to select the check box Prevent this page from creating additional dialogs before selecting ok to close the alert
Currently i am using
Alert alert=driver.switchTo().alert();
//check the checkbox
alert.accept();
As an normal interactive user to check the check box i have to use a combination of <Tab> + <Space/Enter> keys. The <Tab> shifts the focus to the check-box and the <Space/Enter> checks the check-box.
Solutions Tried
I tried using Java sendKeys mechanisms ( Robot class, driver.sendKeys(), etc.), but an UnhandledAlertException is getting thrown.
I tried using alert.sendKeys which is different from driver.sendKeys() but it too failed
//check the checkbox
alert.sendKeys("\t");
alert.sendKeys("{TAB}");
alert.sendKeys("\uE004");
alert.sendKeys("\\U+0009");
alert.sendKeys(Integer.toString(KeyEvent.VK_TAB));
I am trying to avoid robot class as much as possible as i need to run the test in grid in which case robot class will not work.Any pointers on how to send keys to the alert window ?
Temporarily i could do it using a javascript window.alert = function() {}; by simply overriding the alert with an empty function just curious to know if it could be done with webdriver functions like alert.sendkeys or any other methods ??
Any help is greatly appreciated!!

Related

Closing an Alert with Selenium Webdriver

Unable to close an alert which pop-ups once the url is navigated and enter something in the page(https://www.roadrunnersports.com).
I tried using sendkeys and using actions class aswell.
Manually, I am able to close the alert but I want to automate that aswell.
Anyone suggest me a way to handle this?
Note:Its not a sweet alert also to as I am unable to inspect the element.
It looks like it isn't consistently the same modal. But, the ones I've seen can be cleared by hitting the Esc key. Try using that with actions
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
actions = ActionChains(driver)
actions.send_keys(Keys.ESCAPE).perform()
Make sure you wait for the modal to appear though, it seems like it takes a bit.
Actions actions = new Actions(driver); // define action class
actions.sendKeys(Keys.ESCAPE).build().perform(); // Press ESC key

Popup messages with selenium webdriver

I'm testing a website with validation etc. when the user types something incorrectly in each field and clicks save, a popup with the information appears. All those information are in the functions in the code (there is no JSON) file with them. My question is, how can I test them with selenium so when I'm writing a test script (in Java), so when the script is running they also show up on the console? I hope I explained my problem well.
Get Text From Pop Up:
String popUpText = driver.switchTo().alert().getText();
Accept Pop up Message:
driver.switchTo().alert().accept();

After navigating from one tab to another in selenium webdriver, the xpath for a button does not work, how can I make it work?

// opening the base URL
driver1.get(baseUrl+"/");
// opening a new tab
driver1.findElement(By.cssSelector("Body")).sendKeys(Keys.COMMAND + "t");
driver1.get("my URL");
// getting back to the first tab
driver1.findElement(By.cssSelector("body")).sendKeys(Keys.COMMAND, Keys.SHIFT, "{");
// I want to signup by clicking the sign up button
driver1.findElement(By.xpath("/html/body/div[1]/header/div[2]/button")).click();
The error that I get after running is :
"Error communicating with the remote browser. It may have died."
But when I run the same code without the navigation, the button click works fine, then it means there is no problem with the xpath.
The information which browser you are using would be very interesting. And the webdriver doesn't need to control the visbile Tab. So i wouldn't let the Browser change the Tab and I would use the "switchTo" Method of the webdriver.
More information to this topic is here.
As #Kikkirej mentuined, i see no reason to use sendKeys to switch between opened tabs. Use Selenium instead, it is a much better approach.
Edit: in addition, try to provide more information, especially the most basic part - the browser you are automating.

Handle Window Pop Up in Selenium

I am working with Selenium, now there is a condition:
when I hit a button in my webpage a window pop up opens up.
Now I have to click a radio button (one out of two, it will work even if we send a TAB ) and then click an OK button. I searched in the net and got to know about "driver.getWindowHandle()".
But I don't have any idea dealing with the newly opened window popup.
Need help in this.
For switching purpose u can use enhanced for loop:
for (String winHandle : objDriver.getWindowHandles()) {
objDriver.switchTo().window(winHandle);
}
So it will switch the control from one driver window to child windows.
To interact with elements on the window try to find element with whatever tool u r using and perform the required action after switching to the window.
To return back to parent window you can use the same loop or use:
driver.switchTo().defaultContent();
Check my answer in this post and also read the comments to help you understand the difference between getWindowHandle() and getWindowHandles()
Java: focus is not on pop-window during window handling
We handled this situation using AutoItX - https://www.autoitscript.com/site/ in our Windows/IE C# project:
AutoItX3 autoIt = new AutoItX3();
var handle = autoIt.WinWaitActive("[window title]", "", 20);
Assert.IsTrue(handle != 0", string.Format("Was not able to find: {0}", [window title]);
autoIt.Send("{ESCAPE}"); // tab may work as well for selection
The pop up was a Windows window, and not part of IE, therefore the WebDriver didn't know about it.
Hope this helps.

Sencha Disabled Button and Selenium Webdriver

I have a button on a website built with sencha/extjs. Currently the button id is savebutton-1550-btnEl, but this changes everytime the page is loaded. I know that the button is disabled, but for testing purposes, I'd like to set this button as enabled, and then click it.
How would I go about finding this element each time, and then disabling it and clicking it with Java Selenium?
I'm guessing I'll have to execute some javascript, but I'm having a hard time finding the target for the javascript.
To locate the element you will have to use part of the DOM surrounding the element as a unique locator. It is impossible to give a more specific answer without seeing the DOM you are working on, but you may try something like:
WebElement saveButton = driver.findElement(By.xpath("//button[text()='Save']");
For changing the element to enabled, take a look at this answer: Selenium Webdriver - click on hidden elements
Also a longer term solution might be to work with development to see if they can build in a unique locator especially since this probably wont be the only object that you have problems with. At my company we use the "class" field to uniquely identify objects in extjs.
I override the the class "AbstractComponent":
Ext.define('Foo.overrides.AbstractComponent', {
override: 'Ext.AbstractComponent',
onBoxReady: function () {
var me = this;
var el = me.getEl();
if (el && el.dom && me.itemId) {
el.dom.setAttribute('data-test', me.itemId);
}
me.callOverridden(arguments);
}
});
If you set in configuation of the button the "itemId", you
can be accessed the button via seleniumas follows:
IWebElement element = webDriver.FindElement(By.XPath($"//*[#data-test='{itemId}']"));

Categories