Bring browser from back to front? (Selenium Web Driver, Java) - 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

Related

Changing browser zoom level when using selenium grid

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.

How can I force the driver to click on a pop up on a page which has not completed loading

I'm using java with selenium IEDriverServer. I would like to know if is possible to click on a pop up on a page which has not completed loading. When I get the pop up, my code is not running until I click on Ok on pop up.
Thanks.
It's not possible to click on element that is not visible.
But you can add the following config, it will tell the driver to implicitly wait for elements:
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
As you mentioned if is possible to click on a pop up I presume it's a JavaScript Popup.
Essentially, until & unless the synchronous/asynchronous JavaScript/AJAX Calls gets executed completely the Page Loading won't be complete. Hence, invoking click() method while Page Loading may not fetch optimum results.

What is the difference between Selenium WebDriver "Click" and JavascriptExecutor Click

Execution process difference between below two statements.
driver.findElement(By.xpath("//input[#value='Save']")).click();
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
WebDriver click() simulates real user interaction with the UI. I will be performed (in most browsers) by sending a native event to the browser, and it has to be visible in order to click on it. From the docs
...if click() is done by sending a native event (which is the
default on most browsers/platforms)
There are some preconditions for an element to be clicked. The element
must be visible and it must have a height and width greater then 0.
JavaScript click() on the other hand
Executes JavaScript in the context of the currently selected frame or
window.
Regardless if the WebElement is visible or not. This approach misses the idea of user interaction Selenium tries to simulate.
in simple terms. Webdriver uses a native browser events to click on element, and javascript uses JavaScrip to click on the element.
If I remember correctly Selenium 1, was using JavaScript for all it's action but they changed this in webdriver (Selenium 2) and now they are using native browser events to interact with browser. And for this reason you required corresponding support from browser (geckodriver, IEDriver, Chromedriver, etc..). JavaScript engine on the other hand is inbuilt in all the major browsers and so you don't require this extra executables.

handling pop up with many buttons using selenium webdriver

I'm using selenium web driver with Java language. when there are two buttons in a pop up i.e. ok and cancel , it can be easily handled with web driver using the following code:
Alert alert = driver.switchTo().alert();
alert.accept(); // or alert.dismiss(); depending upon the action u want to perform.
but what to do when there are more than two button, i.e. there are 3 to 4 buttons in the pop up ( e.g. like ok,cancel,try again, ignore/continue), in that case what do we do? how can we click on whichever button that we want?
Thank you very much for your help in advance
What we're talking about are JavaScript's dialog popups. There's alert (has an OK button), confirm (has OK / Cancel) and prompt (has an input field and OK). Nothing more. Therefore, the dialog you're seeing is not a JavaScript dialog and can't be handled by Selenium's Alert interface.
You could be dealing with one of those two:
A custom dialog like jQuery's dialog() (or something similar). That's good news! That's no real popup, that's just a well designed overlay consisting of normal HTML made to look like a dialog. You should be able to interact with this the usual way of WebDriver: inspect the elements with the tool of your choice, then find and click the button that needs to be clicked.
A native browser's or even operating system's dialog (a download dialog, for example). That's bad news, as WebDriver can't handle these. Moreover, they tend to look differently across browsers / systems / language settings, so you'll need to detect and handle every case. Your options include:
The Robot class, it allows you to "press" programatically anything on the keyboard (or clicking blindly) and therefore getting rid of the dialog by, say, pressing Enter
AutoIt. It's a Windows program useful for handling any system-level automation.
That's more or less it. You can specify which dialog are you particularly dealing with and we might be able to come up with a better workaround. For example the download dialogs can be avoided entirely, etc.
You only want to use alert() when dealing with native browser popup dialogs. If the web app your testing pops up an HTML dialog then you can select and click on any of the buttons using the element ID, xpath, CSS selector etc.

How to copy text from a browser with Java?

Is there a way to copy text from a browser to my Java app ?
For example, at the left side of my screen I open a browser to point to a URL and shows the content of that page, it might be in a frame or CSS or simple html, on the right side of the screen I open a Java Swing application. I'm interested in certain parts of the browser window that shows some text, and I want my Java app [ without me doing anything ] to copy and paste the text into itself, can it be done ?
I know I can use JEditorPane or JTextPane and set it with an HTMLEditorKit, then load the text into the pane, but if the page uses Frames or some other complex ways, the text I get from the Pane is not what I see on the page, so I don't want to do it by loading the URL into my Java app, instead, I wonder if it can be done in the way I mentioned above ?
I think you're looking at the problem from the wrong angle. If what you want is to harvest a website, I suggest you have a look at the awesome library web-harvest. With a little Xpath wizardry you can get everything you want.
Doing what you describe would imply inter-process communication that seems like an overkill. There are more ways to download a web-page content than the browser.
You may try the following depending on your needs.
With java.awt.Robot you can either 1) Take an screenshot if what you neeed is the content ( without the text, just an image of the browser content ) or 2) Move your self into the browser and programatically press: CTRL-A + CTRL-C and return back to your swing app focus and programatically press: CTRL-V ( or CMD or whatever makes sense in your OS )
But again, this might or not work, depending on what you need.
I know a tool but i am not sure it meet your needs. Have you heard about selenium? http://seleniumhq.org/ It can replicate actions taken by the user in a browser and then manipulate them ussing java code. Have a look at the link it may be handy.
Using java.awt.Robot & a TextField will get the job done, not sure if there is any other way. Have robot press ctrl+a then ctrl+c, bring TextField into focus, and finally have robot press ctrl+v. Now from here you can create a button.setOnAction to save the TextField text into a string. Or you can use a change listener on the TextField setOnKeyReleased to do the same.

Categories