actions.MoveToElement() not working on ie 11 - c#

The same method used in the test automation project I wrote in c # does not work in internet explorer 11 even though the movement method I use is chrome, firefox and edge. It does not give any errors, but the next action is fail
log.Debug("fare " + by + " üzeriine dogru haraket ediyor, webelement label ");
IWebElement element = GetElement(by);
Actions Actions = new Actions(Driver);
WaitElementToClickable(Driver, by, 5);
Actions.MoveToElement(element);
Actions.Perform();
WaitElementToClickable(Driver, by, 5);

I spent a long time trying to get actions to work across all browsers, and for IE I found the following helped.
Selenium webdriver v2.29.0 (https://github.com/SeleniumHQ/selenium/blob/master/java/CHANGELOG) added:
IEDriver supports "requireWindowFocus" desired capability. When
using this and native events, the IE driver will demand focus and
user interactions will use SendInput() for simulating user
interactions. Note that this will mean you MUST NOT use the
machine running IE for anything else as the tests are running.
When I set the IEDriver I use:
InternetExplorerOptions options = new InternetExplorerOptions();
options.requireWindowFocus();
webDriver = new InternetExplorerDriver(options);
And all my move to and click events work fine. I'm using IE11.125-11.309 and Selenium (java bindings) 3.7.1.

Related

How to use Selenium WebDriver to reset the zoom to default on Chrome?

How can I reset Chrome's zoom level to Default(100%) using Selenium WebDriver? It's really easy to do using keyboard keys "Ctrl + 0".
However, when I try to emulate this using WebDriver, I can't get anything to work. Here is what I tried without success:
[TestMethod]
public void ZoomToDefaultLevelWithChrome()
{
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("https://www.ultimateqa.com");
var actions = new Actions(driver);
var browser = (IJavaScriptExecutor)driver;
var html = driver.FindElement(By.TagName("html"));
//None of these work
actions.SendKeys(Keys.Control).SendKeys(Keys.Add).Perform();
actions.KeyDown(Keys.Control).SendKeys(Keys.NumberPad0).Perform();
//actions.KeyDown(Keys.Control).SendKeys(Keys.NumberPad0).Perform();
//actions.KeyDown(Keys.Control).SendKeys(html, "0").Perform();
actions.KeyUp(Keys.Control);
actions.KeyDown(Keys.Control).Perform();
actions.SendKeys(html, "0").Perform();
actions.SendKeys(html, Keys.NumberPad0).Perform();
actions.SendKeys(Keys.NumberPad0).Perform();
browser.ExecuteScript("document.body.style.zoom = '1.0'");
browser.ExecuteScript("document.body.style.transform='scale(1.0)';");
browser.ExecuteScript("document.body.style.zoom='100%';");
}
The first 2 solutions from here don't work in C#: Selenium webdriver zoom in/out page content
Also, this doesn't work in Chrome even though it might in other browsers: selenium vba code to zoom out webpage to 60%
Can you try the below. This should work.
((IJavaScriptExecutor)driver).executeScript("document.body.style.zoom='100%';");
I have also encountered this problem, a work around I found was good was setting the the resolution capability
capability.SetCapability("resolution", 1920x1080);

How to use IMouse in Selenium?

C#, FierFox, Selenium v3.8
My purpose is movement cursor emulation in browser, using IMouse.MouseMove. Google searching gives me something like that, but I always receive System.NullReferenceException.How I should to use IMouse interface?
Simple console app:
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(Directory.GetCurrentDirectory() + "\\bin\\");
service.FirefoxBinaryPath = #"C:\Program Files (x86)\Mozilla Firefox\firefox.exe";
IWebDriver FF = new FirefoxDriver(service);
FF.Navigate().GoToUrl("https://www.google.ru/");
IWebElement element = FF.FindElement(By.Name("btnK"));
Actions act = new Actions(FF);
act.MoveToElement(element).Perform(); // It's OK
ILocatable hoverItem = (ILocatable)element;
IMouse mouse = ((IHasInputDevices)FF).Mouse;
mouse.MouseMove(hoverItem.Coordinates, 100, 100); // System.NullReferenceException
FF.Quit();
The short answer is that you don't do that in the .NET bindings. The IKeyboard and IMouse interfaces aren't intended to be used by users' code. This is despite the fact that they're public, and do have documentation. What you're running into is that those interfaces will not work for drivers that implement the W3C WebDriver specification. At the moment, that list is Firefox and IE (when using version 3.5 or later of the IE driver), but that list is going to grow to include Chrome and Edge soon, and eventually Safari too. The appropriate way to simulate mouse movement with the .NET bindings is through using the Actions class.

How to workaround certain differences between PhantomJSDriver and ChromeDriver

I have an issue when switching from ChromeDriver to PhantomJSWebDriver, that I need a workaround for.
Using the ChromeDriver I perform a .Click on a visible element, with another visible element next to it. the .Click makes the web page place a 'popup' over these elements. I then perform a .Click on an element within this 'popup' and it then goes away, and I am able to perform a .Click on the second of the original elements and it all works fine.
However, when I switch to using the PhantomJSDriver it complains that the second element is NOT visible after the 'popup' has been removed, and so wont do the .Click.
Is this a known issue with the PhantomJS driver, or is there some way to get it to 're-evaluate' what is visible? I have tried using a DriverWait looking for the element and waiting for it to be 'Displayed' but that doesn't work
WebDriverWait waitforpopdown = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
waitforpopdown.Until(d => {
var elpd = driver.FindElement(By.ClassName(trid));
return elpd.Displayed;
});
I'm using PhantomJS 2.1.1 and Selenium 2.52.0.0 and using C#
I had the same problem with PhantomJSDriver. It hasn't the same javascript engine than Chrome, FF and IE.
I "fixed" my problem by installing a selenium server that is running chrome on a docker. If you have the ability to do that, I would recommend to work that way. It's an actual chrome browser that is running, so you don't have any difference between your different types of chromes.
You can find information about it on the following page : https://github.com/peroumal1/docker-chrome-selenium
EDIT : You have to declare your driver like this (note that it's java Syntax, but I'm sure you'll be able to find the C# syntax) :
return new RemoteWebDriver(new URL("http://myurl.url:8888"), DesiredCapabilities.chrome());

Inconsistent behaviour of Click() in Selenium

I'm working on a Product automation(Web CMS), where element.Click() shows the inconsistent behaviour. Basically we are using,
Selenium + Nunit GUI(unit testing framework) - To run the test cases from local on a particular environment
Selenium + Asp.net web application - Multiple user's can run the test cases on different environment
Here environment I mean different levels(Dev, SIT, QA, Production).
My Concern
In one of my test cases, I want to Click a button. So for that, I have tried few code. But all are inconsistent behaviour. Here Inconsistent I mean, the code whatever I wrote for clicking a button are only working in my local or server and viceversa.
1st attempt:-
I tried all the element locator's
IWebElement element = driver.FindElement(By.Id("element id goes here"))
Working fine at my local, but not in server
Result - Failed
2nd attempt:-
driver.FindElement(By.XPath("Element XPath goes here")).SendKeys(Keys.Enter);
Working fine at server, but not in local
Result - Failed
3rd attempt:-
IWebElement element = driver.findElement(By.id("something"));
IJavaScriptExecutor executor = (IJavaScriptExecutor)driver;
executor.ExecuteScript("arguments[0].click()", element);
Not working in both(local and server)
Result - Failed
At last, I tried waiting for the element to be visible and performing action
4th attempt:-
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(60));
return wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("element xpath goes here")));
After webdriver wait performing action on that element (element.click())
Working fine at local but not in server
Result - Failed
I'm looking for a solution, where Clicking the button should not be an inconsistent behaviour. Basically it should work fine in both (Local and Server). Your help would be greatly appreciated..Thanks in advance
FYI - I'm testing in Mozilla Firefox browser 38.5.2
I'm using Selenium in C# locally on Win7 and remotely on Win10 and MacOS with the Firefox browser and also noticed that Firefox sometimes requires special treatment for IWebElement.Click(). So I wrote myself an extension method, which works fine for me, no matter by what locator the element was found:
public static void Click(this IWebElement element, TestTarget target)
{
if (target.IsFirefox)
{
var actions = new Actions(target.Driver);
actions.MoveToElement(element);
// special workaround for the FirefoxDriver
// otherwise sometimes Exception: "Cannot press more then one button or an already pressed button"
target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
// temporarily disable implicit wait
actions.Release().Build().Perform();
target.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(MyDefaultTimeoutInSeconds));
Thread.Sleep(500);
actions.MoveToElement(element);
actions.Click().Build().Perform();
}
else
{
element.Click();
}
}
If you want more stable behavior for your test cases, I would recommend using ChromeDriver. It never needs any special treatment at all, and it's also much faster than the FirefoxDriver.

Sending Ctrl-0 to IE 9 in webdriver C#

I attempting to send a Ctrl + 0 to set IE 9 to 100%.
I have tried the following snippets of code:
IWebElement query = Driver.FindElement(By.TagName("html"));
query.SendKeys("^0");
query.SendKeys(Keys.Control + Keys.NumberPad0);
I have also tried the actions object code:
Actions action = new Actions(Driver);
action.KeyDown(Keys.Control).SendKeys("0").KeyUp(Keys.Control).Perform();
I have read that in Java, WebDriver supports Keys.chord()
But I cannot find an implementation of this in C#.
Any ideas would be helpful.
The <html> tag will never be "visible" to the driver, thus can't have keys sent to it. The following code works for me:
IWebElement element = driver.FindElement(By.TagName("body"));
element.SendKeys(Keys.Control + "0");
However, with the most recent versions of the IEDriverServer.exe, you will not be able to get this far unless you set a special option for the driver to disable detection of the zoom setting. If your zoom level is not set to 100%, the InternetExplorerDriver constructor will throw an exception unless you instantiate your driver with something like:
InternetExplorerOptions options = new InternetExplorerOptions();
options.IgnoreZoomLevel = true;
IWebDriver driver = new InternetExplorerDriver(options);

Categories