Actions Class not working properly after ChromeDriver updated to newer version - selenium

I have updated my ChromeDriver to newest version (to 77). and noticed that previously working methods are not working now. Specifically Actions class is not working properly, its not sending data, not hitting enter.
for example this method is not clicking "Ctrl + F". it used to always click before chromedriver update. I am using pageFactory, this method below I am using in my actual scripts and it not sending keys
public void PressCtrlF() {
Actions a = new Actions(ldriver);
a.sendKeys(Keys.chord(Keys.CONTROL,"F")).build().perform();
}
it supposed to hit "Ctrl+F" and search box should appear
Anyone know how to resolve Actions class due to ChromeDriver update

Related

Why does Selenium ExecuteScript method doesn't work anymore in Salesforce application but ExecuteAsyncScript works

Hi I've been trying to figure this for the past couple of days. I wrote the code below that use to work but doesn't work any more using the ExecuteScript method. The only thing i have changed was update my chrome driver because it was not launching chrome since the driver wasn't working with the older chrome version I have. So once I updated the chrome driver it began to work until i had to run this piece of code. I modified it below not to what the actual link text is.
IJavaScriptExecutor js = (IJavaScriptExecutor)WebActions.One;
js.ExecuteScript("alert('Welcome to Guru99');"); // This was added for testing purpose
IWebElement somelink = WebActions.One.FindElement(By.XPath("//span[text()='Some Text']"));
js.ExecuteScript("arguments[0].click();", somelink );
When i changed the method to be ExecuteAsyncScript it started working again. Is this something that has changed with the app or is this something i'm doing wrong. I tried reading up on ExecuteAsyncScript vs ExecuteScript to see the difference but all i was able to get was something about the call back telling
when its finished which i'm not experienced in java script.
I would just like to understand more then anything or should i just start using ExecuteAsyncScript method. Thanks
There's a chromedriver ticket about this issue here:
https://bugs.chromium.org/p/chromedriver/issues/detail?id=3103
It states:
The cause of the issue is this website modifies the constructor of the
built-in AsyncFunction object. This causes difficulty when
ChromeDriver tries to create an AsyncFunction to wrap the script being
executed.
chromedriver 78 fixes the issue. For testing with Chrome 77, the ticket recommends switching to executeAsyncScript, or using chromedriver 76 since "ChromeDriver vX will run with Chrome vX+1".

Is there a change in the handling of unhandled alert in ChromeDriver and Chrome with Selenium?

I have a test that has been running fine for months. One thing it does is cause an alert and then verify the alert text. This is running with Selenium, Java and Chrome Driver 76.0.3809.68.
Lately it has been giving me the error:
"No such alert".
What happens is it clicks a button and waits for an alert if there:
try {
button.click();
} catch (UnhandledAlertException ex) {
// nothing
}
// then here goes code to accept the alert and get the text
when stepping through I see the alert. When I run it, I see the alert but it disappears. I did read something in the (Chrome Driver) release notes about unexpected alerts but it was a bit vague.
We have a global page which sets the options for Chrome, but everyone uses it and I don't want to screw up for other people. I did do it locally (did not git push) and it worked when I set the options before creating the driver.
Then I tried to do this, which does not seem to work. Should it, or once the web page is retrieved, can you not change options?
// Somewhere after web page retrieved this gets called:
public void setIgnoreAlert() {
ChromeDriver cd = (ChromeDriver) driver;
ChromeOptions cap = new ChromeOptions();
cap.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
Capabilities other = cap;
cd.getCapabilities().merge(other);
}
Which I was really hoping would work, but did not. Do you have to set the behavior before the Chrome instance comes up? That is, can you not set it as I did above? Any other suggestions on how to set it after the Chrome instance is up?
--- added later to answer question
This is done immediately after the try-catch block with button.click():
The method configPage.getAndHandleAlertPopUp() does the following:
public String getAndHandleAlertPopUp() {
Alert alert = driver.switchTo().alert();
String alertPopup = alert.getText();
alert.accept();
return alertPopup;
}
You saw it right. As per the User Prompts section within WebDriver - W3C Recommendation:
The common denominator for user prompts is that they are modal windows requiring users to interact with them before the event loop is unpaused and control is returned to the current top-level browsing context.
By default user prompts are not handled automatically unless a user prompt handler has been defined. When a user prompt appears, it is the task of the subsequent command to handle it. If the subsequent requested command is not one listed in this chapter, an unexpected alert open error will be returned.
User prompts that are spawned from beforeunload event handlers, are dismissed implicitly upon navigation or close window, regardless of the defined user prompt handler.
A user prompt has an associated user prompt message that is the string message shown to the user, or null if the message length is 0.
As per the discussion in ChromeDriver should return user prompt (or alert) text in unhandled alert error response:
When a user prompt handler is triggered, the W3C Specification states that the error response should return an "annotated unexpected alert open error" which includes an optional dictionary containing the text of the user prompt. ChromeDriver should supply the optional information.
Clearly, ChromeDriver was not compliant with this standard as the #Test were annotated with #NotYetImplemented as follows:
#Test
#NotYetImplemented(CHROME)
#NotYetImplemented(CHROMIUMEDGE)
#Ignore(value = HTMLUNIT, reason = "https://github.com/SeleniumHQ/htmlunit-driver/issues/57")
#NotYetImplemented(value = MARIONETTE,
reason = "https://bugzilla.mozilla.org/show_bug.cgi?id=1279211")
#NotYetImplemented(EDGE)
public void testIncludesAlertTextInUnhandledAlertException() {
driver.get(alertPage("cheese"));
driver.findElement(By.id("alert")).click();
wait.until(alertIsPresent());
assertThatExceptionOfType(UnhandledAlertException.class)
.isThrownBy(driver::getTitle)
.withMessageContaining("cheese")
.satisfies(ex -> assertThat(ex.getAlertText()).isEqualTo("cheese"));
}
Now this feature have been implemented with ChromeDriver v76.0:
Resolved issue 2869: ChromeDriver should return user prompt (or alert) text in unhandled alert error response [Pri-2]
So you have to handle the alert as a mandatory measure.
A bit more of your code code block for ...then here goes code to accept the alert and get the text... would have helped us to debug the issue in a better way. However here are the options:
Induce WebDriverWait for alertIsPresent() as follows:
new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
Your code trials was perfect perhaps as you have passed the CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE in a structured way:
public void setIgnoreAlert() {
ChromeOptions opt = new ChromeOptions();
opt.setCapability(CapabilityType.UNEXPECTED_ALERT_BEHAVIOUR, UnexpectedAlertBehaviour.IGNORE);
}
Another perspective would be to disable the beforeunload event handlers and you can find a couple of related discussions in:
How to disable a “Reload site? Changes you made may not be saved” popup for (python) selenium tests in chrome?
How to handle below Internet Explorer popup “Are you sure you want to leave this page?” through Selenium
Note: Once the WebDriver and Web Browser instances are initialized you won't be able to change the configuration on the run. Even if you are able to extract the Session ID, Cookies and other capabilities and session attributes from the Browsing Session still you won't be able to alter those attributes of the WebDriver.
You can find a detailed discussion in How can I reconnect to the browser opened by webdriver with selenium?

Selenium Actions.movetoElement - org.openqa.selenium.UnsupportedCommandException

I have a scenario where I have to hover over a menu link and click on the drop down sub menus. The code that I'm using is below:
public void changeLanguageTest() throws InterruptedException
{
WebElement LanguageMenu = driver.findElement(By.cssSelector(".change-language>button"));
action.moveToElement(LanguageMenu);
WebElement mongolianLang = driver.findElement(By.cssSelector(".change-language>ol>li:nth-child(3)>a"));
action.moveToElement(mongolianLang).click().build().perform();
Thread.sleep(3000L);
}
But when I run this code, It fails with an error message :-
org.openqa.selenium.UnsupportedCommandException: POST
/session/3077f893-d9ab-487d-b09f-c5bcd135ea31/moveto did not match a
known command
I tried below mentioned code too but no success and same error occurred.
BaseClass.action.moveToElement(LanguageMenu).moveToElement(mongolianLang).click().build().perform();
I'm using webdriver v2.53 and running it on FF v47.0.1.
As a bug logged here geckodriver does not yet implement actions. The actions we will implement are those being defined right now in the W3C WebDriver standard and not those of Selenium.
Selenium has said they will provide a Selenium-to-W3C-WebDriver shim for actions, but this may take some time to produce after we have made our implementation. Implementation in geckodriver/Marionette has not yet started.
As mentioned here from v0.12.0 of geckodriver, Implemented routing for new actions API, but it too is not yet fully implemented in Marionette
You should upgrade your geckodriver.

Element not being added when running test through webdriver

I am working on writing a story for a bdd framework which uses jbehave/selenium/webdriver and am having a problem where the test encounters an error while running the story but appears to be fine when running manually. I'm having a problem where javascript for the functionality I'm testing behaves slightly different when I'm running tests manually on firefox vs through selenium web driver on the same system/version of firefox and this difference is causing a js error.
I've debugged and basically the root of the problem appears to be that var request_XML_container = $('div_appendpoint_id'); returns something different when I'm running the test manually vs when I run through the bdd framework.
var request_XML_container = $('div_appendpoint_id');
request_XML_container.innerHTML = encoded_xml_from_request;
var pos = method_to_get_position('id_of_place_div_should_be_appended_to');
// JS exception is thrown saying that style is not defined **ONLY**
// when running through web driver. Running test manually on
// same system and same browser works fine.
request_XML_container.style.left = (pos[0] - 300) + 'px';
request_XML_container.style.top = (pos[1] + 25) + 'px';
request_XML_container.style.display = "block";
Why this would work fine when running manually that var request_XML_container = $('div_appendpoint_id'); would return an item with style defined, but when running through webdriver that the style attribute of the element would not be defined?
UPDATE: I had originally thought that this was updating an iframe, but I read the markup wrong and the iframe I saw is a sibling - not a parent - of the element where the response is being appended to. I'm trying to simply append the response to a div. To be honest, this only makes things more confusing as grabbing a div by id should be pretty straight forward and I'm now sure why webdriver would be producing a different return element in this situation.
UPDATE 2: Steps to reproduce and information about the system I'm on:
Use webdriver to navigate to this url: http://fiddle.jshell.net/C3VB5/11/show/
Have webdriver click the button. It should not work
Run your test again, but pause put a breakpoint at your code to click the driver
Click the button on the browser that webdriver opened. It should not work
Refresh the browser page on the browser that webdriver opened. Now, it should work.
System details:
OS : OS X 10.8.5 (12F37)
IDE : Eclipse Kepler: Build id: 20130614-0229
Browser (used manually and by webdriver) : Firefox 23.0.1
Selenium version: 2.35.0
UPDATE 3: I have provided this maven project on github to aid in reproducing: https://github.com/dkwestbr/WebdriverBug/tree/master/Webdriver
Synopsis/tl:dr; Basically, in certain situations it appears as though webdriver is overwriting the '$()' javascript method with a method that does not return an HTMLElement with innerHTML or style defined (among other things). This post details the issue and how to reproduce.
I have opened this ticket to track the issue: https://code.google.com/p/selenium/issues/detail?id=6287&thanks=6287&ts=1379519170
I have confirmed that this is a bug with the Thucydides framework (understandable since they still aren't at a 1.0 release).
Issue can be tracked here: https://java.net/jira/browse/THUCYDIDES-203

How do I interact with a popup window with Mink, Selenium 2, and Behat?

I am running through an internal site with Behat and for the most part it is going really well. But the problem is that on certain parts of the site we have popup windows that come up to complete an action. In this case we hit a "Withdraw" button and a popup comes up to have you select a reason and save it.
In an ideal world, and if I had actually designed this app, the site wouldn't be using any popup windows. But I am the new guy who is supposed to implement automated functional tests (and I am learning how to do that from the ground up). So I don't really have any say over the site design at this point (though I will push for a lot of changes as time goes by).
I am running Behat with Mink and the Selenium 2 driver on an Ubuntu 12.10 system (will eventually have to run some tests on a Windows environment for testing in IE). I am also using PhantomJS for some of the tests I have setup.
Anyway, does Behat/Mink support working with popup windows somehow through the Selenium 2 driver (or through PhantomJS)? I am early in all of this automation setup and really I am just experimenting with tools. If there is a better tool that can handle this then please let me know.
My primary question is how do I get Behat/Mink to work with the popup window, check a box, fill in a field, and click the save button? I know how to do everything except get it to interact directly with the newly popped up window. Any ideas/suggestions would be welcome.
Thanks!
So it turns out that Mink includes some window switching features, but no way to identify said windows. So I wrote two functions getWindowName() and getWindowNames() that identify the current window and all open windows respectively. I committed these changes to the project in GitHub it seems that my fixes will get implemented soon into the code base.
But with these changes I am able to switch windows no problem.
Link: https://github.com/Behat/Mink/pull/341
By setting the focus of the window we can also name these windows so we can access them again in the future.
Using this method we can easily switch between popup windows and continue testing...
/**
* #Then I switch to popup :name
*
* #param $name
*/
public function iSwitchToPopup($name)
{
$this->iSetMainWindowName();
$this->getSession()->switchToWindow($name);
}
/**
* #Then I set main window name
*/
public function iSetMainWindowName()
{
$window_name = 'main_window';
$script = 'window.name = "' . $window_name . '"';
$this->getSession()->executeScript($script);
}
/**
* #Then I switch back to main window
*/
public function iSwitchBackToMainWindow()
{
$this->getSession()->switchToWindow('main_window');
}