I'm trying to perform an action on a button it's never done.
final Actions action = new Actions(mDriver);
final WebElement myCart = mDriver.findElement(By.cssSelector("path to my span"]"));
final WebElement myButton = mDriver.findElement(By.cssSelector("path to my button"));
action.moveToElement(myCart).build().perform();
action.moveToElement(myButton).click().build().perform();
This code works perfectly with firefox but not with phantom JS
I found some issue here How to handle Mouseover in Selenium 2 API or How to perform mouseover function in Selenium WebDriver using Java? but nothing work with phantom.
Is there any known workaround for this ?
Thanks!
I had similar issues when I used GhostDriver and PhantomJS around a year ago (FYI article). Actually I had problems with IE_Driver and Chrome_Driver too, mostly related with visibility of elements outside the screen_frame (page must be scrolled down).
One of most serious issues was the upload_window and handle it through already-mentioned. I wasn't able to achieve it doh. But my workaround was to switch/cast the driver on these problematic places and after they complete/handle the operation - switch it back to GhostDriver. Even by doing so, the execution speed was impressive.
Hope this helps - even late given.
Update:
find IWebElement to process
set WebDriver from GhostDriver to FirefoxDriver
process the IWebElement item with current WebDriver as FirefoxDriver
verify expected result from processing the IWebElement item
set back WebDriver from FirefoxDriver to GhostDriver
continue workflow
As far as I remember my Test framework implementation - a BaseTest class takes care of initialization of used WebDriver and ISelenium objects. So for your more specific case, you can try this:
// Create a new instance of the Ghost driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new GhostDriver();
//do stuff until new driver is needed
driver = new FirefoxDriver();
//do stuff with new driver
//'cast' back after required operations have been completed and verified
driver = new GhostDriver();
Related
Something like driver.manage().window().maximize(); but for minimize the window.
Thanks!
Selenium doesn't have minimize() option, atleast not for Java, however you can use setPosition do do it
driver.manage().window().setPosition(new Point(0, 0));
However the better way is to run it as headless browser
ChromeOptions options = new ChromeOptions();
options.addArguments("headless");
WebDriver driver = new ChromeDriver(options);
This way you can use maximized browser while it's running in the background.
Selenium's java client have no built-in method for minimizing the browser. Ideally, you shouldn't minimize the browser while the Test Execution is In Progress as Selenium would loose the focus over the Browsing Context and an exception will be raised at any point of time which will halt the Test Execution.
You can find a relevant detailed discussion in How to execute tests with selenium webdriver while browser is minimized
However, to mimic the functionality of minimizing the Browsing Context you can use the following solution:
driver.navigate().to("https://www.google.com/");
Point p = driver.manage().window().getPosition();
Dimension d = driver.manage().window().getSize();
driver.manage().window().setSize(new Dimension(0,0));
driver.manage().window().setPosition(new Point((d.getHeight()-p.getX()), (d.getWidth()-p.getY())));
You can set the position of the WebDriver outside of your view. That way, it'll be out of sight while it runs.
FirefoxDriver driver = new FirefoxDriver();
driver.manage().window().setPosition(new Point(-2000, 0));
OR
Dimension windowMinSize = new Dimension(100,100);
driver.manage().window().setSize(windowMinSize);
Use below code to completely minimize it.
driver.manage().window().setPosition(new Point(-2000, 0))
Let me explain the problem statement :
I want to design Page Object Model for a page using selenium. And the requirement is, scripts executing on multiple browsers will use this class. How should I deal with element locators in my Page class ? What I can think of is
Get the driver object, and using if else, pick the browser specific XPath for locating the element . Like if the driver is chrome then locateChromeElement.
Create different page classes for different browsers.
Create base page class and extend it based on browsers.
What is the best way? What is used in industry?
It depends entirely on your AUT (Application under test). If you have different locators for the same webelement on a page (e.g. in case of multi-lingual sites), then use properties file for storing the webelements and name it as per your page (e.g. for HomePage class you can have different files HomePage.properties (English), HomePage_it.properties (Italian) etc.)
Usually, if you go for CSS for location webelement, you will find it same for almost every browser.
The xpaths will be the same regardless of which browser you use.
to make a script work in multiple browsers, you can create multiple TestNg suites for different browsers, and have the same script for all the suites.
All you need to change in the suites are the Browser classes.
Consider the following script
You can run this entire code in one go. All the test suites will be executed one after the other
class MultipleBrowser{
//for Firefox
#Test
public void FirefoxBrowser()
{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("Automating in firefox Browser");
}
//for ChromeBrowser
#Test
public void ChromeBrowser()
{
WebDriver driver = new ChromeDriver(); //only the class is changed from firefoxDriver to ChromeDriver
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("Automating in Chrome Browser");
}
//for InternetExplorer
#Test
public void IEBrowser()
{
WebDriver driver = new InternetExplorerDriver(); //only the class is changed from ChromeDriver to IEDriver
driver.get("http://www.google.com");
driver.findElement(By.id("lst-ib")).sendKeys("Automating in IE Browser");
}
}
I have an HTML div tag and inside the div there is an element which appears in when mouse enters its boundaries. Now I want to click on the element which becomes visible on mouse entering or hovering.
Issue: the element starts blinking.
Browser: IE8
I am using the code below
IWebElement we = addToBasket.FindElement(By.Id("MyBox"));
action.MoveToElement(we).MoveToElement(driver.FindElement(By.Id("plus-icon"))).Click().Build().Perform();
Any suggestion why its blinking?
The element is blinking because of a feature of the IE driver called "persistent hovers." This feature is of dubious value, but is required because of the brain-dead way IE (the browser, not the driver) responds to WM_MOUSEMOVE messages when using the SendMessage API.
You have a few options. You can turn persistent hovers off by using code like the following:
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnablePersistentHover = false;
IWebDriver driver = new InternetExplorerDriver(options);
Be aware, though that this will subject you to the whims of where the physical mouse cursor is on the screen when you attempt to hover. If that's not acceptable, you have a couple of other approaches you could take. First, you could turn off so-called "native events," which would cause the driver to rely solely on synthesized JavaScript events. This approach has its own pitfalls, due to relying only on JavaScript to synthesize the mouse events.
InternetExplorerOptions options = new InternetExplorerOptions();
options.EnableNativeEvents = false;
IWebDriver driver = new InternetExplorerDriver(options);
Finally, you could migrate from using the default SendMessage Windows API to code that uses the more correct SendInput API. This is done with the RequireWindowFocus property. Its drawback is that the mouse input is injected at a very low level in the system, which requires the IE window to be the foreground window on the system.
InternetExplorerOptions options = new InternetExplorerOptions();
options.RequireWindowFocus = true;
IWebDriver driver = new InternetExplorerDriver(options);
As a final note, do not attempt to set all of these properties at once; pick an approach and stick with it. Several of them are mutually exclusive, and the interaction between them is undefined.
This worked for me.
WebElement element = driver.findElement(By.xpath("element xpath"));
Locatable hoverItem = (Locatable) element;
Mouse mouse = ((HasInputDevice) driver).getMouse();
mouse.mouseMove(hoverItem.getCoordinates());
I have been trying to test a tooltip in my web page using Selenium WebDriver with Firefox 19.
I'm basically trying to use mouse actions to hover over the element that has the tooltip attached in order to test that the tooltip is displayed and to hover over another element to test that the tooltip is hidden.
The first operation works fine but when hovering over another element the tooltip remains visible. This issue does not occur when testing the webpage manually.
Has anyone else encountered this issue before? I'm using Ubuntu 12.04.
It seems that the Advanced Actions API relies on native events, which are disabled in the Linux version of Firefox by default. Therefore, they must be enabled in the WebDriver instance explicitly.
FirefoxProfile profile = new FirefoxProfile();
//explicitly enable native events(this is mandatory on Linux system, since they
//are not enabled by default
profile.setEnableNativeEvents(true);
WebDriver driver = new FirefoxDriver(profile);
Also, in my case I needed to upgrade the WebDriver to version 2.31 since the hover(moveToElement) action did not work properly on 2.30 even with native events explicitly enabled. Tested this with version 2.31 of WebDriver and versions 17 and 19 of Firefox on Linux.
For more information you may check this link:
http://code.google.com/p/selenium/wiki/AdvancedUserInteractions#Native_events_versus_synthetic_events
I also run into this problem with Selenium 2.30 on Firefox 19. It works fine on FF 18.2.
This is a simple but handy method with a javascript call that will send a mouseout() event to whichever element you specify (I prefer to pass them using By but you can change this to whatever you like.
I had a problem with Chrome where tooltips were refusing to close once clicked and obscured other nearby click events causing them to fail. This method saved the day in that case. Hope it helps someone else!
/**
* We need this to close help text after selenium clicks
* (otherwise they hang around and block other events)
*
* #param by
* #throws Exception
*/
public void javascript_mouseout(By by) throws Exception {
for (int i=0; i<10; i++) {
try {
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement element = driver.findElement(by);
js.executeScript("$(arguments[0]).mouseout();", element);
return;
} catch (StaleElementReferenceException e) {
// just catch and continue
} catch (NoSuchElementException e1) {
// just catch and continue
}
}
}
You can call it after any sort of click() event like this:
By by_analysesButton = By.cssSelector("[data-section='Analyses']");
javascript_mouseout(by_analysesButton);
Fyi, mine tries 10x via the for loop with the try/catches because our app has a tendency with Chrome towards stale element exceptions, so if you don't have this problem the method can be pared down considerably.
I had the same problem.
At first I used the method moveToElement() without perform().
Then I added Firefox Profile with setEnableNativeEvents, but it still didn't work for me.
Finally I solved this issue in this way (simply by adding perform():
WebElement username = driver.findElement(By.id("username"));
Actions actions = new Actions(driver);
actions.moveToElement(username).perform();
WebElement tooltip = driver.findElement(By.id("tooltip"));
tooltip.isDisplayed();
and it works fine.
Using Selenium Webdriver 2. java.
I would like to switch back in forth between two firefox browser windows. When I do I get: org.openqa.selenium.NoSuchWindoException: Unable to loacate window"{accb1cc2-74c9-3b4e-8f71-c0b184a037c4}"; duration or timeout:
Here is the java:
driver = new FirefoxDriver();
driver.get("http://mail.google.com");
String firstWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+firstWindowHandle);
Thread.sleep(1000);
driver = new FirefoxDriver();
driver.get("http://www.google.com");
// Get names of currently open windows
String secondWindowHandle = driver.getWindowHandle();
System.out.println("handle of first window ="+secondWindowHandle);
Thread.sleep(1000);
// It fails right here!
driver.switchTo().window(firstWindowHandle );
driver.get("http://www.lifehacker.com");
It prints the following to the console:
- handle of first window = {accb1cc2-74c9-3b4e-8f71-c0b184a037c4}
- handle of the second window = {f5256619-a36e-a441-9979-937da0abacd1}
All help is appreciated.
Unfortunately, you cannot switch between windows the way you are currently trying to do it - WebDriver lost the first window as soon as you instantiated a new instance.
You could try opening the second window via javascript and then switching back and forth from it:
window.open('http://www.bing.com','Bing','modal=yes,alwaysRaised=yes')
This is a bit of a hack, and could have the following problems:
Popup blockers may prevent the action
The browser must have javascript enabled
Future browser versions may break the hack
Complaining and murmuring from peers (and perhaps rightly so) because even though it might work, it's still a hack ;)
Some final thoughts:
Is there any particular reason it has to be the same driver instance?
If not, just switch between two driver instances:
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://mail.google.com");
FirefoxDriver driver2 = new FirefoxDriver();
driver2.get("http://www.google.com");
Swtiching between 2 active Windows:
FirefoxDriver wd=new FirefoxDriver();
wd.get("https://irctc.co.in/");
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);
WebElement wb=wd.findElement(By.linkText("Cabs"));
wb.click(); //Now 2 Windows are open
wd.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS); //Wait for the complete page to load
Set<String> sid=wd.getWindowHandles(); //getWindowHandles() method returns the ids of all active Windows and its return type will be a Collection Set.
Iterator<String> it=sid.iterator(); //Using iterator we can fetch the values from Set.
String parentId=it.next();
System.out.println(parentId);
String childId=it.next();
System.out.println(childId);
wd.switchTo().window(childId); //swtiching control to child Window
wd.close(); //control returns to the parent Window.