Changing browser zoom level when using selenium grid - java

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.

Related

Is there a way to set the initial browser location using selenium?

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).

Window containing an Applet

How can I, in an Applet, get the Window containing the Applet as an Window Object?
My goal is to make a screenshot of the content of a "Website".
I tried to make this with the Robot object:
Window window = ???
BufferedImage bufferedImage = new Robot().createScreenCapture(window.getBorder());
To get the border of the browser is the reason why I need this as a window object.
Or is they another possibility to do this?
To get the border of the browser is the reason why I need this as a window object.
Figure how to do it in JavaScript. Call the JavaScript from the applet.
This might not be the only way to do it, but it is arguably the best. It uses JS for the type of thing that JS is especially good for, interacting with the web page. Get it working and debugged in JS, then it is a relatively simple matter to call that function from the applet.

How to make images appear on your screen without an associated window

MAJOR EDIT
I have restructured my question to be more appropriate and less arbitrary, but the basic message still holds.
What I want to do is make some text appear without an associated window in any of the following languages: Java, C or python. It would just be the text that I want to appear completely overlaid on whatever you happen to currently have on your desktop. It would be overlaid on all programs, etc.
Any recommendations/starting points on how to do this?
OLD POST
I've heard rumors that it's possible to use assembly to make "images" appear (I don't know what they'd actually be considered, what I heard was that my friend professor made a semi-transparent, green dollar bill sign appear and bob up and down on the lower right side of the screen)
So my question is:
Are there any assembly commands/series of commands to make things appear on your screen?
Can anyone point me in the direction to use assembly to interact with what appears on the screen?
Is it possible to have an independent symbol appear without a window associated with it? Or alternatively, is it possible to make the background window completely transparent while still keeping the image intact?
How difficult would it be to create an assembly program which makes text appear in this manner? (i.e. overlaid whatever you currently have on your screen without opening it's own window)
NOTE if you would like to provide any examples, which would be greatly appreciated, I am currently on a mac so my computer only interprets AT&T syntax
OLD POST
Ultimately you DO create a window, you just set its background to transparent. Drop this code into a file called test.java and give it a shot - just tried it out on Mac OS 10.7 and it works. (The text shows up black near the top left corner of the monitor - you have to look carefully to see it.) Note that according to this post you do need Java > 1.6.
import javax.swing.*;
import java.awt.*;
public class test extends JWindow{
public test(){
super();
this.setSize(500, 400);
this.setLocation(50, 50);
setBackground(new Color(0,0,0,0)); // this is the critical line - that fourth 0 represents alpha (or opacity)
setAlwaysOnTop( true ); // keeps it in the foreground so you don't click away from it - note that clicks on the transparent part DO pass through to the desktop, at least on Lion
JLabel testLabel = new JLabel("Floating text hah");
this.add(testLabel);
}
public static void main(String[] args){
System.out.println("Sup");
test t = new test();
t.setVisible(true);
}
}
Hopefully this puts you on the right path.
This is certainly possible, but will depend on your operating system and windowing system as most operating system prevent a program from directly accessing the video memory (to allow program to share it, they have to impose some restriction). The basic idea is to create a transparent borderless window and to draw your content in it. If you ask the windowing system to display your window before all the other window (Z order), then, it will appear as if your program is directly drawing to the screen.
There are some library that does exactly that. For X11 you can use libXosd. For Windows, the OSD executable can display a single string (the source don't appear to be available).

Bring browser from back to front? (Selenium Web Driver, Java)

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

Gui - Best way to navigate between windows?

I try to build a gui (Swing) for a simple java application. The application should have a start window like a menu. From there I would like to navigate to several other windows.
My question is what is the best-practice to achieve such a navigation? Should I build several JFrames and switch the visibility of them on/off when navigating OR should I better have one JFrame and add/remove JPanels in this single frame to navigate between the windows?
Thanks.
I recommend
Do not do a MDI application with sub-frames like those found in the old Windows days. They suck as they make the matter confusing.
Do design a tabbed interface. The welcome page/menu will be displayed on a first tab that is always created on the start.
All cool kids do that nowadays:
Visual Studio
Eclipse
Firefox
If each of your windows correspond to different task (possibly nested), you could present your application as a SDI, with on the left a task panel like:
Each of the task would display one JFrame with the associated window.
Other solution: a table of content like this picture on the left side
(note: it actually also displays a task panel in this example on the bottom right)
Multiple JFrames sounds like a better idea to me. Much more OO.
You must find a balance between these goals:
Not too many things in one "window"
The user must quickly be able to find the correct window to do the next step of work
All relevant information must be visible at any time
Eclipse solves this by creating many small editors where each editor shows some specific information and allows to modify it. Editors are then arranged within one OS window in tabs and "views". A view is always completely visible and they can be arranged. Think of a view as a way to cut an existing editor in half (horizontal or vertical) and then being able to replace one of the halves with another editor. Between each half, you have a splitter so you can adjust the sizes.
Arrangements of views are then saved in "perspectives".
This allows every user to create a perspective which contains all the necessary editors at the same time, arrange them as they need it and work effectively.

Categories