We are developing a web page automation program. I'd like to control where the webpage runs. This code moves the browser to a location after a certain amount of time. I want to move my browser right away from the program. How do you have?
The driver uses a chrome driver.
driver.manage().window().setPosition(new Point(200,0));
Set the position of the browser window right after initializing the driver.
Your code would then look like:
WebDriver driver = new RemoteWebDriver(); //or whatever implementation you use
driver.manage().window().setPosition(new Point(200, 0));
It is possible to move the browser to another monitor. For example you have two monitors with a resolution of 1920x1080 then you need to set the X-axis to >= 1920.
Then you can maximize the window:
driver.manage().window().maximize();
Note: On a Windows system the X-axis depends on your main monitor (Right click on Desktop -> Display settings -> select one monitor and see which one has the checkbox "Make this my main display" ticked.
An example:
You have a three monitor setup and your main monitor is in the center. To move your browser to the right screen you need Point(1920, 0) and to move it to the left screen you need Point(-1920, 0).
On MacOS this does not work because the main monitor is always the last selected one (mouse click).
Related
Currently using selenium (in Java) for automated testing. When running the tests on a selenium grid, the window size changes to a window much smaller than my local (I have no control over the window size when doing a grid run). The smaller window width during the grid run causes the page objects to move in the HTML, changing their references (xpaths, css selectors, etc.). This causes the code to throw a bunch of null pointers. Any ideas on how to get around this? I need a solution that works in Chrome/Firefox/IE
Since I am unable to change the window size, the best solution I've come up with is to zoom out the browser when doing a grid run, moving the objects back into place. I'm using selenium 4.0.0-alpha-1 and tried the following approaches:
Actions class:
new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT)).perform();
Targeting the html tag with the Actions class:
new Actions(driver).sendKeys(driver.findElement(By.tagName("html")),
Keys.chord(Keys.CONTROL, Keys.SUBTRACT)).perform();
Targeting the html tag with the webdriver:
driver.findElement(By.tagName("html"))
.sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
Using javascript:
engine.javaScript.execute("document.body.style.zoom = '75%'");
engine.javaScript.execute("document.body.style.webkitTransform='scale(.75)'");
Using the Robot class:
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_SUBTRACT);
robot.keyRelease(KeyEvent.VK_CONTROL);
--
Methods 1-3 don't work locally or on the grid.
Method 4 works on all browsers (used webkitTransform since its supported across all 3) but it zooms out the CSS and not the browser itself, so the objects remain in the same place despite being zoomed out.
Method 5 works across all browsers locally, but when running on the grid, it nothing happens because I believe the robot needs a GUI to interact with.
Most answers to this question are over 2 years old and don't seem to work anymore, would appreciate any help or if there is another solution to getting around the window sizing. Trying to get around creating separate object references specifically for the grid.
I'm trying to write Webdriver tests where I need to hover the mouse cursor over an element to trigger a drop down menu, and then click a button from the drop down menu. I've been writing my code following the suggestion from How to perform mouseover function in Selenium WebDriver using Java?. So for example, my code might look like this :
Actions action = new Actions(webdriver);
WebElement hoverElem = webdriver.findElement(By.xpath("//html/body/div[13]/ul/li[4]/a"));
WebElement clickElem = webdriver.findElement(By.xpath("//html/body/div[3]/li[12]/a"));
action.moveToElement(hoverElem).moveToElement(clickElem).click().build().perform();
My code runs perfectly when I test it in Firefox, but in Chrome it's inconsistent; sometimes it will work perfectly, and then the next time I run the test it will fail. In Opera it never works. When the code fails, it looks like the drop down menu appears for a split second on the screen, then disappears before WebDriver can click on the button on the drop down menu. I'm not sure how I can fix this problem. How can I get this to work with all 3 browsers?
As a reference, I'm using selenium-2.53.0, Chrome 53.0.2785.101 64-bit, and Opera 39.0.2256.71 64-bit.
In case anyone finds this in the future and is confused about why the mouse-over function works inconsistently with Chrome, Opera, or Internet Explorer, here is why:
The code I have above is correct. The problem is that for whatever reason, mouse over with Chrome, Opera, and IE doesn't work if the mouse cursor is on the browser window while the test is running (this may be an issue within the driver for each of these browsers).
To get around this, you need to ensure the mouse cursor is outside of the browser window while you run tests. I did this by leaving a pixel or two of space on the bottom of the screen when maximizing the browser window, and then using the java.awt.Robot class to move the mouse cursor to the bottom of the screen where the mouse would not interfere with the tests.
Below is an example for my monitor (which is 1680 x 1050, so I leave 40 pixels of space at the bottom of the screen):
driver.manage().window().setPosition(new Point(0, 0));
org.openqa.selenium.Dimension d = new org.openqa.selenium.Dimension(1680, 1010);
driver.manage().window().setSize(d);
To move the cursor out of the way:
Robot robot = new Robot();
robot.mouseMove(0, 1050);
You can call the above whenever you need to reset the mouse cursor to the bottom, for whatever reason.
In Java, a method opens a browser:
desktop.browse(uri);
How can I control for the position of the browser window: I need to put it on top, but also to get it back to the background at some point (for user experience improvements).
I'm currently working on a Java project application for University and it already has a login screen of which loads when the program is ran, and then goes on to the relevant other interfaces.
Anyway, I was wondering how I could have a window to appear for maybe 3 seconds, before the login screen loads, which would simply show the logo of the software (just like you see on Office programs when you first open them and the small window before the main document comes up). Would it require the use of threads to make the window close automatically?
Thanks,
Steve
I was always wondering what pattern is that (i guess now its name is splash screen java) when running a java desktop application and getting that window in the middle of the screen witch seems to be doing somthing (but what exactly...) and witch in some apps annoyingly stays on top and hides other stuff.
To answer your question you could just draw a title window centered and with disabled pannel for the close/maximize/minize buttons and go sleep for 3 seconds Thread.sleep(3000L) then hide the window and continue to the login screen
Or you could while showing the title window do something useful instead of just waiting and start a new Thread to initializing other gui-elements and other stuff that your application might need and wait for that thread to terminate using Thread.join(). When the thread terminates you can hide the window and proceed to the login screen. Thea waiting time would be in this case the time needed for init routine
Also you could consider just putting the logo an title somewhere in a corner on the login screen.
As a user of that software i would not apreciate to wait 3 seconds just watching a logo especialy when the application does nothing.
Hope this helps
Is there any way I can bring browser from the back to the top(front)??
The situation is that there are two browsers, firefox and chrome. So, I instantiated two drivers, new FirefoxDriver() and new ChromeDriver(), let's call them fdriver and cdriver.
What I want is when the program is using Firefox, the firefox browser should be on the top. And so does Chrome. But, I am stuck how to bring the browser to the top when they are on the back.
I already tried,
Javascript: self.focus() and window.focus(). / WebDriverBackedSelenium to make driver back to selenium and use windowMaximize and windowFocus
Any idea is appreciated, thanks
You should be able to do this with
driver.SwitchTo().Window("//name of the window");
and that will bring whatever window you want into focus.
Now I don't use selenium, but I use Geb which is a wrapper around selenium so calling the javascript may be different for you, but this is how I did it (should be similar)
browser.js."alert()"
webdriver.switchTo().alert().accept()
I called the javascript alert function which brought the window to the foreground then I dismissed the alert with webdriver.switchTo().alert().accept().
Robot from java.awt; could help you:
Robot.mouseMove(webDriver.manage().window().getPosition().getX(),webDriver.manage().window().getPosition().getY());
Robot.mousePress(InputEvent.BUTTON1_MASK);
Robot.mouseRelease(InputEvent.BUTTON1_MASK);
Limitations:
if you have more than one monitor, you need to calculate absolute coordinates. As webDriver.manage().window().getPosition().getX() give you offset for the currently used display.
window should be visible