WebDriver - is it possible to use Firefox or IE javascript engine when using HtmlUnitDriver using .NET Selenium bindings? - testing

I am running into inconsistent behaviour when running my test using DesiredCapabilities.Firefox() vs. DesiredCapabilities.HtmlUnitWithJavaScript().
For the most part, my issue lies with DOM elements not appearing on the page when they should be there.
I've used a number of different strategies for "Wait"-ing for said element, but still, HtmlUnit driver seems to think that the element is not visible.
With that said, I am now asking if it's possible to run HtmlUnit providing a Firefox or IE handle; similar to how Java's HtmlUnitDriver implementation works:
HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_3);

try WebDriver driver = new HtmlUnitDriver(DesiredCapabilities.firefox());

Related

Minimizing the browser window in selenium testing [duplicate]

After maximizing the window by driver.manage().window().maximize();, how do I minimize the browser window in Selenium WebDriver with Java?
Selenium's Java client doesn't have a built-in method to minimize the browsing context.
However, as the default/common practice is to open the browser in maximized mode, while Test Execution is in progress minimizing the browser would be against the best practices as Selenium may lose the focus over the browsing context and an exception may raise during the test execution. However, Selenium's Python client does have a minimize_window() method which eventually pushes the Chrome browsing context effectively to the background.
Sample code
Python:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get('https://www.google.co.in')
driver.minimize_window()
Java:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
There seems to be a minimize function now:
From the documentation on:
webdriver.manage().window()
this.minimize() → Promise<undefined>
Minimizes the current window. The exact behavior of this command is specific to individual window managers, but typicallly involves hiding the window in the system tray.
Parameters
None.
Returns
Promise<undefined>
A promise that will be resolved when the command has completed.
So the code should be:
webdriver.manage().window().minimize()
At least in JavaScript.
Unfortunately, Selenium does not provide any built-in function for minimizing the browser window. There is only the function for maximizing the window. But there is some workaround for doing this.
driver.manage().window().setPosition(new Point(-2000, 0));
Use the following code to the minimize browser window. It worked for me and I am using Selenium 3.5:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_SPACE);
robot.keyPress(KeyEvent.VK_N);
robot.keyRelease(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_SPACE);
robot.keyRelease(KeyEvent.VK_N);
I'm using Selenium WebDriver (Java) 3.4.0 and have not found the minimize function either. Then I'm using the following code.
driver.manage().window().maximize();
driver.manage().window().setPosition(new Point(0, -2000));
We need to import the following for the Point function.
import org.openqa.selenium.Point;
For C# , tested using Selinium 3.14 ver ( Package1 , Package2) and it work
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Minimize();
In Selenium version 4, there is a method called minimize() added in Window interface (An inner interface of WebDriver) with Java.
Hence now, if you have Selenium version 4, then you can use driver.manage().window().minimize().
This works for me in Python.
The window will open and after loading it will minimize.
driver.get(url)
driver.minimize_window()
Try this. It should work.
Dimension n = new Dimension(360, 592);
driver.manage().window().setSize(n);

how to locate element with selenium webdriver

After exporting a test in Visual Studio using Selenium webdriver, I execute the test case in VS and it fails when trying to locate an element in the test. The element is a tab within a page but the test case cannot find the id of the tab. This is the failing line driver.FindElement(By.Id("ui-id-14")).Click(); and I also tried to locate the element with the ClassName driver.FindElement(By.ClassName("ui-tabs-anchor")).Click(); by inspecting the element in Chrome browser but it still fails. Anyone can tell me what am I doing wrong here? Thank you in advance.
driver.FindElement(By.Id("ui-id-14")).Click();
driver.FindElement(By.ClassName("ui-tabs-anchor")).Click();
How Do you initalize you driver? A common mistake is not having a implicit wait with selenium. The driver tries to locate the element as soon as the document is ready, which is not feasible with "normal" JS applications as they are rendered after the document is ready. Try:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
This way the driver will try to find the element every 0.5 seconds up to 30 seconds before throwing an Exception. By the way: Welcome to stackoverflow. Please consider posting your code/code snippets, that makes analysis often easier. My suggestion is purely made from my experience with people being new to Selenium.

Is it possible to execute Javascript in the context of a page with Conductor Selenium framework?

I am using Conductor to control PhantomJS via Selenium/Webdriver. PhantomJS has a function evaluateJavascript.
Is it possible to execute Javascript in the context of a page with Conductor? If so, how?
I have not tried it, but I came across a similar problem with HtmlUnit.
I solved it by checking out the conductor framework GitHub project and I modified the Locomotive constructor to get the WebClient from the WebDriver. So you could try to cast the WebDriver to PhantomJSDriver and then access the internal methods like "executeScript", "executePhantomJS":
case PHANTOMJS:
capabilities = DesiredCapabilities.phantomjs();
if (isLocal)
try {
driver = new PhantomJSDriver(capabilities);
// Cast to PhantomJSDriver
((PhantomJSDriver)driver).executeScript(...)
}

issues in running selenium in internet explorer

Hi I am trying to run my selenium webdriver on IE9.
WebDriver version : 2.32.0
IE:9
IEDriverServer_win32:2.32.3
windows7
Below is my code:
File IEDriver=new File(System.getProperty("user.dir")+File.separator+"BrowserDrivers"+File.separator+"IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", IEDriver.getAbsolutePath());
DesiredCapabilities cap=DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver=new InternetExplorerDriver(cap);
driver.get("http://in00616:8421/GS");
Thread.sleep(3000);
//driver.findElement(By.id("j_username")).sendKeys("admin");
//driver.findElement(By.id("j_password")).sendKeys("admin");
driver.findElement(By.xpath(".//input[#id='j_username']")).sendKeys("admin");
driver.findElement(By.xpath(".//input[#id='j_password']")).sendKeys("admin");
driver.findElement(By.id("login")).submit();
Thread.sleep(2000);
driver.findElement(By.xpath(".//button[text()='Securities']")).click();
Thread.sleep(2000);
driver.findElement(By.xpath(".//span[text()='Issue']")).click();
Thread.sleep(2000);
driver.findElement(By.id("tabSecurities_Issue_Request_for_Issues")).click();
Above code logs in to my site but then when I try to click on Securities button I am not able to do it. Securities button starts flickering and then I am notified that unable to find the element.
Exception in thread "main" org.openqa.selenium.NoSuchElementException:
Unable to find element with xpath == .//span[text()='Issue Type']
(WARNING: The server did not provide any stacktrace information) –
Same code works fine in FireFox.
Please help as i am suppose to test my UI on InternetExplorer.
I think it is the version compatibility issue.
Can anyone suggest the compatible version set for IEDriverServer, Selenium WebDriver and IE which is in working condition.
As this SO answer points out, IE does not have native XPath support. Instead, Selenium WebDriver uses an old third party xpath library when IE is being used. Firefox has integrated support for XPath, which is why your selectors work fine in that browser.
I would highly recommend you update your selectors to instead use CSS selectors. They are supported across all browser, are easier to read, understand, and pick up, and they are pretty fast.
You can learn more about how to use CSS selectors from some different tuturials here, here, and here, and a CSS selectors cheatsheet.
Also, whenever possible, please try to not select an element by the text it contains. If you can select an element by its ID, class, other attribute, or even through the DOM chain (i.e. "div.1 > div.2 > span.a > a.b"), is better than trying to select an element by text.
Webdriver has difficulty with IE using locators. It seems like Murnal has difficulty using CSS locator. My advice would be you HAVE to use other locators if one doesnt work. This issue comes again and again while using non firefox browser. In the meantime an easier way to come up with alternate locator is use Firefox selenium IDE, there in the boxes where you type command you will see it gives alternate locator as well. Copy that and try plugging tha in your webdriver's findelement script.
Hi all i have found out that it was the issue of Selenium Webdriver 2.32 with IEDriver_Server2_32. After trying out permutation & Combination with latest available webdriver versions and IEDriver_Server, i have found out suitable stable configuration to work on IE9 below is the stable configuration : Webdriver : 2.33.0 IEDriver_Server : 2.33.0. There is still small issue but i am trying to look for workaround. Issue : In IE if some control's tooltip overlaps other control than IE is not able to find out that control. i guess this issue is with IEs working. IE uses nativeEvents to perform operation hence it is not able to locate that control. In FF it is able to find out that control and it is working fine. Thanks everyone.

Selenium Webdriver hover not working

Selenium Webdriver 2.31.0
with Scala 2.9
Anyone know how to do a mouse hover in Firefox? I'm basically trying to hover over an element to display a tooltip.
This code fails to move the mouse over the element specified.
val webElement = webDriver.findElement(By.cssSelector(myElement.queryString))
val builder = new Actions(webDriver)
val hover = builder.moveToElement(webElement).build()
hover.perform()
I have also tried mouse events without success (as described here WebDriver mouseOver is not working properly with selenium grid)
This is somewhat anecdotal since I don't have an exact technical explanation, but I've experienced this in the past and have remedied by upgrading Selenium.
The first thing I check is to make sure my selenium is up to date. This includes dependencies, standalone-server and browser drivers (though, in this case, not applicable as Firefox is included with Selenium).
Another possible (and more probable) cause, more directly related to Firefox, is Firefox itself. It's been my experience that a Firefox update can, from time to time, break some selenium functions, particularly hovers. I've found that either upgrading selenium, or if no update has been released, downgrading Firefox will solve the problem.
I wish I had more detailed information to give you, but I'm still learning the finer details of this situation myself. If nothing else, I hope this points you in the right direction.
Since you havn't said you got any errors,
After build().perform(), provide a wait method say, Thread.sleep() for certain amount of time, since there are posibilities where mousehover performed in fraction of seconds and it may not be possible to see the tooltip.
Makesure the locator is correct (because you may point to someother locator which doesn't show up any tooltip)
Makesure you firefox supports the mousehover functionality
The code might resemble as same as your's, but give it a try(JAVA),
Actions builder = new Actions(driver);
WebElement we = driver.findElement(locator);
Actions perf= builder.moveToElement(we).build();
perf.perform();
Thread.sleep(1000);
You can look out the link for your ref : #firefox issue
As your issue is in Firefox, you may need to enable Native Events with webdriver, specifically
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
I've had to do this to get drag and drop working in Firefox on Unix, although it worked with the same code on a Windows box.